C語言函數和彙編函數相互調用)

來源:互聯網
上載者:User

在C程式main函數中,接收使用者輸入任意個整數,然後在main中調用使用ARM彙編編寫的函數(在該函數中完成對這些整數的排序功能),然後再在C程式main函數中輸出這些排好順序的整數。

main.c

#include <stdio.h>
int main()

{
int i=0;
int num=0;
int *array=NULL;
while(num <= 0) //輸入數組中元素的個數

    {
printf("please enter the number of elements:\n");
scanf("%d",&num);
if(num > 0)

        {
break;
}
}
if(NULL == (array = (int *)malloc(num*sizeof(int))))

    {
printf("malloc failed!\n");
exit(-1);
}
printf("please enter the elements:\n");
for(i = 0; i<num; i++) {
printf("\n%d:\t", i);
scanf("%d", array+i);
}
sort(array, num);//調用相應的彙編的函數,注意分析傳參過程
   printf("The Result is:\n");
for(i = 0; i<num; i++) {
printf("%d:\t%d\n", i, *(array+i));
}
return 0;
}

 

下面是相應的Sort.s:

.section .text;聲明為程式碼片段
.globl sort;聲明全域變數
sort:  ;linux下需要加冒號
mov        r2, #0            
mov        r8, r0
mov        r9, r0
loop1:
sub        r1, r1, #1
cmp        r2, r1        
add        r1, r1, #1
    beq        end    
mov        r6, r2
add        r3, r2, #1        
loop2:
cmp        r3, r1
    beq        continue1
mov        r3, r3, lsl #2
add        r8, r8, r3
    ldr        r5, [r8]
mov        r6, r6, lsl #2
add        r9, r9, r6
    ldr        r4, [r9]
cmp        r4, r5
    bgt        exchange
continue2:
sub        r8, r8, r3
mov        r3, r3, lsr #2
sub        r9, r9, r6
mov        r6, r6, lsr #2
add        r3, r3,    #1
    b        loop2
exchange:
str        r4, [r8]
str        r5, [r9]
        b        continue2
continue1:
add        r2, r2, #1
    b        loop1

end:

 

注意:通過APCS傳過來的兩個變數,儲存在r0和r1,分別代表是數組的首地址和元素的個數
使用Arm交叉編譯通過

 

/--------------------------------------------------------------------------------------------------------------------------------------

對於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   |
       +----------+

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.