0

Hello,
I am beginning learning intel assembly. I am taking simple gcc programs and looking at the assembly. All gcc outputs have a line similar to:
mov DWORD PTR [ebp-4] that I can't figure out.
For example

int main(void)
{  
   int x = 5;
   return 0;
}

gives me

.file "test.c"
   .intel_syntax noprefix
   .text 
.globl main
   .type main, @function
main:
   push  ebp
   mov   ebp, esp ; memory address of start of stack
   sub   esp, 16  ; push for room for a word
   mov   DWORD PTR [ebp-4], 5 ; I'm confused here
   mov   eax, 0 ; retval of function
   leave
   ret
   .size main, .-main
   .ident   "GCC: (Ubuntu 4.4.1-4ubuntu9) 4.4.1"
   .section .note.GNU-stack,"",@progbits

I know it is putting the value of 5 on the stack, but why at ebp-4? A word is 2 bytes or 16 bits. Is the 4 in bits or bytes? Since, gcc has subtracted 16 from the esp, why not put it at ebp?

Thanks

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by NotNull
1

Its being used to access local vars.

sub esp, 16
The stack pointer has 16 subtracted from it to reserve room
for 4 doublewords as local variables on the stack.
mov DWORD PTR [ebp-4], 5
Copies 5 into first doubleword on the stack.

Take a look at the stack at this point:
XXXXXXXXXXXXXXXXX] - 16 <--- ESP points here
XXXXXXXXXXXXXXXXX] - 12
XXXXXXXXXXXXXXXXX] - 8
XX XX XX XX XXXXXXX] - 4 Beginning of Local Vars
PUSHED VALUE OF EBP] + 0 <--- EBP points here
RETURN ADDRESS XX ] + 4
XXXXXXXXXXXXXXXXX] + 8 Parameters usually begin here

Edited by NotNull: n/a

Votes + Comments
Nice
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.