Instructions

Arithmetic

  • ADD a, b, c: a = b + c
  • SUB 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 I after the instruction (don’t need this for ARMv8)
    • Ex: ADDI X20, X22, #4: X20 = X22 + 4
  • Treats everything as signed

Logical

  • AND a, b, c: a = b & c
  • ORR a, b, c: a = b | c
  • EOR
    • 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 XA
  • MOV XA, #B: Assigns B to XA

Load/Store

  • LD_R__ [XA, #0], b: Load from Memory[b] to register a
  • ST_R_ a, [XB, #0]: Store from register a to Memory[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: if r == 0, go to label L1
    • CBNZ r, L1: if r != 0, go to label L1
  • Implicit: Checks flags set
    • Written B.cond L1: if cond flag set, go to label L1

Comparison Flags

  • Flags that can be set by
    • Using the dedicated CMP a, b instruction
    • Adding the S suffix to an arithmetic operation

Flags

PropertyFlag
NegativeN
ZeroZ
CarryC
OverflowV

Comparisons (uses boolean operation on the flags):

PropertySignedUnsigned
EqualEQEQ
Not EqualNENE
Less thanLTLO
Less than or equalLELS
Greater thanGTHI
Greater than or equalGEHS

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 label
  • BR X30: Go to address based on value of X30
    • BR = branch to register
  • BR LR: Return to caller
  • BL 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
    1. Place parameters in registers X0 to X7 (special case if more than 8 parameters)
    2. Transfer control to procedure
    3. Acquire storage for procedure
    4. Preform procedure’s operations
    5. Place result in register for caller (X0)
    6. 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