The entry for the program is main, which calls the assembly function in main.
The first thing to do is to solve the problem of defining functions
In the C language, to extern a function declaration, and then this function in the assembly is implemented.
In the Assembly, use export to introduce the function name defined in the C language, and then start writing the segment where the function name begins.
An example is a function that adds six numbers.
C Language:
#include <stdio.h>extern int sum (int a,int b,int c,int d,int e,int f); int Main () { int result = SUM (1 , 2 , 3 , 4 , 5 , 6 ); return 0
Assembly Code:
Area example,code,readonly EXPORT sum ENTRY sum add r0,r0,r1 add R2,R2,R3 ADD r0,r0,r2 LDR r4,[sp] ldr r5,[sp,#4] add r4,r4,r5 add r0,r4,r0 BX LR END
In fact, the key problem is the parameter and the return value.
We can see that C calls this function, and C gives it a parameter.
How does a compilation accept these parameters?
4 or less parameters, stored directly in the R0~R3 4 registers inside.
4 later parameters are placed in the stack.
If the function has a return value, then the return value is placed in the R0.
Debug See how the compiler handles it, and we'll take the parameters out.
It's easier to understand.
First,
R0 =6
R1=5
R2=3
R3=4
Then put the value of the R1 on the stack, r13=5, and then r1=2
Then the value of the R0 is placed in the next position of the stack, r13+4=6, and then r0=1.
After that:
R0=1,r1=2,r2=3,r3=4
Inside the stack:
So the time to take the parameters is:
LDR R4,[SP]; r4=5LDR r5,[sp,#4]; r5=6
Put the added result in R0, then BX LR returns
can see:
You can verify that the R0 is the one that holds the return value.
C Language Call Assembly