4.6 Assembly language interworking example

The BX instruction can carry out an instruction set change. When changing to Thumb state, you must ensure that the least significant bit in the address is set.

This example implements a short header section (SECTION1) followed by an ADR instruction to get the address of the label THUMBProg, and sets the least significant bit of the address. The BX instruction changes the state to Thumb state.
In SECTION2, the Thumb code adds the contents of two registers together, using an ADR instruction to get the address of the label ARMProg, leaving the least significant bit clear. The BX instruction changes the state back to ARM state.
In SECTION3 the ARM code adds together the contents of two registers and ends.

Assembly language interworking

     ; ********
     ; addreg.s
     ; ********
     PRESERVE8
     AREA     AddReg,CODE,READONLY  ; Name this block of code.
     ENTRY                          ; Mark first instruction to call.
; SECTION1
start
     ADR R0, ThumbProg:OR:1         ; Generate branch target address
                                    ; and set bit 0, hence arrive
                                    ; at target in Thumb state.
     BX  R0                         ; Branch exchange to ThumbProg.
; SECTION2
     THUMB                          ; Subsequent instructions are Thumb
ThumbProg
     MOVS R2, #2                    ; Load R2 with value 2.
     MOVS R3, #3                    ; Load R3 with value 3.
     ADDS R2, R2, R3                ; R2 = R2 + R3
     ADR R0, ARMProg
     BX  R0                         ; Branch exchange to ARMProg.
; SECTION3
     ARM                            ; Subsequent instructions are ARM
ARMProg
     MOV R4, #4
     MOV R5, #5
     ADD R4, R4, R5
; SECTION 4
stop MOV R0, #0x18                  ; angel_SWIreason_ReportException
     LDR R1, =0x20026               ; ADP_Stopped_ApplicationExit
     SVC 0x123456                   ; ARM semihosting
     END                            ; Mark end of this file.

Building the example

Follow these steps to build and link the modules:
  1. To assemble the source file for interworking, type:
    armasm --debug --apcs=/interwork addreg.s
    
  2. To link the object files, type:
    armlink addreg.o -o addreg.axf
    
    Alternatively, to view the size of the interworking veneers, type:
    armlink addreg.o -o addreg.axf --info=veneers
    
  3. Run the image using a compatible debugger with an appropriate debug target.
Related concepts
4.7 Interworking using veneers
4.8 C and C++ language interworking
4.9 C, C++, and assembly language interworking using veneers
Related information
--apcs=qualifier…qualifier assembler option
--debug assembler option
--info=topic[,topic,...] linker option
--output=filename linker option
Non-ConfidentialPDF file icon PDF versionARM DUI0471K
Copyright © 2010-2014 ARM. All rights reserved.