8 Instruction Encoding and ISA

8.1 The anatomy of Instruction Encoding

8.1.1 The data processing instruction format

For a CPU to successfully identify and execute each unique assembly instruction, the instruction must be translated (encoded) into a unique binary pattern.

General Data Processing Instruction Format

8.1.2 3 types of instructions

Bit 25 tells us if operand 2 is an immediate value (1) or contains a register and shift paramenters (0).
Bit 4 tells us if it is immediate shift or register shift.
Bit 6-5 specifies the shift type (00: LSL, 01: LSR, 10: ASR, 11: ROR/RRX).

Data Processing with Immediate Shift
XXXCCS Rd, Rs1, Rs2, {shft #}
Data Processing with Register Shift
XXXCCS Rd, Rs1, Rs2, {shft Rshift}
Data Processing with Immediate Value
XXXCCS Rd, Rs1, #immediate (rotated)

8.1.3 Examples - Arithmetic instructions

8.1.4 Examples - MOV instructions

Note that for MOV instructions, the Rs1 is filled with 0000, because MOV instructions inherently only require a destination register and a second operand.

8.1.5 Examples - Comparison instructions

8.2 Branch instructions

Branch Instruction Format; B(L)CC Offset

8.2.1 Examples - Branch instructions

Note that for Branch instructions, the 24-bit offset is a signed integer. The assembler calculates this offset based on the distance to the target label. Bits 27-25 are hardcoded to 101 to identify the branch family.

8.3 Load/Store instructions

8.3.1 LDR/STR{B} instruction format

Load/Store Single Word/Byte (Register Offset)
LDR/STR{B}CC Rs, [Rd, offset pre or post]!
Load/Store Single Word/Byte (Immediate Offset)
LDR/STR{B}CC Rs, [Rd, offset pre or post]!

8.3.2 Examples - LDR/STR{B} instructions

Immediate Offset Example:
LDR R0, [R2, #4]
Immediate Offset Example (with Byte and Write-back):
STRB R0, [R2, #-4]!

8.3.3 LDM/STM instruction format

Load/Store Multiple Instructions
LDM/STM{mode}CC Rs{!}, {register list}
  1. Bits 27-25: Hardcoded to 100 to indicate multiple load/store.

  2. Bits 24-20 (Control flags): P, U, W, L are the same. Bit 22 changed from B to ^.

    1. Bit 22 (^): "don’t care" bit, used for restoring status registers in privileged modes.

  3. Bits 15-0 (Register List): Instead of an offset, the entire bottom 16 bits are mapped as a boolean list. Each bit corresponds directly to one of the 16 ARM registers (e.g., Bit 0 represents R0, Bit 15 represents R15/PC). If a bit is set to 1, that register is included in the memory transfer.

8.3.4 Examples - LDM/STM instructions

Note that for stack operations, the Base Register (Rs) is typically the Stack Pointer (SP or R13, which is 1101 in binary). The register list uses 1 bit per register, where Bit 0 corresponds to R0 and Bit 15 corresponds to R15 (PC).

8.4 Fixed vs Variable Length ISA

8.5 Hardware Constraints

Given only 32 bits per instruction, the game is to allocate the optimal number of bits to each feature.

8.6 Orthogonality

Orthogonality means that different features of the architecture (instructions, registers, and addressing modes) can be combined independently without weird exceptions.
If an ISA is perfectly orthogonal, any instruction can use any register, and any instruction can use any addressing mode.
How ARM acheives orthogonality:

  1. Condition Codes: Every single ARM instruction reserves bits 31-28 for the Condition field. You can make any instruction conditional (ADDEQ, MOVEQ, BEQ, LDREQ).

  2. Operand 2: Any instruction that uses Operand 2 can apply a bit-shift. (i.e. don’t need a separate instruction just to do that).

However, perfect orthogonality is nearly impossible in a 32-bit fixed system. Because different operations require fundamentally different information (a Branch needs a massive memory offset, while an ADD needs three registers), the architecture must break its own rules. This is why the CPU relies on the key Identifier bits (27-25) to switch between entirely different decoding layouts.