Lesson 35 +35 XP

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:

  1. an operation code field (the operation),
  2. one or more address fields (memory addresses or processor registers),
  3. 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

X = (A + B) ast (C + D)

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

InstructionRTL
ADD R1, A, BR1 ← M[A] + M[B]
ADD R2, C, DR2 ← M[C] + M[D]
MUL X, R1, R2M[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:

InstructionRTL
MOV R1, AR1 ← M[A]
ADD R1, BR1 ← R1 + M[B]
MOV R2, CR2 ← M[C]
ADD R2, DR2 ← R2 + M[D]
MUL R1, R2R1 ← R1 ast R2
MOV X, R1M[X] ← R1

The most common format in commercial computers.

1.3 One-address instructions

An implied accumulator holds one operand and the result:

InstructionRTL
LOAD AAC ← M[A]
ADD BAC ← AC + M[B]
STORE TM[T] ← AC
LOAD CAC ← M[C]
ADD DAC ← AC + M[D]
MUL TAC ← AC ast M[T]
STORE XM[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 + ∗:

InstructionEffect
PUSH ATOS ← A
PUSH BTOS ← B
ADDTOS ← (A + B)
PUSH CTOS ← C
PUSH DTOS ← D
ADDTOS ← (C + D)
MULTOS ← (C + D) ∗ (A + B)
POP XM[X] ← TOS

1.5 RISC (load/store) instructions

A RISC computer touches memory only with LOAD and STORE; every computation is between registers:

InstructionRTL
LOAD R1, AR1 ← M[A]
LOAD R2, BR2 ← M[B]
LOAD R3, CR3 ← M[C]
LOAD R4, DR4 ← M[D]
ADD R1, R1, R2R1 ← R1 + R2
ADD R3, R3, R4R3 ← R3 + R4
MUL R1, R1, R3R1 ← R1 ast R3
STORE X, R1M[X] ← R1
SolvedExample 1 · Comparing the formats

Count the instructions and the data memory references (operand reads and writes, not instruction fetches) for X = (A + B) ∗ (C + D) in each format.

  1. Three-address: 3 instructions; memory: A, B, C, D read, X written = 5.
  2. Two-address: 6 instructions; memory: A, B, C, D, X = 5.
  3. One-address: 7 instructions; memory: A, B, T (write), C, D, T (read), X = 7.
  4. Zero-address: 8 instructions; memory: A, B, C, D, X = 5 (the stack is in registers).
  5. RISC: 8 instructions; memory: 5 (only LOAD and STORE).
Answer3, 6, 7, 8, 8 instructions; fewer addresses per instruction means more instructions
SolvedExample 2 · How many instructions fit an instruction length (expanding opcode)

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?

  1. Two-address format: opcode = 32 − 2 × 12 = 8 bits, so 28 = 256 opcodes.
  2. 250 are used, 6 are free.
  3. Each free opcode is extended with the 12 bits of the unused second address field: 6 × 212.
Answer6 × 4096 = 24 576 one-address instructions
SolvedExample 3 · Writing a zero-address program

Write a zero-address program for Y = (A − B) / (C + D ∗ E).

  1. RPN: A B − C D E ∗ + /.
  2. Program: PUSH A, PUSH B, SUB, PUSH C, PUSH D, PUSH E, MUL, ADD, DIV, POP Y.
Answer10 instructions, as listed

2. Data Transfer Instructions

Data transfer instructions move data without changing it:

NameMnemonicNameMnemonic
LoadLDInputIN
StoreSTOutputOUT
MoveMOVPushPUSH
ExchangeXCHPopPOP

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

ArithmeticLogical and bit manipulationShift
IncrementINCClearCLRLogical shift rightSHR
DecrementDECComplementCOMLogical shift leftSHL
AddADDANDANDArithmetic shift rightSHRA
SubtractSUBORORArithmetic shift leftSHLA
MultiplyMULExclusive-ORXORRotate rightROR
DivideDIVClear carryCLRCRotate leftROL
Add with carryADDCSet carrySETCRotate right through carryRORC
Subtract with borrowSUBBComplement carryCOMCRotate left through carryROLC
Negate (2's complement)NEGEnable / disable interruptEI / DI

4. Program Control Instructions

These change the address in PC, so the program does not simply continue in sequence:

NameMnemonicNameMnemonic
BranchBRReturnRET
JumpJMPCompare (by subtraction)CMP
SkipSKPTest (by ANDing)TST
CallCALL

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 ALUStatus register bits from an 8-bit ALU

BitSet 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

MnemonicBranch conditionTest
BZ / BNZzero / not zeroZ = 1 / Z = 0
BC / BNCcarry / no carryC = 1 / C = 0
BP / BMplus / minusS = 0 / S = 1
BV / BNVoverflow / no overflowV = 1 / V = 0
Unsigned compare (A − B)
BHIA > BC = 1 and Z = 0
BHEA ≥ BC = 1
BLOA < BC = 0
BLOEA ≤ BC = 0 or Z = 1
Signed compare (A − B)
BGTA > B(S ⊕ V) = 0 and Z = 0
BGEA ≥ BS ⊕ V = 0
BLTA < BS ⊕ V = 1
BLEA ≤ B(S ⊕ V) = 1 or Z = 1
BE / BNEA = B / A ≠ BZ = 1 / Z = 0

In subtraction by 2's complement, C = 1 means no borrow, so for unsigned numbers C = 1 means A ≥ B.

SolvedExample 4 · Status bits after a compare (Mano)

A = 11110000 and B = 00010100. Find the status bits after A − B, and decide BHI and BGT.

  1. overline{B} + 1 = 11101100. A + 11101100 = 1 1101 1100: result 11011100, C8 = 1.
  2. S = 1 (bit 7), Z = 0.
  3. Carry into bit 7: the low 7 bits 1110000 + 1101100 = 1 1011100, so C7 = 1 and V = C8 oplus C7 = 0.
  4. Unsigned: A = 240, B = 20, A > B: BHI (C = 1, Z = 0) branches.
  5. Signed: A = −16, B = +20, A < B: S ⊕ V = 1, so BLT branches and BGT does not.
AnswerC = 1, S = 1, Z = 0, V = 0; BHI taken, BGT not taken
SolvedExample 5 · A compare that overflows

A = 01111111 (+127), B = 11111111 (−1 signed, 255 unsigned). Find the status bits after A − B.

  1. overline{B} + 1 = 00000001. A + 1 = 10000000, C8 = 0.
  2. S = 1, Z = 0. C7 = 1 (0111 1111 + 1 carries into bit 7), so V = 0 ⊕ 1 = 1.
  3. Signed: +127 − (−1) = +128 overflows, yet S ⊕ V = 0, so BGT correctly says A > B.
  4. Unsigned: 127 < 255: C = 0, BLO taken.
AnswerC = 0, S = 1, Z = 0, V = 1

4.3 Subroutine call and return

CALL saves the return address (the PC) and branches; RET restores it. Most computers use the memory stack:

CALL: SP ← SP - 1, quad M[SP] ← PC, quad PC ← EA qquad RET: PC ← M[SP], quad SP ← SP + 1

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:

KindCauseExample
Externalan I/O device, a timer, a power failurekeyboard ready, timer tick
Internal (traps)an error during an instructionoverflow, divide by zero, invalid opcode, stack overflow
Softwarea special instruction in the programsupervisor 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:

VERILOG
// 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
endmodule

The 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:

OUTPUT
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=1
💡 Tip

Key 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.
🎯 END-TERM EXAM PREP

📝 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)