开发者

Why does pop take a parameter in assembly?

Consider:

popl   %ebp

It seems the %ebp开发者_JAVA百科 is unnecessary, because the pop operation of stack doesn't need a parameter.

Why does it make sense?


From x86 Assembly Guide:

pop — Pop stack

The pop instruction removes the 4-byte data element from the top of the hardware-supported stack into the specified operand (i.e. register or memory location). It first moves the 4 bytes located at memory location [SP] into the specified register or memory location, and then increments SP by 4.

Syntax
pop <reg32>
pop <mem>

Examples
pop edi — pop the top element of the stack into EDI.
pop [ebx] — pop the top element of the stack into memory at the four bytes starting at location EBX.

Another good reference is x86 Assembly and it is available in PDF form.


This parameter sets the destination.


To expand on Andrey's answer, in addition to incrementing the stack pointer over the popped element, the popped element is also copied to a destination address or register.

The instruction you gave is more or less equivalent to the two instructions (Intel syntax)

add esp, 4               # increment the stack pointer
mov ebp, [esp - 4]       # load what ESP was pointing to

which I think is this in att (gas) syntax

add $4, %esp
mov -4(%esp), %ebp

Of course pop doesn't modify FLAGS (so imagine doing the add with LEA), and it's not interruptible between the load and add.

For the special case of pop esp, doing the load 2nd in the pseudocode replicates the actual documented behaviour of incrementing before data from the old top-of-stack location is written to ESP. And of doing the increment before address calculation for a memory-destination pop using %esp as part of the addressing mode, like popl 12(%esp, %edx, 4)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜