תÌû£ºarm»ã±àÓïÑÔµ÷ÓÃCº¯ÊýÖ®²ÎÊý´«µÝ
ÍøÉÏ¿´µ½µÄ£¬¸Ð¾õ²»´í¾Í¸´ÖÆÏÂÀ´×÷Ϊ²Î¿¼ÁË¡£
¸ÐлÔ×÷Õß¡£
arm»ã±àÓïÑÔµ÷ÓÃCº¯ÊýÖ®²ÎÊý´«µÝ
¶ÔÓÚARMÌåϵÀ´Ëµ£¬²»Í¬ÓïÑÔ׫дµÄº¯ÊýÖ®¼äÏ໥µ÷Óã¨mix calls£©×ñѵÄÊÇ ATPCS£¨ARM-Thumb Procedure Call Standard£©£¬ATPCSÖ÷ÒªÊǶ¨ÒåÁ˺¯Êýºô½Ðʱ²ÎÊýµÄ´«µÝ¹æÔòÒÔ¼°ÈçºÎ´Óº¯Êý·µ»Ø£¬¹ØÓÚATPCSµÄÏêϸÄÚÈÝ¿ÉÒԲ鿴ADS1.2 Online Books ----Developer GuideµÄ2.1½Ú¡£ÕâƪÎĵµÒª½²µÄÊÇ»ã±à´úÂëÖжÔCº¯Êýµ÷ÓÃʱÈçºÎ½øÐвÎÊýµÄ´«µÝÒÔ¼°ÈçºÎ´ÓCº¯ÊýÕýÈ··µ»Ø¡£
²»Í¬ÓÚx86µÄ²ÎÊý´«µÝ¹æÔò£¬ATPCS½¨Ò麯ÊýµÄÐββ»³¬¹ý4¸ö£¬Èç¹ûÐβθöÊýÉÙÓÚ»òµÈÓÚ4£¬ÔòÐβÎÓÉR0,R1,R2,R3Ëĸö¼Ä´æÆ÷½øÐд«µÝ£»ÈôÐβθöÊý´óÓÚ4£¬´óÓÚ4µÄ²¿·Ö±ØÐëͨ¹ý¶ÑÕ»½øÐд«µÝ¡£
ÎÒÃÇÏÈÌÖÂÛÒ»ÏÂÐβθöÊýΪ4µÄÇé¿ö.
ʵÀý1£º
test_asm_args.asm
//--------------------------------------------------------------------------------
IMPORT test_c_args ;ÉùÃ÷test_c_argsº¯Êý
AREA TEST_ASM, CODE, READONLY
EXPORT test_asm_args
test_asm_args
STR lr, [sp, #-4]! ;±£´æµ±Ç°lr
ldr r0,=0x10 ;²ÎÊý 1
ldr r1,=0x20 ;²ÎÊý 2
ldr r2,=0x30 ;²ÎÊý 3
ldr r3,=0x40 ;²ÎÊý 4
bl test_c_args ;µ÷ÓÃCº¯Êý
LDR pc, [sp], #4 ;½«lr×°½øpc(·µ»Ømainº¯Êý)
END
test_c_args.c
//--------------------------------------------------------------------------------
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);
}
main.c
//--------------------------------------------------------------------------------
int main()
{
test_asm_args();
for(;;);
}
³ÌÐò´Ómainº¯Êý¿ªÊ¼Ö´ÐÐ,mainµ÷ÓÃÁËtest_asm_args,test_asm_argsµ÷ÓÃÁËtest_c_args,×îºó´Ótest_asm_args·µ»Ømain¡£´úÂë·Ö±ðʹÓÃÁË»ã±àºÍC¶¨ÒåÁËÁ½¸öº¯Êý,test_asm_args ºÍ test_c_args,test_asm_argsµ÷ÓÃÁËtest_c_args,Æä²ÎÊýµÄ´«µÝ·½Ê½¾ÍÊÇÏòR0~R3·Ö±ðдÈë²ÎÊýÖµ£¬Ö®ºóʹÓÃblÓï¾ä¶Ôtest_c_args½øÐе÷Óá£ÆäÖÐÖµµÃ×¢ÒâµÄµØ·½ÊÇÓúìÉ«±ê¼ÇµÄÓï¾ä£¬test_asm_argsÔÚµ÷ÓÃtest_c_args֮ǰ±ØÐë°Ñµ±Ç°µÄ lrÈëÕ»£¬µ÷ÓÃÍêtest_c_argsÖ®ºóÔٰѸղű£´æÔÚÕ»ÖеÄlrд»Øpc,ÕâÑù²ÅÄÜ·µ»Øµ½mainº¯ÊýÖС£
Èç¹ûtest_c_argsµÄ²ÎÊýÊÇ8¸öÄØ£¿ÕâÖÖÇé¿ötest_asm_argsÓ¦¸ÃÔõÑù´«µÝ²ÎÊýÄØ£¿
ʵÀý2£º
test_asm_args.asm
//--------------------------------------------------------------------------------
IMPORT test_c_args ;ÉùÃ÷test_c_argsº¯Êý
AREA TEST_ASM, CODE, READONLY
EXPORT test_asm_args
test_asm_args
STR lr, [sp, #-4]! ;±£´æµ±Ç°lr
ldr r0,=0x1 ;²ÎÊý 1
ldr r1,=0x2 ;²ÎÊý 2
ldr r2,=0x3 ;²ÎÊý 3
ldr r3,=0x4 ;²ÎÊý 4
ldr r4,=0x8
str r4,[sp,#-4]! ;²ÎÊý 8 ÈëÕ»
ldr r4,=0x7
str r4,[sp,#-4]! ;²ÎÊý 7 ÈëÕ»
ldr r4,=0x6
str r4,[sp,#-4]! ;²ÎÊý 6 ÈëÕ»
ldr r4,=0x5
str r4,[sp,#-4]! ;²ÎÊý 5 ÈëÕ»
bl test_c_args_lots
ADD sp, sp, #4 ;Çå³ýÕ»ÖвÎÊý 5,±¾Óï¾äÖ´ÐÐÍêºóspÖ¸Ïò ²ÎÊý6
ADD sp, sp, #4 ;Çå³ýÕ»ÖвÎÊý 6,±¾Óï¾äÖ´ÐÐÍêºóspÖ¸Ïò ²ÎÊý7
ADD sp, sp, #4 ;Çå³ýÕ»ÖвÎÊý 7,±¾Óï¾äÖ´ÐÐÍêºóspÖ¸Ïò ²ÎÊý8
ADD sp, sp, #4 ;Çå³ýÕ»ÖвÎÊý 8,±¾Óï¾äÖ´ÐÐÍêºóspÖ¸Ïò lr
LDR pc, [sp],#4 ;½«lr×°½øpc(·µ»Ømainº¯Êý)
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(;;);
}
Õⲿ·ÖµÄ´úÂëºÍʵÀý1µÄ´úÂë´ó²¿·ÖÊÇÏàͬµÄ£¬²»Í¬µÄµØ·½ÊÇtest_c_argsµÄ²ÎÊý¸öÊýºÍtest_asm_argsµÄ²ÎÊý´«µÝ·½Ê½¡£
ÔÚtest_asm_argsÖУ¬²ÎÊý1¡«²ÎÊý4»¹ÊÇͨ¹ýR0~R3½øÐд«µÝ£¬¶ø²ÎÊý5¡«²ÎÊý8ÔòÊÇͨ¹ý°ÑÆäѹÈë¶ÑÕ»µÄ·½Ê½½øÐд«µÝ£¬²»¹ýҪעÒâÕâËĸöÈëÕ»²ÎÊýµÄÈëջ˳Ðò£¬ÊÇÒÔ²ÎÊý8->²ÎÊý7->²ÎÊý6->²ÎÊý5µÄ˳ÐòÈëÕ»µÄ¡£
Ö±µ½µ÷ÓÃtest_c_args֮ǰ£¬¶ÑÕ»ÄÚÈÝÈçÏ£º
sp->+----------+
| ²ÎÊý5 |
+----------+
| ²ÎÊý6 |
+----------+
| ²ÎÊý7 |
+----------+
| ²ÎÊý8 |
+----------+
| lr |
+----------+
test_c_argsÖ´Ðзµ»Øºó£¬ÔòÉèÖÃsp,¶Ô֮ǰÈëÕ»µÄ²ÎÊý½øÐÐÇå³ý£¬×îºó½«lr×°Èëpc·µ»Ømainº¯Êý£¬ÔÚÖ´ÐÐ LDR pc, [sp],#4 Ö¸Áî֮ǰ¶ÑÕ»ÄÚÈÝÈçÏ£º
+----------+
| ²ÎÊý5 |
+----------+
| ²ÎÊý6 |
+----------+
| ²ÎÊý7 |
+----------+
| ²ÎÊý8 |
sp->+----------+
| lr |
+----------+