和C調用匯編一致,前四個參數用R0-R3傳遞,后面的用堆棧傳遞 測試這個程序:
//******************************************************
//main.C extern int test(int, int, int);
int n; int main(void)
{
n = test(2,4,6);
while(1);
} int add(int a, int b, int c)
{
return a+b+c;
}
//******************************************************
;****************************************************
;test.S
IMPORT add ;引進add
EXPORT test ;供出test
AREA test,CODE,READONLY
CODE32
STR LR,[SP],#-4 ;保存LR:入棧
BL add ;調用C程序
LDR LR,[SP,#4]! ;LR出棧
MOV PC,LR ;返回main END
;*************************************************** 過程說明:main調用n = test(2,4,6),使2、4、6分別通過R0、R1、R2傳遞給匯編函數test,然后test調用C程序add,R0、R1、R2分別傳給a、b、c,然后求和結果用R0返回test,test又將R0返回main函數,所以最后 n = 12; 如圖:

注意匯編程序中IMPORT和EXPORT的用法:
IMPORT add:進口,指add在外部文件中,當前文件要調用
EXPORT test:出口,指test在當前文件中,可以給外部文件調用 同樣在C文件中,如果要調用外部文件,使用extern關鍵字申明函數,如:extern int test(int, int, int); 這些關鍵字是必須的,如果在沒有使用這些關鍵字的情況下,調用外部函數,編譯器要報錯的。
|