I'm reading my textbook and it has code for a swap function:
In C:
int exchange(int *xp, int y) {
int x = *xp;
*xp = y;
return x;
}
In x86 Assembly with annotations:
// xp is at %ebp + 8, y at %ebp + 12
movl 8(%ebp), %edx // get xp
movl (%edx), %eax // get x at xp
movl 12(%ebp), %ecx // get y
movl %ecx, (%edx) // store y at xp
So from my understanding, if the int* xp pointed to an int I at address A, then the first line of assembly code stores A at %edx. Then it gets dereferenced in the second line and stored at %eax.
If this is true, I'm wondering why line 1's "8(%ebp)" doesn't dereference the pointer, storing the int I in %edx instead of the address A? Isn't that what parentheses do in assembly?
Or does that mean that when pointers are pushed onto the stack, the address of the pointer is pushed on rather than the value it holds so 8(%ebp) technically holds &xp?
Just wanted to clarify if my understanding was correct.