Instruction Set Architecture
In questa pagina 8
In questa pagina 4
Definition (ISA). The instruction set architecture is the boundary between hardware and software: everything a machine-language program can use. Two processors with the same ISA run the same code even if organized differently (CPU Structure and RegistersArchitecture versus organization, the stored-program (von Neumann) machine, the internal components of a CPU (ALU, control unit, register file, internal bus), user-visible and control/status registers, the status word with flags and supervisor bit, and how many registers are worth having.CPU Structure and Registers →).
Example. ADD r1, r2, r3 with r2=5, r3=7 leaves r1=12: an instruction of the ARM ISA, whatever the internal organization.
The ISA fixes: the available operations, data types and sizes, visible registers, Instruction Formats and Addressing ModesAddressing modes (immediate, direct, indirect, register, register indirect, displacement, PC-relative, scaled indexed, stack) with effective address and a numeric example, fixed versus variable instruction formats, the MIPS R, I and J formats and the 32-bit ARM data-processing format with the encodable-constant rule.Instruction Formats and Addressing Modes →, the memory model (byte addressing, byte order) and exception handling.
Elements of an instruction
- Opcode: the operation.
- Source operand references: in a register, in memory, inside the instruction (immediate), in an I/O port.
- Result reference.
- Next instruction: implicit (the following one) or explicit in jumps.
In assembly each instruction is written with a mnemonic: ADD r1, r2, r3, LDR r0, [r1].
Operand types
Addresses (unsigned integers), numbers (two's complement integers, floating point, sometimes BCD; see Binary ArithmeticBinary addition and two's complement subtraction, overflow for unsigned (carry) and signed (sign change) numbers, the N Z C V flags, multiplication by shifts and additions, Booth's algorithm with a worked example, division, and logical, arithmetic and rotate shifts.Binary Arithmetic → and Fixed-Point and Floating-Point NumbersFixed-point numbers as an integer divided by 2 to the F with resolution and a sensor example, floating point in base 2, the IEEE 754 single and double formats, conversions in both directions with worked examples, special values and denormals, range, precision, rounding and floating-point operations.Fixed-Point and Floating-Point Numbers →), characters (ASCII, UTF-8), logical data (single bits). Typical ARM sizes: byte (8 bits), halfword (16), word (32), doubleword (64).
Operation types
| Class | Examples |
|---|---|
| data transfer | load, store, move, push, pop |
| arithmetic | add, sub, mul, div, neg, increment |
| logic | and, or, xor, not, test |
| shift and rotate | logical, arithmetic, rotate |
| conversion | integer to floating point, sign extension |
| I/O | in, out (only with isolated I/O) |
| system control | privileged instructions, operating-system call |
| control transfer | unconditional and conditional jump, procedure call and return |
Number of addresses
Definition (0-, 1-, 2- and 3-address instructions). How many explicit operands an arithmetic instruction has. Example: , variables in memory.
Example. SUB Y, A, B has 3 addresses (destination and two sources); SUB A, B has 2, because the destination coincides with the first operand.
3 addresses (OP dest, src1, src2):
SUB Y, A, B ; Y <- A - B
MUL T, D, E ; T <- D * E
ADD T, T, C ; T <- T + C
DIV Y, Y, T ; Y <- Y / T2 addresses (OP dest, src; the destination is also the first operand):
MOV Y, A ; Y <- A
SUB Y, B ; Y <- Y - B
MOV T, D
MUL T, E ; T <- D * E
ADD T, C
DIV Y, T1 address (implicit accumulator):
LOAD D
MUL E
ADD C
STOR Y ; Y <- D*E + C (temporary)
LOAD A
SUB B
DIV Y
STOR Y0 addresses (stack machine, operands on top of the stack): PUSH A, PUSH B, SUB, PUSH C, PUSH D, PUSH E, MUL, ADD, DIV, POP Y.
Fewer addresses: shorter but more numerous instructions. RISC architectures use 3 register addresses.
Load/store architecture
Definition (load/store architecture). In ARM and MIPS only load and store access memory; all arithmetic and logic works between registers. The same calculation in ARM (variables already loaded in
r1-r5):SUB r0, r1, r2 @ r0 = A - B MUL r6, r4, r5 @ r6 = D * E ADD r6, r6, r3 @ r6 = C + D*E SDIV r0, r0, r6 @ r0 = Y (SDIV exists only in more recent ISA versions)
Example. If r2 points to a cell containing , LDR r1, [r2] loads into r1 and ADD r0, r1, r1 leaves r0=14.
CISC
Definition (CISC). In the 1970s high-level languages drifted away from machine language (semantic gap). The CISC answer (Complex Instruction Set Computer, e.g. VAX, x86): many instructions, many addressing modes, instructions performing high-level operations directly (string copies, procedure calls with context saving). Goals: simpler compilers, shorter programs, more functions in microcode (Hardwired and Microprogrammed ControlWhat the control unit does, control signals of a single-cycle datapath with the table for each instruction class, hardwired control (combinational or finite-state machine), microprogrammed control (control memory, horizontal and vertical microinstructions, microsequencer), and a comparison.Hardwired and Microprogrammed Control →).
Example: x86 adds a memory operand to a register in a single instruction; ARM needs LDR r2, [r1] and ADD r0, r0, r2.
What programs really execute. Measurements on compiled programs show: assignments dominate (about 40% of high-level statements) together with conditionals; procedure calls are the most expensive in time (memory accesses for parameters and context); most procedures have fewer than 6 parameters and few local variables; operands are mostly local scalar variables. Conclusion: make simple and frequent operations fast and keep operands in registers.
RISC
Definition (RISC architecture). Reduced Instruction Set Computer (MIPS, ARM, RISC-V):
- one instruction per cycle (at steady state, in a pipeline);
- register-register operations, only load and store access memory;
- few, simple addressing modes;
- fixed format (32 bits) and few formats, simple decoding;
- many registers (16-32 or more);
- hardwired control unit;
- optimization is moved to the compiler (register allocation, instruction reordering).
Example. In a fixed 32-bit RISC with registers each register field takes bits.
Register windows. A hardware solution to make calls fast (e.g. SPARC): many physical registers divided into windows; each active procedure sees a window with registers for parameters, local variables and temporaries. The caller's and callee's windows overlap: the caller's temporaries are the callee's parameter registers, so parameters pass without copying. Windows are organized circularly: when nested calls exhaust them, the oldest are saved to memory.
Definition (register allocation by graph coloring). A software solution: the compiler assigns variables to physical registers. Build a graph where each node is a variable (or temporary) and an edge joins two variables live at the same time. Assigning registers means coloring the graph with colors so that adjacent nodes get different colors. Variables that cannot be colored are kept in memory (spill).
Example. - and - conflict but - do not: colors suffice, , .
Comparison
| CISC | RISC | |
|---|---|---|
| instructions | many, complex, variable length | few, simple, fixed length |
| memory access | in many instructions | load/store only |
| cycles per instruction | many and variable | about 1 in a pipeline |
| registers | few | many |
| control | often microprogrammed | hardwired |
| code | more compact | longer |
Today the distinction is blurred: x86 processors translate CISC instructions internally into RISC-like micro-operations executed in a pipeline; ARM has added richer instructions and a compact 16-bit format (Thumb). For backward compatibility no widespread commercial architecture is purely RISC or CISC.
MIPS in brief
A textbook RISC: 32 registers of 32 bits (register 0 always reads 0 and is not writable), 32-bit instructions in formats R, I, J.
add $1, $2, $3 # $1 = $2 + $3
addi $2, $2, 4 # $2 = $2 + 4 (immediate)
lw $1, 12($2) # $1 = M[$2 + 12]
sw $1, 12($2) # M[$2 + 12] = $1Common mistakes
- Confusing ISA and microarchitecture: pipeline and cache are not part of the ISA.
- In 2-address machines, forgetting that the first operand is overwritten.
- Thinking RISC means "less powerful": it executes more instructions but each in less time.
- In a
sw, the first register is read (the datum to write), not written.
Versione ripasso
ISA = hardware/software boundary: operations, data types, visible registers, formats and addressing modes (Instruction Formats and Addressing ModesAddressing modes (immediate, direct, indirect, register, register indirect, displacement, PC-relative, scaled indexed, stack) with effective address and a numeric example, fixed versus variable instruction formats, the MIPS R, I and J formats and the 32-bit ARM data-processing format with the encodable-constant rule.Instruction Formats and Addressing Modes →), memory model, exceptions. Same ISA, same code, whatever the organization (CPU Structure and RegistersArchitecture versus organization, the stored-program (von Neumann) machine, the internal components of a CPU (ALU, control unit, register file, internal bus), user-visible and control/status registers, the status word with flags and supervisor bit, and how many registers are worth having.CPU Structure and Registers →).
Instruction and operands
Opcode, source operands (register, memory, immediate, I/O), result, next instruction. Operand types: addresses, numbers (Binary ArithmeticBinary addition and two's complement subtraction, overflow for unsigned (carry) and signed (sign change) numbers, the N Z C V flags, multiplication by shifts and additions, Booth's algorithm with a worked example, division, and logical, arithmetic and rotate shifts.Binary Arithmetic →, Fixed-Point and Floating-Point NumbersFixed-point numbers as an integer divided by 2 to the F with resolution and a sensor example, floating point in base 2, the IEEE 754 single and double formats, conversions in both directions with worked examples, special values and denormals, range, precision, rounding and floating-point operations.Fixed-Point and Floating-Point Numbers →), characters, bits; ARM sizes 8, 16, 32, 64 bits. Operation classes: transfer, arithmetic, logic, shift/rotate, conversion, I/O, system control, control transfer.
Number of addresses
For : 3 addresses 4 instructions (SUB Y,A,B; MUL T,D,E; ADD T,T,C; DIV Y,Y,T); 2 addresses (destination = first operand) needs MOVs; 1 address (accumulator) uses LOAD/STOR; 0 addresses (stack): PUSH/POP. Fewer addresses: shorter but more instructions.
Load/store
Only load/store access memory; arithmetic is register-register (ARM, MIPS): SUB r0,r1,r2; MUL r6,r4,r5; ADD r6,r6,r3; SDIV r0,r0,r6.
CISC vs RISC
- CISC: semantic gap; many complex instructions and modes, variable length, microprogrammed, compact code (Hardwired and Microprogrammed ControlWhat the control unit does, control signals of a single-cycle datapath with the table for each instruction class, hardwired control (combinational or finite-state machine), microprogrammed control (control memory, horizontal and vertical microinstructions, microsequencer), and a comparison.Hardwired and Microprogrammed Control →).
- RISC: one instruction per cycle in a pipeline; register-register; few modes; fixed 32-bit format; many registers (32 registers: 5 bits each); hardwired control; compiler optimizes.
- Programs: assignments about 40%, calls most expensive, under 6 parameters: keep operands in registers.
- Register windows (SPARC): overlapping windows pass parameters without copying; circular, oldest spilled to memory.
- Graph coloring: nodes = variables, edge = live together, colors = registers, uncolorable = spill. Example: , .
- Today blurred: x86 translates to RISC-like micro-operations; ARM has Thumb.
Common mistakes: ISA is not the pipeline or cache; in 2-address machines the first operand is overwritten; in sw the first register is read; RISC executes more but faster instructions.