flat assembler
Message board for the users of flat assembler.
Index
> Main > Shorter encoding when a register has known value |
Author |
|
l4m2 21 Jul 2015, 00:50
when eax equals to 0, people still use
Code: lea ecx, [edx*4] Code: [eax+4edx] |
|||
21 Jul 2015, 00:50 |
|
Feryno 21 Jul 2015, 13:00
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] |
|||
21 Jul 2015, 13:00 |
|
PeExecutable 21 Jul 2015, 13:16
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.
|
|||
21 Jul 2015, 13:16 |
|
l4m2 21 Jul 2015, 13:26
not only lea. I try to have static variables but direct address costs 3 more bytes than
|
|||
21 Jul 2015, 13:26 |
|
l4m2 21 Jul 2015, 13:32
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
|
|||
21 Jul 2015, 13:32 |
|
revolution 21 Jul 2015, 13:45
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 |
|||
21 Jul 2015, 13:45 |
|
l4m2 21 Jul 2015, 13:57
Code: assume eax 0 lea ecx, [edx*4] ;8D0C90 endassume |
|||
21 Jul 2015, 13:57 |
|
l4m2 21 Jul 2015, 14:02
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 ? |
|||
21 Jul 2015, 14:02 |
|
shutdownall 22 Jul 2015, 10:46
l4m2 wrote:
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 !). |
|||
22 Jul 2015, 10:46 |
|
revolution 22 Jul 2015, 14:59
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 ;... |
|||
22 Jul 2015, 14:59 |
|
l4m2 23 Jul 2015, 01:36
revolution wrote: I suppose one could have this: besides Code: ecx=edx*8 eax=0 |
|||
23 Jul 2015, 01:36 |
|
l4m2 24 Jul 2015, 15:29
Quote:
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 Code: imul ecx,edx,4 Last edited by l4m2 on 25 Jul 2015, 03:31; edited 1 time in total |
|||
24 Jul 2015, 15:29 |
|
shutdownall 24 Jul 2015, 16:16
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. |
|||
24 Jul 2015, 16:16 |
|
l4m2 25 Jul 2015, 03:25
shutdownall wrote: You are talking here only about partial optimizations. |
|||
25 Jul 2015, 03:25 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.