Function assembly code !!!! (The input method of function parameters and the local variable storage method inside the function)

Source: Internet
Author: User
extern "C" void fun(int a, int b) {int x = a;int y = b;int z = x + y;return;}int main() {fun(5, 10);return 0;}
For such a C function call process, we can use the following assembly code to describe:
1. In the 8086cpu, BSP is used to save the stack pointer, and ESP is used to save the newly created Stack pointer.
2. The parameters are stored in EBP + X, while local variables in the function are stored in EBP-X,
3. Before a function is called, the compiler inserts a piece of code before the function is called to import the form parameter to the stack!
Therefore, for the above C code, the generated assembly code should be like the following:
1. Call code:
 push 10     ; 0000000aH
 push 5
 call _fun@8
2. Function Code:
Pushebp; Save the previous address movebp and ESP; put the new stack address into EBP subesp, 12; 0000000ch; int x = A; moveax, dword ptr _ A $ [EBP] movdword PTR _ x $ [EBP], eax; int y = B; movecx, dword ptr _ B $ [EBP] movdword PTR _ y $ [EBP], ECx; int z = x + y; movedx, DWORD PTR _ x $ [EBP] addedx, dword ptr _ y $ [EBP] movdword PTR _ Z $ [EBP], edxmovesp, ebppopebpret0
What is _ x _ y ?? Let's take a look!
_a$ = 8
_b$ = 12
_x$ = -4
_y$ = -8
_z$ = -12
Actually, it's just a few numbers.
So, we can assign values to the X and Y teams.
; Int x =;
MoV eax, dword ptr [EBP + 8]; the reason for adding 8 is that the address where + 4 is used to save the returned value
MoV dword ptr [EBP-4], eax
 ; int y = b;
 mov ecx, DWORD PTR [ebp + 12]
 mov DWORD PTR [ebp - 8], ecx
 ; int z = x + y;
 mov edx, DWORD PTR [ebp - 4]
 add edx, DWORD PTR [ebp - 8]
 mov DWORD PTR [ebp - 12], edx

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.