What does 0x4 do in "movl $0x2d, 0x4(%esp)"?

assembly, c, gnu-assembler, x86

Solution

`movl $0x2d, 0x4(%esp)` means to take the current value of the stack pointer (`%esp`), add 4 (`0x4`) then store the long (32-bit) value `0x2d` into that location.

The `eax` register is one of the general purpose 32-bit registers. x86 architecture specifies the following 32-bit registers:

eax  Accumulator Register
ebx  Base Register
ecx  Counter Register
edx  Data Register
esi  Source Index
edi  Destination Index
ebp  Base Pointer
esp  Stack Pointer

and the names and purposes of some of then harken back to the days of the Intel 8080.

This page gives a good overview on the Intel-type registers. The first four of those in the above list can also be accessed as a 16-bit or two 8-bit values as well. For example:

3322222222221111111111
10987654321098765432109876543210
<-             eax            ->
                <-     ax     ->
                <- ah -><- al ->

The pointer and index registers do not allow use of 8-bit parts but you can have, for example, the 16-bit `bp`.

Problem

I am looking into assembly code generated by GCC. But I don't understand: ``` movl $0x2d, 0x4(%esp) ``` In the second operand, what does `0x4` stands for? offset address? And what the use of register EAX?

Original source