28

You'll have to excuse me, I'm brand new to x86 assembly, and assembly in general.

So my question is, I have something like:

addl %edx,(%eax)

%eax is a register which holds a pointer to some integer. Let's call it xp

Does this mean that it's saying: *xp = *xp + %edx? (%edx is an integer)

I'm just confused where addl will store the result. If %eax is a pointer to an int, then (%eax) should be the actual value of that int. So would addl store the result of %edx+(%eax) in *xp? I would really love for someone to explain this to me!

I really appreciate any help!

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
kodai
  • 3,080
  • 5
  • 32
  • 34

1 Answers1

51

Yes, this instruction is doing exactly what you think it's doing.

Most x86 arithmetic instructions take two operands: a source and a destination. In AT&T syntax (used here), the destination is always the right operand. So with an instruction like:

addl %edx, %eax

the values in edx and eax are added together and the result is stored in eax. However, in your example, (%eax) is a memory operand; that's what parentheses mean in AT&T syntax (like square-brackets in NASM syntax).

This means that eax is treated as a pointer, so the right operand is taken from the address pointed to by eax, and the result is stored to the same address.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Jay Conrod
  • 28,943
  • 19
  • 98
  • 110
  • `(%eax)` is "register indirect", if you want to [name different addressing modes](https://stackoverflow.com/questions/46257018/do-terms-like-direct-indirect-addressing-mode-actual-exists-in-the-intel-x86-man) at all. "Memory indirect" addressing is when the CPU loads an address from memory and then dereferences *that* (x86 doesn't support memory-indirect addressing). https://en.wikipedia.org/wiki/Addressing_mode#Memory_indirect – Peter Cordes Sep 18 '17 at 03:56