Search code examples
assemblyx86masm

ADD instruction or plus sign?


Very simble question, yet I didn't find a good answer so I'm asking here.
I see that sometimes for addition we use the ADD instruction, and sometimes I see the use of the plus sign (+).
Please look at the following:

mov eax,[esi + TYPE DWORD]

Well, I learned that for adding numbers I should use the ADD instruction, yet it seems to work like this.
I thought it should have been done this way:

add esi, TYPE DWORD
mov eax,[esi]
sub esi, TYPE DWORD

I mean, how can the computer access the memory address esi + TYPE DWORD without doing an add instruction? so odd...

Thank you so much for reading (:


Solution

  • First of all, these are encoded as two different things - the ADD is an instruction, will come with a full opcode, while the + will be translated by your assembler into referencing mode for the main instruction it's attached to (MOV in this case).

    The main difference however is that the first will be performed by the memory unit (or more accurately - the address generation unit), without consuming any additional arbitration on top of the load execution, while the latter will be performed over the ALU, and consume the required resources accordingly (depending on the micro architecture, but in your example, on any modern out-of-order CPU that would take at least a few queue entries, decode slots, scheduling slots, ports, etc..).

    In that sense the first is often "cheaper", which is why it's often used even for normal arithmetic operations without memory referencing, by using a LEA instruction, as Harold pointed out.