Questions about C language function call pressure stack and return value, function return value

Source: Internet
Author: User

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:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.