The function call between arm assembly and C must comply with atpcs. We recommend that the function have no more than four parameters. If the number of parameters is less than or equal to 4, the parameter is composed of r0, R1, R2, r3 registers are passed. If the number of parameters is greater than 4, the parts greater than 4 must be passed through the stack.
R0 is used to store the first parameter of the function, R1 is used to store the second parameter, R2 is used to store the third parameter, and R3 is used to store the fourth parameter. R0 is also used to return the call result of the function. The value in the return value statement in Function C is stored in R0.
The arm stack is full stack, and the SP Pointer Points to the place where valid data is stored. If the new data of the stack is pressed, the SP must be changed first, and then the data is pushed into the SP. Next, we will analyze the content of the blog http://blog.sina.com.cn/s/blog_3e00004650100tsbf.html.
Scenario (1) Number of function parameters <= 4
Test_asm_args.asm
Import test_c_args; declare the test_c_args Function
Area test_asm, code, readonly
Export test_asm_args
Test_asm_args
Str lr, [Sp, #-4]!
; Save the current LR.Stack is full decline stack FD, first adjust SP pointer, then press LR address.
LDR r0, = 0x10; parameter 1
LDR R1, = 0x20; parameter 2
LDR R2, = 0x30; parameter 3
LDR R3, = 0x40; parameter 4
BL test_c_args; call the c Function
Ldr pc, [Sp], #4; load LR into PC (return main function), Pc = LR, SP = SP + 4, and restore the original stack.
End
Void test_c_args (int A, int B, int C, int D)
{
Printk ("test_c_args: \ n ");
Printk ("% 0x % 0x % 0x % 0x \ n", A, B, C, D );
}
Int main ()
{
Test_asm_args ();
For (;;);
}
Scenario 2: Eight function parameters
Test_asm_args.asm
//--------------------------------------------------------------------------------
Import test_c_args; declare the test_c_args Function
Area test_asm, code, readonly
Export test_asm_args
Test_asm_args
Str lr, [Sp, #-4]! ; Save the current lR
LDR r0, = 0x1; parameter 1
LDR R1, = 0x2; parameter 2
LDR R2, = 0x3; parameter 3
LDR R3, = 0x4; parameter 4
LDR R4, = 0x8
STR R4, [Sp, #-4]! ; Parameter 8 into the stack
LDR R4, = 0x7
STR R4, [Sp, #-4]! ; Parameter 7
LDR R4, = 0x6
STR R4, [Sp, #-4]! ; Parameter 6
LDR R4, = 0x5
STR R4, [Sp, #-4]! ; Parameter 5 into the stack
BL test_c_args_lots
Add SP, SP, #4; clear parameter 5 in the stack. After this statement is executed, SP points to parameter 6.
Add SP, SP, #4; clear parameter 6 in the stack. After this statement is executed, SP points to parameter 7.
Add SP, SP, #4; clear parameter 7 in stack. After this statement is executed, SP points to parameter 8.
Add SP, SP, #4; clear parameter 8 in the stack. After this statement is executed, SP points to lR
Ldr pc, [Sp], #4; load LR into PC (return the main function)
End
Test_c_args.c
//--------------------------------------------------------------------------------
Void test_c_args (int A, int B, int C, int D, int e, int F, int g, int H)
{
Printk ("test_c_args_lots: \ n ");
Printk ("% 0x % 0x % 0x % 0x % 0x % 0x % 0x % 0x \ n ",
A, B, c, d, e, f, g, h );
}
Main. c
//--------------------------------------------------------------------------------
Int main ()
{
Test_asm_args ();
For (;;);
}
Arm Assembly and C call problems (General function calls)