Loading lessons...
Instruction Formats, Data Transfer & Manipulation, Program Control and Status Bits
Instruction Formats, Data Transfer & Manipulation, Program Control and Status Bits
A real instruction set has more than 25 instructions and several formats. This lesson classifies instructions by their number of address fields (three, two, one, zero, and the RISC load/store style), compares them on one expression, then surveys the three families every instruction set has: data transfer, data manipulation and program control, with the status bits that conditional branches test.
1. Instruction Formats
An instruction is divided into fields:
- an operation code field (the operation),
- one or more address fields (memory addresses or processor registers),
- a mode field (how to compute the effective address, next lesson).
The number of address fields depends on the CPU organisation of lesson 34. We compare them by writing a program for
with the symbols ADD, SUB, MUL, DIV, MOV, LOAD, STORE, PUSH, POP. A, B, C, D, X are memory addresses, R1 to R4 registers, T a temporary memory word.
1.1 Three-address instructions
| Instruction | RTL |
|---|---|
| ADD R1, A, B | R1 ← M[A] + M[B] |
| ADD R2, C, D | R2 ← M[C] + M[D] |
| MUL X, R1, R2 | M[X] ← R1 ast R2 |
Short programs, but each instruction needs three address fields, so instructions are long.
1.2 Two-address instructions
The first field is both a source and the destination:
| Instruction | RTL |
|---|---|
| MOV R1, A | R1 ← M[A] |
| ADD R1, B | R1 ← R1 + M[B] |
| MOV R2, C | R2 ← M[C] |
| ADD R2, D | R2 ← R2 + M[D] |
| MUL R1, R2 | R1 ← R1 ast R2 |
| MOV X, R1 | M[X] ← R1 |
The most common format in commercial computers.
1.3 One-address instructions
An implied accumulator holds one operand and the result:
| Instruction | RTL |
|---|---|
| LOAD A | AC ← M[A] |
| ADD B | AC ← AC + M[B] |
| STORE T | M[T] ← AC |
| LOAD C | AC ← M[C] |
| ADD D | AC ← AC + M[D] |
| MUL T | AC ← AC ast M[T] |
| STORE X | M[X] ← AC |
1.4 Zero-address instructions
A stack computer: arithmetic instructions use the top of the stack; only PUSH and POP have an address. The program is the reverse Polish form A B + C D + ∗:
| Instruction | Effect |
|---|---|
| PUSH A | TOS ← A |
| PUSH B | TOS ← B |
| ADD | TOS ← (A + B) |
| PUSH C | TOS ← C |
| PUSH D | TOS ← D |
| ADD | TOS ← (C + D) |
| MUL | TOS ← (C + D) ∗ (A + B) |
| POP X | M[X] ← TOS |
1.5 RISC (load/store) instructions
A RISC computer touches memory only with LOAD and STORE; every computation is between registers:
| Instruction | RTL |
|---|---|
| LOAD R1, A | R1 ← M[A] |
| LOAD R2, B | R2 ← M[B] |
| LOAD R3, C | R3 ← M[C] |
| LOAD R4, D | R4 ← M[D] |
| ADD R1, R1, R2 | R1 ← R1 + R2 |
| ADD R3, R3, R4 | R3 ← R3 + R4 |
| MUL R1, R1, R3 | R1 ← R1 ast R3 |
| STORE X, R1 | M[X] ← R1 |
Count the instructions and the data memory references (operand reads and writes, not instruction fetches) for X = (A + B) ∗ (C + D) in each format.
- Three-address: 3 instructions; memory: A, B, C, D read, X written = 5.
- Two-address: 6 instructions; memory: A, B, C, D, X = 5.
- One-address: 7 instructions; memory: A, B, T (write), C, D, T (read), X = 7.
- Zero-address: 8 instructions; memory: A, B, C, D, X = 5 (the stack is in registers).
- RISC: 8 instructions; memory: 5 (only LOAD and STORE).
An instruction is 32 bits and an address is 12 bits. There are 250 two-address instructions. How many one-address instructions can be added?
- Two-address format: opcode = 32 − 2 × 12 = 8 bits, so 28 = 256 opcodes.
- 250 are used, 6 are free.
- Each free opcode is extended with the 12 bits of the unused second address field: 6 × 212.
Write a zero-address program for Y = (A − B) / (C + D ∗ E).
- RPN: A B − C D E ∗ + /.
- Program: PUSH A, PUSH B, SUB, PUSH C, PUSH D, PUSH E, MUL, ADD, DIV, POP Y.
2. Data Transfer Instructions
Data transfer instructions move data without changing it:
| Name | Mnemonic | Name | Mnemonic | |
|---|---|---|---|---|
| Load | LD | Input | IN | |
| Store | ST | Output | OUT | |
| Move | MOV | Push | PUSH | |
| Exchange | XCH | Pop | POP |
The same LD can appear with different addressing modes (lesson 36): LD ADR (direct), LD @ADR (indirect), LD #NBR (immediate), LD ADR(X) (indexed), LD R1 (register), LD (R1) (register indirect), LD $ADR (relative).
3. Data Manipulation Instructions
| Arithmetic | Logical and bit manipulation | Shift | |||
|---|---|---|---|---|---|
| Increment | INC | Clear | CLR | Logical shift right | SHR |
| Decrement | DEC | Complement | COM | Logical shift left | SHL |
| Add | ADD | AND | AND | Arithmetic shift right | SHRA |
| Subtract | SUB | OR | OR | Arithmetic shift left | SHLA |
| Multiply | MUL | Exclusive-OR | XOR | Rotate right | ROR |
| Divide | DIV | Clear carry | CLRC | Rotate left | ROL |
| Add with carry | ADDC | Set carry | SETC | Rotate right through carry | RORC |
| Subtract with borrow | SUBB | Complement carry | COMC | Rotate left through carry | ROLC |
| Negate (2's complement) | NEG | Enable / disable interrupt | EI / DI |
4. Program Control Instructions
These change the address in PC, so the program does not simply continue in sequence:
| Name | Mnemonic | Name | Mnemonic |
|---|---|---|---|
| Branch | BR | Return | RET |
| Jump | JMP | Compare (by subtraction) | CMP |
| Skip | SKP | Test (by ANDing) | TST |
| Call | CALL |
CMP and TST perform a subtraction or an AND only to set the status bits; the result is discarded.
4.1 Status bit conditions
The status register (or flags, condition codes) holds bits set by the last ALU operation:
Status register bits from an 8-bit ALU
| Bit | Set to 1 when |
|---|---|
| C (carry) | the output carry C8 is 1 |
| S (sign) | the most significant bit F7 of the result is 1 |
| Z (zero) | the result is 0 |
| V (overflow) | C8 oplus C7 = 1 (signed overflow) |
4.2 Conditional branch instructions
| Mnemonic | Branch condition | Test |
|---|---|---|
| BZ / BNZ | zero / not zero | Z = 1 / Z = 0 |
| BC / BNC | carry / no carry | C = 1 / C = 0 |
| BP / BM | plus / minus | S = 0 / S = 1 |
| BV / BNV | overflow / no overflow | V = 1 / V = 0 |
| Unsigned compare (A − B) | ||
| BHI | A > B | C = 1 and Z = 0 |
| BHE | A ≥ B | C = 1 |
| BLO | A < B | C = 0 |
| BLOE | A ≤ B | C = 0 or Z = 1 |
| Signed compare (A − B) | ||
| BGT | A > B | (S ⊕ V) = 0 and Z = 0 |
| BGE | A ≥ B | S ⊕ V = 0 |
| BLT | A < B | S ⊕ V = 1 |
| BLE | A ≤ B | (S ⊕ V) = 1 or Z = 1 |
| BE / BNE | A = B / A ≠ B | Z = 1 / Z = 0 |
In subtraction by 2's complement, C = 1 means no borrow, so for unsigned numbers C = 1 means A ≥ B.
A = 11110000 and B = 00010100. Find the status bits after A − B, and decide BHI and BGT.
- overline{B} + 1 = 11101100. A + 11101100 = 1 1101 1100: result 11011100, C8 = 1.
- S = 1 (bit 7), Z = 0.
- Carry into bit 7: the low 7 bits 1110000 + 1101100 = 1 1011100, so C7 = 1 and V = C8 oplus C7 = 0.
- Unsigned: A = 240, B = 20, A > B: BHI (C = 1, Z = 0) branches.
- Signed: A = −16, B = +20, A < B: S ⊕ V = 1, so BLT branches and BGT does not.
A = 01111111 (+127), B = 11111111 (−1 signed, 255 unsigned). Find the status bits after A − B.
- overline{B} + 1 = 00000001. A + 1 = 10000000, C8 = 0.
- S = 1, Z = 0. C7 = 1 (0111 1111 + 1 carries into bit 7), so V = 0 ⊕ 1 = 1.
- Signed: +127 − (−1) = +128 overflows, yet S ⊕ V = 0, so BGT correctly says A > B.
- Unsigned: 127 < 255: C = 0, BLO taken.
4.3 Subroutine call and return
CALL saves the return address (the PC) and branches; RET restores it. Most computers use the memory stack:
Using a stack lets subroutines call other subroutines, and themselves (recursion), since each call gets its own stack word. The basic computer's BSA stores the return address in the subroutine's first word instead, which does not allow recursion.
4.4 Program interrupt
An interrupt transfers control to a service routine, like a call that is started by a signal rather than an instruction. The CPU saves the PC and the program status word (PSW) (status bits and mode) before servicing, and restores them on return. Three kinds:
| Kind | Cause | Example |
|---|---|---|
| External | an I/O device, a timer, a power failure | keyboard ready, timer tick |
| Internal (traps) | an error during an instruction | overflow, divide by zero, invalid opcode, stack overflow |
| Software | a special instruction in the program | supervisor call to the operating system |
5. Verilog: Status Bits and Branch Decisions
The module forms A − B as A + overline{B} + 1, takes C8 and C7 from the adder, and the testbench prints the status bits with the unsigned and signed branch decisions of the table:
// Status bits C, S, Z, V of an 8-bit compare (A - B) and the conditional
// branch decisions that test them
module status_bits (
input [7:0] a,
input [7:0] b,
output [7:0] f,
output c, s, z, v
);
wire [8:0] sum = {1'b0, a} + {1'b0, ~b} + 9'd1; // A + B' + 1 = A - B
wire [6:0] nb = ~b[6:0];
wire [7:0] low = {1'b0, a[6:0]} + {1'b0, nb} + 8'd1;
wire c7 = low[7]; // carry into bit 7
assign f = sum[7:0];
assign c = sum[8]; // C = 1: no borrow (A >= B unsigned)
assign s = f[7]; // sign of the result
assign z = (f == 8'h00); // result is zero
assign v = c ^ c7; // overflow: C8 xor C7
endmodule
module tb;
reg [7:0] a, b;
wire [7:0] f;
wire c, s, z, v;
status_bits u (a, b, f, c, s, z, v);
task cmp(input [7:0] x, input [7:0] y);
begin
a = x;
b = y;
#1 $display("A=%b B=%b A-B=%b C=%b S=%b Z=%b V=%b BHI=%b BLO=%b BGT=%b BLT=%b BE=%b",
a, b, f, c, s, z, v,
c & ~z, // unsigned A > B
~c, // unsigned A < B
~(s ^ v) & ~z, // signed A > B
s ^ v, // signed A < B
z);
end
endtask
initial begin
cmp(8'b11110000, 8'b00010100); // 240 vs 20, or -16 vs +20
cmp(8'b00010100, 8'b11110000); // 20 vs 240, or +20 vs -16
cmp(8'b01111111, 8'b11111111); // 127 vs 255, or +127 vs -1 (overflow)
cmp(8'b01010101, 8'b01010101); // equal
end
endmoduleThe first line is Example 4 (unsigned greater, signed less), the third is Example 5 (V = 1 but BGT is still correct), and equal operands give Z = 1:
A=11110000 B=00010100 A-B=11011100 C=1 S=1 Z=0 V=0 BHI=1 BLO=0 BGT=0 BLT=1 BE=0
A=00010100 B=11110000 A-B=00100100 C=0 S=0 Z=0 V=0 BHI=0 BLO=1 BGT=1 BLT=0 BE=0
A=01111111 B=11111111 A-B=10000000 C=0 S=1 Z=0 V=1 BHI=0 BLO=1 BGT=1 BLT=0 BE=0
A=01010101 B=01010101 A-B=00000000 C=1 S=0 Z=1 V=0 BHI=0 BLO=0 BGT=0 BLT=0 BE=1Key points to remember
- Formats: three-, two-, one-, zero-address and RISC load/store; fewer addresses per instruction means more instructions.
- X = (A + B) ∗ (C + D): 3, 6, 7, 8 and 8 instructions.
- Three families: data transfer, data manipulation (arithmetic, logical, shift), program control.
- Status bits: C (C8), S (F7), Z (result 0), V (C8 oplus C7).
- Unsigned branches test C and Z; signed branches test S ⊕ V and Z.
📝 IPU End-Term Exam Corner (5-10 Marks)
- Q1. Write programs to evaluate X = (A + B) ∗ (C + D) using three-, two-, one- and zero-address instructions and RISC instructions. Compare them. (10 Marks)
- Q2. List typical data transfer and data manipulation instructions. (5 Marks)
- Q3. Explain the status bits C, S, Z, V with a diagram. Find them after comparing A = 11110000 and B = 00010100. (10 Marks)
- Write: the 2's complement subtraction, C8, C7, and the unsigned and signed interpretation.
- Q4. List the conditional branch instructions for unsigned and signed numbers. (5 Marks)
- Q5. A 32-bit instruction has 12-bit addresses and 250 two-address instructions. How many one-address instructions are possible? (5 Marks)