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: = address field of the instruction, = named register, = content of .
Mode Operand Pro Con immediate itself no memory access value limited by field width direct , EA = simple address space limited by the field indirect , EA = large address space two memory accesses register fast, few bits few registers register indirect , EA = large space, one access register must be loaded first displacement (base + offset) EA = flexible extra addition PC-relative EA = relocatable code, short jumps limited reach scaled indexed EA = arrays with -byte elements more complex stack operand on top of the stack, implicit short instructions only the top is accessible
Example: memory , , , register ; instruction with .
| Mode | Operand value |
|---|---|
| immediate | 200 |
| direct | |
| indirect | |
| register () | 500 |
| register indirect () | |
| displacement () |
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 steps uniformly through arrays of bytes, halfwords, words, doublewords. In ARM
LDR r0, [r1, r2, LSL #2]reads elementr2of a word array starting atr1.
Example. With r1=0x2000, r2=3: the word at 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 -bit instructions (one every bytes), x86 from to 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, $3I 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: bits; a register takes bits because there are 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 2). Hence
MOV r0, #0xFF000000is encodable (0xFF rotated) butMOV r0, #0x101is not.
Example. MOV r0, #0xFF000000: 0xFF rotated right by positions (rotation field , ).
Common mistakes
- Confusing direct and immediate addressing: in direct the field is an address, in immediate it is the value.
- Forgetting sign extension of the immediate in negative displacements.
- Forgetting that a branch target is relative to PC + 4 (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 →).
Versione ripasso
Addressing modes
EA = effective address; = address field, = content of .
| Mode | Operand / EA | Note |
|---|---|---|
| immediate | no access; limited range | |
| direct | EA = | limited space |
| indirect | EA = | 2 accesses |
| register | fast | |
| register indirect | EA = | 1 access |
| displacement | EA = | flexible |
| PC-relative | EA = | relocatable, short jumps |
| scaled indexed | arrays, | |
| stack | implicit top | short instructions |
Example (, , , , ): immediate 200, direct 500, indirect 800, register 500, register indirect 800, displacement . 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,#0xFF000000ok,#0x101not. 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.