Search code examples
assemblyx86gnu-assembler

Difference between value of reg, and adress of reg


How is this different?:

movl 4(%esp), %eax # put old ptr into eax
9 popl 0(%eax)

The first instruction puts content of first arg to %eax, but the second does as well (topmost content of stack), or is it indirect adressing? Will not the values overlap each other in %eax?


Solution

  • 0(%eax) is the memory location at the address stored in eax. %eax is the content of eax. The two are not equivalent. Also note that x86 uses a fully-descending stack, so pop 0(%eax) is equivalent to

    mov (%esp), 0(%eax)
    add $4, %esp
    

    i.e. it loads from (%esp) and not 4(%esp) as your other instruction. pop also changes the value of esp which mov 4(%esp), %eax does not do. (do also note that mov (%esp), 0(%eax) is not actually a valid instruction, this is just for illustration of the principle).