0

Hello. I've started to learn assembly this week, and I found a example that I couldn't understand, so, if anyone could explain it to me, I would appreciate.

int increment(int x) { 
          x = x + 1;
          return(x);
}

and this got translated to:

pushl %ebp 
          movl %esp, %ebp
          incl 8(%ebp)
          movl 8(%ebp), %eax
          popl %ebp 
          ret

So my question is, why incl/movl 8, and not 4 ? doesnt esp "move it" 4 places ?
Thank you very much

2
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by Ancient Dragon
0

The function return address is in the first 8 bytes. The 8 is the offset into the stack pointer whose address is in ebp register.

0

I just told you. Before a function is called the program pushes the return address onto the stack, which is 8 bytes. Then it pushes each of the parameters. The ebp register contains the stack address of where the return address was stored. So to get to the first byte of the parameter it has to add 8 to the ebp register.

0

but i'm working a 32 bit CPU, shouldn't the return address be 4 bytes instead of 8 bytes ? (sorry for the first misunderstanding)

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.