flat assembler
Message board for the users of flat assembler.

Index > Main > Shorter encoding when a register has known value

Author
Thread Post new topic Reply to topic
l4m2



Joined: 15 Jan 2015
Posts: 657
l4m2
when eax equals to 0, people still use
Code:
lea ecx, [edx*4]    
but
Code:
[eax+4edx]    
shorter won't ?
Post 21 Jul 2015, 00:50
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17664
Location: In your JS exploiting you and your system
revolution
It uses 4 fewer bytes. But if you ever change the preceding code and the value in EAX is not zero then you also need to update the LEA. It is a potential bug trap and relies upon remembering such finicky details, and/or having great comments to explain what is happening.


Last edited by revolution on 21 Jul 2015, 13:07; edited 1 time in total
Post 21 Jul 2015, 09:36
View user's profile Send private message Visit poster's website Reply with quote
Feryno



Joined: 23 Mar 2005
Posts: 457
Location: Czech republic, Slovak republic
Feryno
I like to use LEA instead of ADD/SUB when I need not to destroy flags and also in case of some multiplication with some numbers like 3, 4, 5, 8, 9
in 64 bit mode the shortest encoding is like
Code:
lea ecx,[rax+rdx*4]    
Post 21 Jul 2015, 13:00
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
PeExecutable



Joined: 26 Jun 2015
Posts: 181
PeExecutable
Sometimes it is beneficial to use a lea, and some other below, sometimes an add or a inc instruction helps. You have to reorder and replace them and time the code until you find the combo that is faster. Do not rely in your understanding only, rely in timing. It's amazing how the processor tricks you, it has become a smarter unit over the years. Similarly, be careful to rely in timing if you have hyper threading enabled.
Post 21 Jul 2015, 13:16
View user's profile Send private message Reply with quote
l4m2



Joined: 15 Jan 2015
Posts: 657
l4m2
not only lea. I try to have static variables but direct address costs 3 more bytes than
Post 21 Jul 2015, 13:26
View user's profile Send private message Reply with quote
l4m2



Joined: 15 Jan 2015
Posts: 657
l4m2
I just found that the title has been changed from assume to Shorter encoding when a register has known value. assume because in some compiler assume can be used to optimize
Post 21 Jul 2015, 13:32
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17664
Location: In your JS exploiting you and your system
revolution
l4m2 wrote:
I just found that the title has been changed from assume to Shorter encoding when a register has known value. assume because in some compiler assume can be used to optimize
I changed the title because "assume" didn't convey any information about the topic. But you are free to change it again at any time if you feel it should something else. Although, I would urge you to say more about what you are suggesting, because by itself just having "assume" in the title is ambiguous.
Post 21 Jul 2015, 13:45
View user's profile Send private message Visit poster's website Reply with quote
l4m2



Joined: 15 Jan 2015
Posts: 657
l4m2
Code:
assume eax 0
  lea ecx, [edx*4]  ;8D0C90
endassume    
Post 21 Jul 2015, 13:57
View user's profile Send private message Reply with quote
l4m2



Joined: 15 Jan 2015
Posts: 657
l4m2
Code:
foo:push esi
    mov esi, var
    assume esi var
      add [var1], eax
      xor [var3], ecx
    endassume
    pop esi
    ret
var:
var1  dd ?
var2  dd ?
var3  dd ?    
Post 21 Jul 2015, 14:02
View user's profile Send private message Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 518
Location: Munich
shutdownall
l4m2 wrote:
Code:
assume eax 0
  lea ecx, [edx*4]  ;8D0C90
endassume    


Sounds nice but how will you assure, that eax is 0 ?
You can "assume" whatever you want but one very important register ist restricted to use - doesn't make much sense for me. The only way to assure is to paste xor eax,eax first but then you have only one byte less (and eax destroyed !). Wink
Post 22 Jul 2015, 10:46
View user's profile Send private message Send e-mail Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17664
Location: In your JS exploiting you and your system
revolution
I suppose one could have this:
Code:
;...
  cmp eax,0 ;or test eax,eax, or whatever
  jnz .somewhere
  assume eax 0
    lea ecx, [edx*4]  ;8D0C90
  end assume
;...    
Post 22 Jul 2015, 14:59
View user's profile Send private message Visit poster's website Reply with quote
l4m2



Joined: 15 Jan 2015
Posts: 657
l4m2
revolution wrote:
I suppose one could have this:
Code:
;...
  cmp eax,0 ;or test eax,eax, or whatever
  jnz .somewhere
  assume eax 0
    lea ecx, [edx*4]  ;8D0C90
  end assume
;...    

besides
Code:
ecx=edx*8
eax=0    
can
Post 23 Jul 2015, 01:36
View user's profile Send private message Reply with quote
l4m2



Joined: 15 Jan 2015
Posts: 657
l4m2
Quote:

Sounds nice but how will you assure, that eax is 0 ?

You can "assume" whatever you want but one very important register ist restricted to use - doesn't make much sense for me. The only way to assure is to paste xor eax,eax first but then you have only one byte less (and eax destroyed !).

Just let ecx=edx*4 these code have same length:
Code:
xor ecx,ecx
lea ecx,[ecx+4edx]

lea ecx,[4edx+eax]
sub ecx,eax

mov ecx,edx
shl ecx,2

lea ecx,[2edx]
shl ecx,1 ;or maybe add ecx,ecx instead    
and this code is shorter but maybe slower(?)
Code:
imul ecx,edx,4    


Last edited by l4m2 on 25 Jul 2015, 03:31; edited 1 time in total
Post 24 Jul 2015, 15:29
View user's profile Send private message Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 518
Location: Munich
shutdownall
You are talking here only about partial optimizations.
In fact you can do much more with optimized datastructures than just keeping an eye on single instructions. Cool
Post 24 Jul 2015, 16:16
View user's profile Send private message Send e-mail Reply with quote
l4m2



Joined: 15 Jan 2015
Posts: 657
l4m2
shutdownall wrote:
You are talking here only about partial optimizations.
In fact you can do much more with optimized datastructures than just keeping an eye on single instructions. Cool
Always lots of instruction when needed (especially for global variables)
Post 25 Jul 2015, 03:25
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.