Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assembly Language: + vs add

Tags:

x86

assembly

I am a university student studying assembly language on my own time. I noticed that while there are instructions like add and mul, arithmetic operators are often used within instructions. For example:

mov eax,[ebx+ecx]

Is it equivalent to the following?

add ebx,ecx
mov eax,[ebx]

(ignoring the change in the contents of ebx)

like image 319
Anshuman Fotedar Avatar asked Dec 31 '25 01:12

Anshuman Fotedar


2 Answers

Nearly... This index address method ([ebx+ecx] or any other) is normaly used for addressing elements in some array or record. In your case the ebx can be a pointer to byte array and ecx is an index. Using index addressing can be dangerous because there is no efected flags after arithmetic operations so we can't check array range overflow. Normaly high level compilers in debug mode use clasic slower method, so that we are able to detect array range overflow. When we switch compiler to relase mode (and we are sure that there is no more possible bugs) the faster index addressing method is in use.

like image 107
GJ. Avatar answered Jan 04 '26 20:01

GJ.


No, it is not equivalent. If this is real code, then the thing inside the brackets is an example of an addressing mode. The addressing mode controls how the effective address for the operation is computed, but it typically does not have a persistent effect. Your second code snippet actually adds to the ebx register, changing its contents for any following instructions.

Update: I just saw your last sentence about ignoring the change ... If you want to ignore that, then yes, I believe the two snippets are equivalent.

like image 26
unwind Avatar answered Jan 04 '26 22:01

unwind



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!