3.10 Calls to C from assembly language

You can call a C function from assembly code by using the IMPORT directive.

Defining the function in C

The following example is a C function that is to be called from assembly code:

int g(int a, int b, int c, int d, int e) 
    return a + b + c + d + e;

Assembly language call

To call this C function from assembly code, import the g function symbol using the IMPORT directive:

    ; int f(int i) { return g(i, 2*i, 3*i, 4*i, 5*i); }
    EXPORT f
    AREA f, CODE
    IMPORT g           ; i is in R0
    STR lr, [sp, #-4]! ; preserve lr
    ADD R1, R0, R0     ; compute 2*i (2nd param)
    ADD R2, R1, R0     ; compute 3*i (3rd param)
    ADD R3, R1, R2     ; compute 5*i
    STR R3, [sp, #-4]! ; 5th param on stack
    ADD R3, R1, R1     ; compute 4*i (4th param)
    BLX g              ; branch to C function
    ADD sp, sp, #4     ; remove 5th param
    LDR pc, [sp], #4   ; return
Non-ConfidentialPDF file icon PDF versionARM DUI0471M
Copyright © 2010-2016 ARM Limited or its affiliates. All rights reserved.