I'm reading through a book that is describing C linked lists and how they are represented within x86 ASM. I having difficulty understanding the instruction MOV [edx], eax
.
If the instruction was reversed, MOV eax, [edx]
, I would understand it to mean copy the 4 bytes represented by the memory address stored in edx and store it in eax.
What does MOV [edx], eax
represent?
If using the []
with the MOV
instruction, I thought it meant to copy the data residing at the memory address to it's destination. If that is true, how can you copy whatever is in eax
to a data value in edx
?
It's Intel assembly syntax. In Intel assembly syntax the destination is always the first operand, the rest operands are source operands. The other commonly used assembly syntax for x86 is AT&T, but as Intel and AT&T syntaxes look very different, they are easy to distinguish.
mov [edx],eax
stores the value of eax
in memory, to the address given in edx
(in little-endian byte order).
mov eax,[edx]
does exactly the reverse, reads a value stored from the memory, from the address given in edx
, and stores it in eax
.
[reg]
always means indirect addressing, it's just like a pointer *reg
in C.
To copy the contents of eax
to edx
, all you need is mov edx,eax
. Destination is first operand, source is the second operand.