5.19 SVC handlers in assembly language

The easiest way to call the handler for the requested SVC number is to use a jump table.

SVC jump table

    AREA SVC_Area, CODE, READONLY
    PRESERVE8
    IMPORT SVCOutOfRange
    IMPORT MaxSVC
    CMP    R0,#MaxSVC          ; Range check
    LDRLS  pc, [pc,R0,LSL #2]
    B      SVCOutOfRange
SVCJumpTable
    DCD    SVCnum0
    DCD    SVCnum1
                               ; DCD for each of other SVC routines
SVCnum0                        ; SVC number 0 code
    B    EndofSVC
SVCnum1                        ; SVC number 1 code
    B    EndofSVC
                               ; Rest of SVC handling code
EndofSVC
                               ; Return execution to top level 
                               ; SVC handler so as to restore
                               ; registers and return to program.
    END

If R0 contains the SVC number, the code in the preceding example can be inserted into the following example, after the BIC instruction.

Top-level SVC handler

    PRESERVE8
    AREA TopLevelSVC, CODE, READONLY ; Name this block of code.
    EXPORT     SVC_Handler
SVC_Handler
    PUSH       {R0-R12,lr}           ; Store registers.
    LDR        R0,[lr,#-4]           ; Calculate address of SVC
                                     ; instruction and load it 
                                     ; into R0.
                                     ; 
    BIC        R0,R0,#0xFF000000     ; Mask off top 8 bits of
                                     ; instruction to give SVC number.
    ;
    ; Use value in R0 to determine which SVC routine to execute.
    ;
    LDM         sp!, {R0-R12,pc}^    ; Restore registers and return.
    END
Non-ConfidentialPDF file icon PDF versionARM DUI0471M
Copyright © 2010-2016 ARM Limited or its affiliates. All rights reserved.