Mov instructions and registers - confusion!

I read Computer Systems: A Programmer's Perspective, Chapter 3 explains the mov instruction, and the explanation given in the book confuses me.

give function (p. 142 1 issue)

 int exchange( int *xp, int y) { int x = *xp; *xp = y; return x; } 

Function body assembly code

 movl 8(%ebp), %eax //Get xp movl 12(%ebp), %edx //Get y movl (%eax), %ecx //Get x at *xp movl %edx, (%eax) //Store y at *xp movl %ecx, %eax //Set x as return value 

What bothers me is what will be stored, and where Here's how I understand it:

 movl 8(%ebp), %eax //Get xp 

The CPU moves +8 bytes to the stack (from the frame pointer %ebp ), takes the value stored in this place, and stores this value in the %eax register (for allocation, it stores the value, not the address)

I'm right? Thanks!

+7
assembly
source share
3 answers

Yes, it looks like everything is fine with you. IMHO, the syntax of AT & T 8(%ebp) less intuitive than Intel [ebp+8] , which is clearer. The brackets indicate that you are using the value at the address in the register, and the number is the offset from the address you really want.

+9
source share

Yes, this is the AT & T syntax, which takes the form:

 instruction source, dest 

Intel assembly is the opposite order .

You are also right at 8(%ebp) moving 8 bytes from the frame pointer. The reason that it moves 8 bytes, in particular, is that the parameters are pushed onto the stack in the reverse order ("right" to "left" when viewing a typical function call). Thus, y was pressed first, then xp and finally the return address of the calling function (so you are moving 8 bytes, not 4).

+2
source share

You need to understand what a stack stack is. Find out exactly what push and pop instructions do. Before this code was

  push y_val push xp_ptr call exchange .cont ... .exchange push ebp mov ebp, esp // .. rest of code // stack frame: old_ebp_val ; [ebp] points here .cont ; [ebp + 4] xp_ptr ; [ebp + 8] y_val 
+1
source share

All Articles