Keil Logo Arm Logo

CARM User's Guide

Discontinued

ARM Code Example

LOC      OBJ     LINE   SOURCE
                    1
                    2              NAME    my_arm_modul
                    3
                    4   ;  /***********************************/
                    5   ;  /* Sample Assembler to C Interface */
                    6   ;  /***********************************/
                    7   ;
                    8   ;  extern int func1 (int v1, int *p);
                    9   ;
                   10   ;  extern int val;
                   11   ;
                   12   ;  int func2 (int *x1, int *x2)  {
                   13   ;    int y;
                   14   ;
                   15   ;    y = func1 (x1, *x2);
                   16   ;    val += y;
                   17   ;    return (y);
                   18   ;  }
                   19
                   20   EXTERN CODE32 (func1?A)
                   21   EXTERN DATA   (val)
                   22
                   23   PUBLIC func2?A
                   24   PUBLIC func2?T
                   25
00000000           26   AREA ?PR?func2, CODE
                   27   ; veneer code for Thumb entry
                   28   func2?T    PROC    CODE16       ; entry point for ARM mode call
00000000 4778      29              BX      R15          ; switch to ARM mode
00000002 46C0      30              NOP
                   31              ENDP
                   32
                   33   func2?A    PROC    CODE32       ; entry point for ARM mode call
00000004 E92D4000  34              STMDB   R13!,{LR}    ; save return address to stack
                   35   ;---- Variable 'x1' passed in Register 'R0'
                   36   ;---- Variable 'x2' passed in Register 'R1'
                   37   ;   y = func1 (*x1, x2);
00000008 E5900000  38              LDR     R0,[R0,#0x0] ; load content of x1
                   39   ;
                   40   ;---- Variable 'v' passed in Register 'R0'
                   41   ;---- Variable 'p' passed in Register 'R1'
0000000C EB000000  42              BL      func1?A      ; function call
                   43   ;---- The function value of 'func1' is returned in Register 'R0'
                   44   ;---- temporary Variable 'y' is saved in Register 'R0'
                   45   ;   val += y;
00000010 E59F1000  46              LDR      R1,=val      ; address of val
00000014 E5912000  47              LDR      R2,[R1,#0x0] ; load val
00000018 E0822000  48              ADD      R2,R2,R0     ; add y to val
0000001C E5812000  49              STR      R2,[R1,#0x0] ; store val
                   50   ;   return (y);
00000020 E8BD0008  51              LDMIA    R13!,{R3}
00000024 E12FFF13  52              BX       R3
                   53
                   54              ENDP
                   55
                   56              END

Note

  • The example above includes ARM/Thumb interworking code. func2 may be called from ARM or Thumb mode code.
  • If func2 is only called from ARM mode code, you may omit the veneer code for Thumb entry and you may use the instruction LDMIA R13!,{R15} instead of the LDMIA R13!,{R3}; BX R3 combination to return from the function.

arm-logo-small

Keil logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.