Instructions
Arithmetic
ADD a, b, c: a = b + cSUB a, b, c: a = b - c- Immediate Operands
- For adding constants (notated as
#NUM) to a register - Allows you to store a small constant inside the instruction to prevent a load operation
- Add an
Iafter the instruction (don’t need this for ARMv8) - Ex:
ADDI X20, X22, #4: X20 = X22 + 4
- For adding constants (notated as
- Treats everything as signed
Logical
AND a, b, c: a = b & cORR a, b, c: a = b | cEOR- Can be used as NOT when one input is entirely 1s
LSL a, b, c: a = b << c- Multiply by
LSR a, b, c: a = b >> r- Divide by
- Immediate operands can be used here in the same way
- Treats everything as unsigned
Transfer
MOV XA, XB: Assigns XB to XAMOV XA, #B: Assigns B to XA
Load/Store
LD_R__ [XA, #0], b: Load fromMemory[b]to register aST_R_ a, [XB, #0]: Store from register a toMemory[b]- First Blank
- Nothing: offset given as multiple of 4
U: Unscaled offset (offset is not given as multiple of 4)X: Atomic
- Second Blank (For Load)
- Nothing: Zero-extend loading style
S: Sign-extend loading style (negative numbers are padded with 1)
- Third Blank
- Nothing: Doubleword (8 bytes)
H: Halfword (4 bytes)B: Byte
Conditional Branch
- Explicit: Checks if register is 0
CBZ r, L1: ifr == 0, go to label L1CBNZ r, L1: ifr != 0, go to label L1
- Implicit: Checks flags set
- Written
B.cond L1: ifcondflag set, go to label L1
- Written
Comparison Flags
- Flags that can be set by
- Using the dedicated
CMP a, binstruction - Adding the S suffix to an arithmetic operation
- Using the dedicated
Flags
| Property | Flag |
|---|---|
| Negative | N |
| Zero | Z |
| Carry | C |
| Overflow | V |
Comparisons (uses boolean operation on the flags):
| Property | Signed | Unsigned |
|---|---|---|
| Equal | EQ | EQ |
| Not Equal | NE | NE |
| Less than | LT | LO |
| Less than or equal | LE | LS |
| Greater than | GT | HI |
| Greater than or equal | GE | HS |
Trick: can be done using a single unsigned comparison because a signed negative number would appear as a massive unsigned positive number
Unconditional Branch
B L: Jump to labelBR X30: Go to address based on value of X30- BR = branch to register
BR LR: Return to callerBL L1: Branch and link to L1- Used for procedure call
- Before jumping, saves the address of the next instruction into X30 (special register)
- Node: every instruction if 4 bytes
- Also saves the current context of X30 to the stack so it can be restored after that procedure returns
- Procedure Call Steps
- Place parameters in registers X0 to X7 (special case if more than 8 parameters)
- Transfer control to procedure
- Acquire storage for procedure
- Preform procedure’s operations
- Place result in register for caller (X0)
- Return to place of call (address in X30)
- Leaf procedure: procedure that does not call any other procedures
- When a procedure calls another procedure, saved registers are pushed to the stack and then restored after the procedure returns
- Saved registers 19-25
- Stack pointer X28
- Frame pointer X29
- Link (return address) X30