和C調(diào)用匯編一致,前四個(gè)參數(shù)用R0-R3傳遞,后面的用堆棧傳遞 測試這個(gè)程序:
//******************************************************
//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 ;引進(jìn)add
EXPORT test ;供出test
AREA test,CODE,READONLY
CODE32
STR LR,[SP],#-4 ;保存LR:入棧
BL add ;調(diào)用C程序
LDR LR,[SP,#4]! ;LR出棧
MOV PC,LR ;返回main END
;*************************************************** 過程說明:main調(diào)用n = test(2,4,6),使2、4、6分別通過R0、R1、R2傳遞給匯編函數(shù)test,然后test調(diào)用C程序add,R0、R1、R2分別傳給a、b、c,然后求和結(jié)果用R0返回test,test又將R0返回main函數(shù),所以最后 n = 12; 如圖:

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