The use of = sign in ARM assembly

Load constants

All AArch64 instructions are 32-bits long…

ARM instruction 只有 32 bits 長,需要借助 pseudo instruction: adr, ldr 的輔助,例如載入常數

1
2
ldr rd, =constant # rd=32-bit constant
adr rd, label # rd=32-bit relative address

ldr 會依據情況轉換成 mov, mvn, ldr [pc, #offset] 等指令,= 用意是從 literal pool 找出該常數,若沒有就放入 literal pool。

  • Source code

    1
    2
    3
    ldr     r0, =0xff
    ldr r1, =0xffffffff
    ldr r2, =0xf00000ff
  • Disassembly

    1
    2
    3
    mov     r0, #0xff
    mvn r1, #0x00
    ldr r2, [pc, #offset]

adrentry.S 用意是將 sys_call_table 地址載入到 stbl,再來藉由 ldr x16, [stbl, scno, lsl #3] 載入 syscall 地址。

ADR accepts a PC-relative expression, that is, a label with an optional offset where the address of the label is relative to the PC.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
sc_nr	.req	x25					// number of system calls
scno .req x26 // syscall number
stbl .req x27 // syscall table pointer

el0_svc:
adr stbl, sys_call_table // load syscall table pointer
uxtw scno, w8 // syscall number in w8
mov sc_nr, #__NR_syscalls
bl enable_irq
cmp scno, sc_nr // check upper syscall limit
b.hs ni_sys

ldr x16, [stbl, scno, lsl #3] // address in the syscall table
blr x16 // call sys_* routine
b ret_from_syscall
ni_sys:
handle_invalid_entry 0, SYSCALL_ERROR
ret_from_syscall:

Reference

Addressing modes

The address for a load or store is formed from two parts: a value from a base register, and an offset.

The offset takes one of three formats:

  • Immediate
  • Register
  • Scaled register

The offset and base register can be used in three different ways to form the memory address.

  • Offset
  • Pre-indexed
  • Post-indexed

直接看上課簡報認識指令