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:
- 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. - 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, includingEAX
, and you might need the original value ofEAX
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:
push EAX
saves the original content ofEAX
onto the stack.call MySubroutine
calls the subroutine, which may modifyEAX
. The return value of the subroutine is typically placed in theEAX
register, overwriting the previous value.pop EAX
retrieves the saved value from the stack and restores it toEAX
.
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. |