Keil™, An ARM® Company

CARM User's Guide

Discontinued

Thumb Code Example

LOC      OBJ       LINE   SOURCE

                      1
                      2              NAME    my_thumb_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 CODE16 (func1?T)
                     21   EXTERN DATA   (val)
                     22
                     23   PUBLIC func2?A
                     24   PUBLIC func2?T
                     25
00000000             26   AREA ?PR?func2, CODE
                     27   ; veneer code for ARM entry
                     28   func2?A    PROC    CODE32       ; entry point for ARM mode call
00000000 E59FC000    29              LDR     R12,[R15]    ; load Thumb entry
00000004 E12FFF1C    30              BX      R12          ; switch to Thumb mode
00000008 0000000C R  31              DD      func2?T
                     32              ENDP
                     33
                     34   ; entry point for Thumb mode call
                     35   func2?T    PROC    CODE16
0000000C B500        36              PUSH    {LR}         ; save return address to stack
                     37   ;---- Variable 'x1' passed in Register 'R0'
                     38   ;---- Variable 'x2' passed in Register 'R1'
                     39   ;   y = func1 (*x1, x2);
0000000E 6800        40              LDR     R0,[R0,#0x0] ; load content of x1
                     41   ;
                     42   ;---- Variable 'v' passed in Register 'R0'
                     43   ;---- Variable 'p' passed in Register 'R1'
00000010 F000        44              BL      func1?T      ; function call
00000012 F800
                     45   ;---- The function value of 'func1' is returned in Register 'R0'
                     46   ;---- temporary Variable 'y' is saved in Register 'R0'
                     47   ;   val += y;
00000014 4901        48              LDR      R1,=val      ; address of val
00000016 680A        49              LDR      R2,[R1,#0x0] ; load val
00000018 1812        50              ADD      R2,R0        ; add y to val
0000001A 600A        51              STR      R2,[R1,#0x0] ; store val
                     52   ;   return (y);
0000001C BC08        53              POP      {R3}
0000001E 4718        54              BX       R3
                     55
                     56              ENDP
                     57
                     58              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 Thumb mode code, you may omit the veneer code for ARM entry and you may use POP {PC} instead of the POP {R3}; BX R3 combination to return from the function.