Salta al contenuto
Note per Studenti Instruction Formats and Addressing Modes Report

Instruction Formats and Addressing Modes

In questa pagina 5

Addressing modes

Definition (addressing modes). The effective address (EA) is the memory address of the operand. Notation: AA = address field of the instruction, RR = named register, (X)(X) = content of XX.

Mode Operand Pro Con
immediate AA itself no memory access value limited by field width
direct (A)(A), EA = AA simple address space limited by the field
indirect ((A))((A)), EA = (A)(A) large address space two memory accesses
register (R)(R) fast, few bits few registers
register indirect ((R))((R)), EA = (R)(R) large space, one access register must be loaded first
displacement (base + offset) EA = (R)+A(R)+A flexible extra addition
PC-relative EA = (PC)+A(\text{PC})+A relocatable code, short jumps limited reach
scaled indexed EA = (Rb)+(Ri)⋅s+A(R_b)+(R_i)\cdot s+A arrays with ss-byte elements more complex
stack operand on top of the stack, implicit short instructions only the top is accessible

Example: memory M[200]=500M[200]=500, M[500]=800M[500]=800, M[700]=900M[700]=900, register R1=500R1=500; instruction with A=200A=200.

Mode Operand value
immediate 200
direct M[200]=500M[200]=500
indirect M[M[200]]=M[500]=800M[M[200]]=M[500]=800
register (R1R1) 500
register indirect (R1R1) M[500]=800M[500]=800
displacement (R1+200R1+200) M[700]=900M[700]=900

Special cases of displacement: offset 0 is register indirect; a base register that is always 0 ($0 in MIPS) gives absolute addressing.

Formula (scaled index). The factor s∈{1,2,4,8}s\in\{1,2,4,8\} steps uniformly through arrays of bytes, halfwords, words, doublewords. In ARM LDR r0, [r1, r2, LSL #2] reads element r2 of a word array starting at r1.

Example. With r1=0x2000, r2=3: the word at 0x2000+3⋅4=0x200C\mathrm{0x2000}+3\cdot4=\mathrm{0x200C} is read.

Format design

Trade-offs: instruction length (longer means more operations, operands and modes, but more memory and bandwidth), number of opcodes, number of registers, width of immediates and displacements.

Definition (fixed and variable format).

  • Fixed format (RISC: all instructions 32 bits): simple and fast decoding, suited to pipelining.
  • Variable format (x86: 1 to 15 bytes): compact code, complex decoding.

Example. ARM and MIPS use fixed 3232-bit instructions (one every 44 bytes), x86 from 11 to 1515 bytes.

MIPS formats (32 bits)

Definition (MIPS R, I and J formats).

Format Fields (bits) Use
R op (6) · rs (5) · rt (5) · rd (5) · shamt (5) · funct (6) register operations: add $1, $2, $3
I op (6) · rs (5) · rt (5) · immediate (16) immediates, load/store, conditional branches: lw $8, 0x1200($9)
J op (6) · address (26) unconditional jump

In format R: 6+5+5+5+5+6=326+5+5+5+5+6=32 bits; a register takes 55 bits because there are 25=322^5=32 registers. In format J the address is a word address: it is multiplied by 4 (shift left by 2) because instructions are 4-byte aligned. In format I the 16-bit immediate is sign-extended to 32 bits.

ARM format (32 bits, data-processing instructions)

Definition (ARM data-processing format).

cond (4) 00 I (1) opcode (4) S (1) Rn (4) Rd (4) operand 2 (12)
  • cond: execution condition (all 32-bit ARM instructions can be conditional).
  • S: if 1 the instruction updates the flags.
  • Rn, Rd: 4 bits because there are 16 registers.
  • operand 2: a register, possibly shifted, or (I = 1) an 8-bit constant rotated right by an even number of positions (4 rotation bits ×\times 2). Hence MOV r0, #0xFF000000 is encodable (0xFF rotated) but MOV r0, #0x101 is not.

Example. MOV r0, #0xFF000000: 0xFF rotated right by 88 positions (rotation field 44, 4⋅2=84\cdot2=8).

Common mistakes

Versione ripasso

Addressing modes

EA = effective address; AA = address field, (X)(X) = content of XX.

Mode Operand / EA Note
immediate AA no access; limited range
direct EA = AA limited space
indirect EA = (A)(A) 2 accesses
register (R)(R) fast
register indirect EA = (R)(R) 1 access
displacement EA = (R)+A(R)+A flexible
PC-relative EA = (PC)+A(\text{PC})+A relocatable, short jumps
scaled indexed (Rb)+(Ri)s+A(R_b)+(R_i)s+A arrays, s∈{1,2,4,8}s\in\{1,2,4,8\}
stack implicit top short instructions

Example (M[200]=500M[200]=500, M[500]=800M[500]=800, M[700]=900M[700]=900, R1=500R1=500, A=200A=200): immediate 200, direct 500, indirect 800, register 500, register indirect 800, displacement M[700]=900M[700]=900. Offset 0 = register indirect; base always 0 = absolute. LDR r0,[r1,r2,LSL #2] with r1=0x2000, r2=3: address 0x200C.

Formats

  • Fixed (RISC, 32 bits): simple decoding, pipeline-friendly. Variable (x86, 1-15 bytes): compact, complex decoding.
  • MIPS R: op 6, rs 5, rt 5, rd 5, shamt 5, funct 6. I: op 6, rs 5, rt 5, imm 16 (sign-extended). J: op 6, address 26 (word address, times 4).
  • ARM data processing: cond 4, 00, I 1, opcode 4, S 1, Rn 4, Rd 4, operand 2 (12). Operand 2: shifted register, or 8-bit constant rotated right by an even amount: MOV r0,#0xFF000000 ok, #0x101 not. S = update flags; cond = conditional execution.

Instruction cycle with indirect addressing: Fetch-Execute Cycle and DatapathThe registers PC, IR, MAR and MBR, the fetch and execute phases and the four kinds of action, the full instruction-cycle state diagram with interrupt check, a worked accumulator-machine example, and the datapath of a simple RISC core with single-cycle and multicycle timing.Fetch-Execute Cycle and Datapath →; ISA overview: Instruction Set ArchitectureWhat an instruction set defines, the elements of an instruction, operand and operation types, 0-, 1-, 2- and 3-address machines on the same calculation, load/store architectures, and the RISC and CISC design philosophies with register windows and register allocation by graph coloring.Instruction Set Architecture →.

Common mistakes: direct = address, immediate = value; sign-extend negative displacements; branch target relative to PC + 4.

Teoria collegata