ARM LEGv8

  • A subset of ARM
  • RISC Architecture

Design Principles

  • Simplicity favors regularity
    • Application: All arithmetic operations have the form OUT, IN, IN
  • Smaller is faster
    • Application: Registers being preferred for arithmetic
  • Make the common case fast
    • Small constants are common Immediate operands avoid a load instruction
  • Good Design demands good compromises
    • Different formats complicate decoding, but allow all instructions to be 32-bits
    • Keeps formats as similar as possible

Components

Examples

If Statement

if (i==j)
    f = g+h; 
else  
	f = g-h;
	  SUB X9,X22,X23
      CBNZ X9,Else
      ADD X19,X20,X21
      B Exit 
Else: SUB X9,X22,x23
Exit: ...

If (the other way)

if (a > b)
	a = a + 1;
	SUBS X9,X22,X23 // use subtract to make comparison
	B.LTE Exit // conditional branch  
	ADDI X22,X22,#1 // a = a+1
Exit:
	CMP X22, X23 
	B.GT Exit 
	ADDI X22, X22, #1 // a = a+1
Exit:

Loops

while (save[i] == k)
	i += 1;
 Loop:  LSL X10,X22,#3
        ADD    X10,X10,X25
        LDUR   X9,[X10,#0] 
 
        SUB    X11,X9,X24
        CBNZ   X11,Exit 
 
        ADDI   X22,X22,#1
        B      Loop 
 
Exit: ...

Basic Blocks

  • A sequence of instructions with
    • No embedded branches (except at end)
    • No branch targets (except at beginning)
  • Means either the entire block or none is executed
  • Compilers and processors do optimizations on basic blocks

Program Pointers

  • Program Counter (PC)
    • Points to current instruction being run
    • In intervals of 4 because each instruction is 4 bytes
  • Frame Pointer
    • Points to base address of function’s frame
    • Allows you to easily move stack pointer back to where it was before a procedure was called
  • Stack Pointer
    • Points to top of stack
    • Grows from the top down in most systems
    • pushing subtracts 4 from the address

Program Memory Layout

  • Structure
    • Stack 🔽 (Stack pointer to here)
    • (Space)
    • Dynamic Data 🔼
    • Static Data
    • Text (Program counter to here)
    • Reserved
      • Bookkeeping operations by OS
  • That’s actually an abstraction because the OS could give us non contiguous memory and then tell us that it’s contiguous
    • OSs do defragmentation to reduce that because contiguous memory is much faster

Stack

  • Stack pointer must be manually moved in assembly
SUBI SP,SP,#24    // move the stack pointer up 24 
STUR X10,[SP,#16] // store at botom of stack
STUR X9,[SP,#8]   // store at middle of stack
STUR X19,[SP,#0]  // store at top of stack
ADD X9,X0,X1
ADD X10,X2,X3
SUB X19,X9,X10
ADD X0,X19,XZR
LDUR X10,[SP,#16] // get from bottom of stack
LDUR X9,[SP,#8]   // get from middle of stack
LDUR X19,[SP,#0]  // get from top of stack
ADDI SP,SP,#24    // move the stack pointer down 24
BR LR