What is an assembly-level representation of pushl/popl %esp?

amorimluc picture amorimluc · Feb 19, 2013 · Viewed 13.3k times · Source

C++

ATT Assembly

I'm trying to understand the behavior of the following two instructions:

pushl %esp

And:

popl %esp

Note that they store the computed value back into %esp.

I'm considering these instructions independently, not in sequence. I know that the value stored in %esp is always the value before the increment/decrement, but how could I represent the behavior in assembly language? This is what I've come up with so far:

For push:

movl %esp, %edx     1. save value of %esp
subl  $4, %esp      2. decrement stack pointer
movl %edx, (%esp)   3. store old value of %esp on top of stack

For pop:

movl (%esp), %esp   You wouldn’t need the increment portion. 

Is this correct? If not, where am I going wrong? Thanks.

Answer

nrz picture nrz · Feb 19, 2013

As it says about push esp in Intel® 64 and IA-32 Architectures Developer's Manual: Combined Volumes:

The PUSH ESP instruction pushes the value of the ESP register as it existed
before the instruction was executed. If a PUSH instruction uses a memory operand
in which the ESP register is used for computing the operand address, the address
of the operand is computed before the ESP register is decremented.

And as regards to pop esp:

The POP ESP instruction increments the stack pointer (ESP) before data at the old
top of stack is written into the destination.