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

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.

But why 8?

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.

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)

depends on the compiler. What compiler did you use to produce that assembly code?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.