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