armarmv7armv8

Interpretation of svc.n #imm instruction in ARM


I am not able to figure out what does different imm value after svc mean. e.g.

svc.n #c6
svc.n #ac

Can anyone please help?


Solution

  • The immediate does nothing at all. It's just encoded into the instruction, so if you want it you can get it.

    So, if you only have a single SVC handler, the immediate is arbitrary. If you have multiple handlers and need to invoke the right one, you need to find the instruction that generated the SVC IRQ and dismantle it to find the immediate.

    For example, on ARMv7m:

    SVC_Handler
        ; Link register contains the 'exit handler mode' code
        ; Bit 2 tells whether the MSP or PSP was in use
        TST     lr, #4
        MRSEQ   r0, MSP
        MRSNE   r0, PSP
        ; r0 now contains the SP that was in use
        ; Return address is on the stack: load it into r1
        LDR     r1, [r0, #24]
        ; Use the return address to find the SVC instruction
        ; SVC instruction contains an 8-bit code
        LDRB    r1, [r1, #-2]
        ; Immediate from the SVC instruction is now in r1 to do whatever with
    

    You could then use the immediate to index into a jump table, for example.