I read a book that write the next program in C, and convet the call of this function to assembler code:
int *p; /* pointer to integer */
int foo (int n, int *q) {}
/* function get int and pointer to int, returns int */
/* Now, let's call the function: */
*p = foo (*p, p);
It convert to:
MOV EBX, [P]
PUSH EBX
PUSH DWORD [EBX]
CALL foo
MOV EBX, [P]
MOV [EBX], EAX
ADD ESP, 8
I didn’t understood why it is correct, as I understood it the code should looks like this:
MOV EBX, P ;; **CHANGE**
PUSH EBX
PUSH DWORD [EBX]
CALL foo
MOV EBX, P ;; **CHANGE**
MOV [EBX], EAX
ADD ESP, 8
and that because P is a pointer. If we do MOV EBX, [P], as the book suggest, we got the integer number (Not the adress), and then if we do PUSH DWORD [EBX], we got illigal instruction.
Where i’m wrong?
Pis a label, which equates to the address of a variable (your pointer variable).[P]would be the value at that address, which is the pointer.Note, though, some assemblers do things a bit differently. NASM and its derivatives are pretty strict about brackets and such. MASM, not so much; there are times it will let you treat a label almost as if it were a variable in its own right.