Is there any difference between the following two:
mov eax, [eax+4]
and
add eax, 4
mov eax, [eax]
If not then, does the assembler choose which one to pick for some kind of optimization?
There are a few differences between these two pieces of code. For one, the instruction encoding and sizes. The first is encoded as follows...
8b 40 04 mov eax, [eax+4]
...amounting two 3 bytes.
For the second one...
83 c0 04 add eax, 4
8b 00 mov eax, [eax]
...totals 5 bytes.
Moreover, the add instruction sets the OF
(overflow), SF
(sign), ZF
(zero), AF
(adjust/auxiliary), CF
(carry), and PF
(parity) flags according to the result. The mov
instruction neither sets or scrambles any of these flags.
Finally to answer your last question... No. There is no existing assembler that helps with any optimization. Code in assembly is compiled on a strictly literal basis. My suggestion is to use the 3-byte piece of code, as it is shorter and executes faster.
Good luck!
Yes, there are differences. Whether you care about them is a different matter. The add
affects the flags, the [eax+4]
doesn't. The two versions have different instruction and byte counts and they possibly use different execution units. These might matter when optimizing.
Normally, the assembler will not change your code, whatever you write is what you will get.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With