General Apple Silicon/ARM64 Assembly Notes #1376
Replies: 3 comments
-
Linux/General ARM64 Assembly NotesMachine RegistersGeneral-purpose Registers
Frame PointerTODO: While this message is about x86_64 assembly, it gives a good explanation on what a frame pointer is.
Link Register
SIMD and Floating-Point registers (NEON)AArch64 has 32 x 128-bit NEON registers (V0-V31). These registers can also be viewed as 32-bit Sn registers or 64-bit Dn registers. Register Alignment
Condition Flag
Condition Codes
Exception Levels
Loads and Stores Addressing
; x1 stays the same
; w0 = x1[0]
ldr w0, [x1] flowchart TD
A[x1] -->|Memory| B(w0)
; x1 stays the same
; w0 = x1[12]
ldr w0, [x1, #12] flowchart TD
A[+] -->|Memory| B
B[w0]
C[x1] --> A
D[#12] --> A
; x1 updated to x1 + 12
; w0 = x1[12]
ldr w0, [x1, #12]! flowchart TD
A[+] --->|Memory| B
A --> E
B[w0]
C[x1] --> A
D[#12] --> A
E["x1 (updated)"]
; x1 updated to x1 + 12
; w0 = x1[0]
ldr w0, [x1], #12 flowchart TD
A[x1] ---->|Memory| B
A --> D
B[w0]
C[#12] --> D
D[+] --> E
E["x1 (updated)"]
Calling Convention; Preserve the frame pointer and link register
; Keep in mind that instructions (like `bl`), can overwrite the link register. So it's
; important to preserve this register. Otherwise, you may run into an infinite loop...
stp fp, lr, [sp, #-16]!
; Move the stack pointer (`sp`) to the frame pointer register
mov fp, sp
; TODO: Add examples for setting functions arguments.
; TODO: Add examples for return arguments.
; Restore the frame pointer and link register
ldp fp, lr, [sp], #16
ret Source
|
Beta Was this translation helpful? Give feedback.
-
MacOS ARM64 Assembly NotesSyscallUnlike Linux, macOS discourages the usage syscalls. This means that applications that rely on syscalls may break in the future, as macOS makes no promises for stability.
[1] The intermediate value does not seem to matter. With that being said, Apple's own software seems to have it set to
High level notes on
High level notes on
Syscall Implementation For DarlingMacros for how to handle more than one return argument/error flag: #define ARM64_SET_SECOND_RETURN_VALUE(value) \
asm volatile ( \
"mov x1, %[return2]\n" :: \
[return2] "r"(value) \
)
// Apple treats the carry flag as the error flag
#define ARM64_SET_ERROR_FLAG \
asm volatile ( \
"mrs x9, nzcv\n" \
"orr x9, x9, 0x20000000\n" \
"msr nzcv, x9" \
)
#define ARM64_CLEAR_ERROR_FLAG \
asm volatile ( \
"mrs x9, nzcv\n" \
"mov x10, 0x20000000\n" \
"bic x9, x9, x10\n" \
"msr nzcv, x9" \
) Variadic FunctionsTODO Address Space SizeTODO https://discord.com/channels/587344761107513532/858049574090702888/1244323294229041164 Sources
|
Beta Was this translation helpful? Give feedback.
-
CFI (Call Frame Information)Not directly related to ARM64 assembly, but useful to know when dealing with debugging/exception handling. TODO: Add more notes about CFI Sources
|
Beta Was this translation helpful? Give feedback.
-
Since I'm not familiar with ARM64 assembly, I figured I would make a public diary about my experiences learning about it. Hopefully this can be helpful for others who are learning/struggling.
Useful Resources
Beta Was this translation helpful? Give feedback.
All reactions