Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the function of the push / pop instructions used on registers in x86 assembly?

When reading about assembler I often come across people writing that they push a certain register of the processor and pop it again later to restore it's previous state.

  • How can you push a register? Where is it pushed on? Why is this needed?
  • Does this boil down to a single processor instruction or is it more complex?
like image 633
Ars emble Avatar asked Jan 03 '11 11:01

Ars emble


People also ask

What is the function of Push & Pop instruction?

Instructions that store and retrieve an item on a stack. Push enters an item on the stack, and pop retrieves an item, moving the rest of the items in the stack up one level.

What does the pop function do in assembly?

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.

What is push and pop instruction in 8086?

The PUSH/POP instructionsPUSH decrements the SP register (by 2) and copies a value onto the top of the stack. POP retrieves the value from the top of the stack and stores it into the destination, then increments the SP register (by 2).

What is pop instruction?

The POP instruction reads a byte from the address indirectly referenced by the SP register. The value read is stored at the specified address and the stack pointer is decremented. No flags are affected by this instruction.


1 Answers

pushing a value (not necessarily stored in a register) means writing it to the stack.

popping means restoring whatever is on top of the stack into a register. Those are basic instructions:

push 0xdeadbeef      ; push a value to the stack
pop eax              ; eax is now 0xdeadbeef

; swap contents of registers
push eax
mov eax, ebx
pop ebx
like image 171
Linus Kleen Avatar answered Oct 11 '22 08:10

Linus Kleen