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

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


//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:shell

.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交叉編譯經過數組

 

/--------------------------------------------------------------------------------------------------------------------------------------ide

對 於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(;;);
}oop

   程序從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(;;);
}ui

這部分的代碼和實例1的代碼大部分是相同的,不一樣的地方是test_c_args的參數個數和test_asm_args的參數傳遞方式。
在test_asm_args中,參數1~參數4仍是經過R0~R3進行傳遞,而參數5~參數8則是經過把其壓入堆棧的方式進行傳遞,不過要注意這四個入棧參數的入棧順序,是以參數8->參數7->參數6->參數5的順序入棧的。code

直到調用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   |
       +----------+排序

相關文章
相關標籤/搜索