ARM 彙編與C調用的若干問題(一般函數調用情況)

來源:互聯網
上載者:User

標籤:style   blog   http   color   ar   for   strong   資料   sp   

ARM 彙編與C之間的函數調用需要符合ATPCS,建議函數的形參不超過4個,如果形參個數少於或等於4,則形參由R0,R1,R2,R3四個寄存器進行傳遞;若形參個數大於4,大於4的部分必須通過堆棧進行傳遞。

    R0 用來存放函數的第一個參數,R1用來存放第二個參數,R2用來存放第三個參數,R3用來存放第四個參數。其中R0還用來返回函數的調用結果,對應C函數裡面的return value語句中的value 存放在R0中。

    ARM堆棧的是滿棧FULL STACK,SP指標指向的位置是存放有效資料的地方,若壓棧新的資料,必須先改變SP,再向SP裡面壓入資料。下面結合部落格http://blog.sina.com.cn/s/blog_3e5694650100tsbf.html,的內容進行分析。

                             情景(一)函數形參的個數<= 4

                                

 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.棧是滿遞減棧FD,首先調整SP指標,然後壓入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函數) ,PC = LR,SP = SP+4,恢複原來的棧。
                END
 
           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);
           }

           int main()
           {
              test_asm_args();
              for(;;);
           }

                                                                                                   情景二:函數的參數是8個

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

ARM 彙編與C調用的若干問題(一般函數調用情況)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.