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
     ; ********
     AREA     AddReg,CODE,READONLY  ; Name this block of code.
     ENTRY                          ; Mark first instruction to call.
     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.
     THUMB                          ; Subsequent instructions are Thumb
     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.
     ARM                            ; Subsequent instructions are ARM
     MOV R4, #4
     MOV R5, #5
     ADD R4, R4, R5
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.

Non-ConfidentialPDF file icon PDF versionARM DUI0471M
Copyright © 2010-2016 ARM Limited or its affiliates. All rights reserved.