user2519974
user2519974

Reputation: 11

Assembly in IA-32 and x86-64 POP instruction

What does pop ebp mean in an IA-32 and x86-64 machines, at the end before a ret (return) instruction of code? So I do have old and new ebp, and the new ebp is pushed into the stack by a call of a function. Then this ebp is pop'ed? How does the pop instruction changes the value of ebp?

Upvotes: 1

Views: 955

Answers (1)

Michael
Michael

Reputation: 58427

PUSH EAX

Essentially means:

SUB ESP,4
MOV [ESP],EAX

And

POP EAX

Means:

MOV EAX,[ESP]
ADD ESP,4

When you talk about old and new EBP I guess you're referring to the function prologues and epilogues?

PUSH EBP     ; Store caller's EBP on the stack
MOV EBP,ESP  ; Set EBP to the current stack pointer

; Here we can do things like:
MOV EAX,[EBP+8]
PUSH EAX
MOV EBX,[EBP+12]
POP EAX
; ..to access the stack. Since we've got a fixed reference point for
; the stack in EBP we don't have to worry about the stack pointer
; changing.

; For example, we could do this instead to access the same stack
; elements as above:
MOV EAX,[ESP+8]
PUSH EAX
MOV EBX,[ESP+16]
POP EAX
; But notice that we had to change the second offset since the push
; instruction changed the stack pointer. It's obviously easier to deal
; with a base address that doesn't change every time we execute
; certain instructions.

MOV ESP,EBP  ; Restore the stack pointer
POP EBP      ; Restore the caller's EBP before returning

Upvotes: 2

Related Questions