Questions about C language function call pressure stack and return value, function return value
According to the C compiler, when calling a function, the pressure stack sequence is from right to left, and the return value is stored in the eax register. This proposition was supposed to be true. Next we will use a small program to disassemble and observe the execution process:
# Include <stdio. h> int add (int x, int y) {return x + y;} int main () {int eax = 0; int z = 0; int x = 6; int y = 5; z = add (x, y) ;__ asm _ ("movl % eax, % 0": "+ B" (eax ): "m" (x); printf ("z is % d \ n", z); printf ("eax is % d \ n", eax); return 0 ;}
The Code explains that movl % eax and % 0 in asm code mean to assign the value of register eax to the eax variable of our program. But why is the execution result:
Z is 11
Eax is 0
In theory, it should be the result of adding x and y. Disassemble this exe program:
The above is the main function.
So here I will explain how to press stack y first and then stack x, which is indeed from right to left..
Next, let's look at the add call:
Take the value of y and then the value of x, and then save the result in eax. Then return to the main function.
After calling add, the eax value is assigned to z,This indicates that the return value of the function is indeed saved in eax.. But why is the printed eax 0.
Next, let's look at it,
First, the value of the eax variable in the program is assigned to the eax register, which is of course 0. So now I have a deep understanding of the C language Embedded Assembly execution process. Even if "+ B" is specified to the ebx register, the compiler will first assign the variable value to the eax register, then assign the value to ebx, And the return principle is the same, for example: