3

I am studying up on IA32. When I think about what the popl DEST instruction is doing I think the following:

movl (%esp), DEST
addl $4, %esp

But then I started second guessing myself when I thought about popl %esp. Even though that is probably a pointless instruction, I think there is probably a better way to think of generally describing the popl DEST instruction. How would you describe it?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Kyle Weller
  • 2,533
  • 9
  • 35
  • 45
  • I'm sorry that someone did not find my question useful. Please feel free to comment on it so that I can improve questions in the future. – Kyle Weller Feb 18 '13 at 03:38
  • The `add` instruction modifies the status flags. Therefore `lea esp, [esp + 4]` would be a better match to what `pop` does. – ecm Sep 02 '19 at 11:19

1 Answers1

4

Here's a small portion of the pseudo code for the POP instruction from Intel's documentation:

IF StackAddrSize = 32
  THEN
    IF OperandSize = 32
      THEN
        DEST ← SS:ESP; (* Copy a doubleword *)
        ESP ← ESP + 4;
      ELSE (* OperandSize = 16*)
        ...
    FI;
...

But here's what it says specifically about POP xSP:

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

This means that the sequence

PUSH ESP
POP ESP

does nothing out of ordinary, just like this one:

PUSH EAX
POP EAX

Similarly, there's some text on PUSH xSP:

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.

Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180
  • So it looks like the way I chose to describe it is just like the documentation from Intel. There is just a special note for `pop %esp` that says that it increments the stack pointer first. – Kyle Weller Feb 18 '13 at 03:37
  • @KyleWeller: Indeed. Intel could have written pseudo-code that actually matched the behaviour of `pop esp` and `push esp`, but for some reason chose not too. It's barely more complicated than the current pseudo-code, just adding a temporary. [What is an assembly-level representation of pushl/popl %esp?](https://stackoverflow.com/a/69489798) – Peter Cordes Dec 12 '21 at 06:34