askvity

What is push EAX?

Published in Assembly Instructions 3 mins read

The push EAX instruction is used to place the value currently stored in the EAX register onto the stack. This is a common practice in assembly programming, especially when calling subroutines or functions.

Understanding the push EAX Instruction

The push EAX instruction performs the following actions:

  1. Decrements the Stack Pointer: The stack pointer (usually ESP) is decremented by 4 bytes (on a 32-bit system). This makes space on the stack for the value being pushed.
  2. Copies the Value: The value currently held in the EAX register is copied onto the stack at the memory location pointed to by the stack pointer.

Why Use push EAX?

Here are some common reasons for using push EAX:

  • Preserving Register Values: As the reference states, the instruction PUSH EAX is used to preserve the value of EAX before calling a subroutine. This is important because subroutines often modify registers, including EAX, and you might need the original value of EAX after the subroutine returns.

  • Passing Arguments to Functions: In some calling conventions, arguments are passed to functions via the stack. push EAX could be used to push an argument onto the stack before calling the function.

  • Local Variable Storage: Although less common than using the stack frame directly, you could theoretically use push EAX to reserve space on the stack for a local variable (by pushing a dummy value and then modifying it).

Example Scenario: Subroutine Call

Let's say you have a subroutine that calculates a value and returns it (often in the EAX register). Before calling this subroutine, you might want to save the current value of EAX. Here's a simplified example:

; Save the current value of EAX
push EAX

; Call the subroutine
call MySubroutine

; Restore the original value of EAX
pop EAX

In this example:

  1. push EAX saves the original content of EAX onto the stack.
  2. call MySubroutine calls the subroutine, which may modify EAX. The return value of the subroutine is typically placed in the EAX register, overwriting the previous value.
  3. pop EAX retrieves the saved value from the stack and restores it to EAX.

This ensures that after the subroutine returns, EAX contains its original value, as it did before the call. The reference notes that, when a subroutine is called the return value is kept in the register EDX after execution, rather than EAX, however, the principle of preserving EAX before a subroutine call remains sound if your subroutine also modifies the EAX register.

Summary

Aspect Description
Instruction push EAX
Function Pushes the value in the EAX register onto the stack.
Stack Pointer Decrements ESP (or equivalent stack pointer register) by 4.
Common Use Cases Preserving EAX before subroutine calls, passing arguments.

Related Articles