Loading lessons...
Assembly Programs: Loops, Multiplication, Subroutines & Input-Output
Assembly Programs: Loops, Multiplication, Subroutines & Input-Output
The basic computer has only 25 instructions, yet it can compute anything (lesson 26). This lesson shows how: program loops with a pointer and a counter, multiplication by shift and add, double-precision arithmetic, logic and shift operations missing from the instruction set, subroutines with parameters, and input-output programs. Two of the programs are run on the Verilog model of the basic computer.
1. Program Loops
A program loop is a sequence of instructions executed many times, each time with different data. The basic computer builds a loop from:
- a pointer: a memory word holding the address of the current operand, used with indirect addressing (ADD PTR I) and advanced with ISZ PTR;
- a counter: a memory word initialised to the negative of the number of passes and incremented with ISZ CTR. When it reaches 0, ISZ skips the branch back, ending the loop.
Write a program to add 5 numbers stored from location 150 onwards (12, −3, 40, 7, 100) and store the sum.
- Initialise PTR ← 150 (from ADS) and CTR ← −5 (from NBR); clear AC.
- Loop: ADD PTR I adds the operand that PTR points to; ISZ PTR moves to the next; ISZ CTR counts; BUN LOP repeats until CTR = 0, when ISZ skips the BUN.
- Store AC in SUM.
| Location | Hex | Label | Instruction | Comment |
|---|---|---|---|---|
| ORG 100 | ||||
| 100 | 210B | LDA ADS | first address of operands | |
| 101 | 310C | STA PTR | initialise pointer | |
| 102 | 210D | LDA NBR | load minus 5 | |
| 103 | 310E | STA CTR | initialise counter | |
| 104 | 7800 | CLA | clear AC | |
| 105 | 910C | LOP, | ADD PTR I | add an operand to AC |
| 106 | 610C | ISZ PTR | increment pointer | |
| 107 | 610E | ISZ CTR | increment counter | |
| 108 | 4105 | BUN LOP | repeat loop | |
| 109 | 310F | STA SUM | store sum | |
| 10A | 7001 | HLT | ||
| 10B | 0150 | ADS, | HEX 150 | first address of operands |
| 10C | 0000 | PTR, | HEX 0 | pointer |
| 10D | FFFB | NBR, | DEC −5 | constant to initialise the counter |
| 10E | 0000 | CTR, | HEX 0 | counter |
| 10F | 0000 | SUM, | HEX 0 | sum |
| 150 to 154 | DEC 12, −3, 40, 7, 100 | the operands |
// Adding a list of numbers with a pointer and a counter (basic_computer of lesson 29)
module tb;
reg clk = 0, rst = 1;
wire S;
basic_computer u (clk, rst, 12'h100, S);
always #5 clk = ~clk;
task load(input [11:0] addr, input [15:0] word);
u.M[addr] = word;
endtask
// after each ADD PTR I show the running sum, the pointer and the counter
always @(negedge clk)
if (u.SC == 0 && u.IR == 16'h910C)
$display("AC=%h (%0d) PTR=%h CTR=%h", u.AC, $signed(u.AC), u.M[12'h10C], u.M[12'h10E]);
initial begin
load(12'h100, 16'h210B); // LDA ADS
load(12'h101, 16'h310C); // STA PTR
load(12'h102, 16'h210D); // LDA NBR
load(12'h103, 16'h310E); // STA CTR
load(12'h104, 16'h7800); // CLA
load(12'h105, 16'h910C); // LOP, ADD PTR I
load(12'h106, 16'h610C); // ISZ PTR
load(12'h107, 16'h610E); // ISZ CTR
load(12'h108, 16'h4105); // BUN LOP
load(12'h109, 16'h310F); // STA SUM
load(12'h10A, 16'h7001); // HLT
load(12'h10B, 16'h0150); // ADS, HEX 150
load(12'h10C, 16'h0000); // PTR, HEX 0
load(12'h10D, 16'hFFFB); // NBR, DEC -5
load(12'h10E, 16'h0000); // CTR, HEX 0
load(12'h10F, 16'h0000); // SUM, HEX 0
load(12'h150, 16'h000C); // DEC 12
load(12'h151, 16'hFFFD); // DEC -3
load(12'h152, 16'h0028); // DEC 40
load(12'h153, 16'h0007); // DEC 7
load(12'h154, 16'h0064); // DEC 100
#12 rst = 0;
wait (S == 0);
#10 $display("halted: SUM = %0d", u.M[12'h10F]);
$finish;
end
endmoduleEach line is the state after one ADD PTR I. The counter climbs from FFFB (−5) and the pointer from 150; after the fifth pass ISZ CTR reaches 0 and skips BUN LOP:
AC=000c (12) PTR=0150 CTR=fffb
AC=0009 (9) PTR=0151 CTR=fffc
AC=0031 (49) PTR=0152 CTR=fffd
AC=0038 (56) PTR=0153 CTR=fffe
AC=009c (156) PTR=0154 CTR=ffff
halted: SUM = 1562. Multiplication Program
There is no multiply instruction, so multiplication is done by shift and add, as by hand in binary. For positive numbers of up to 8 significant bits:
- Look at the multiplier Y one bit at a time from the right: circulate Y right through E (cir EAC), so the bit lands in E.
- If the bit is 1, add the multiplicand X to the partial product P.
- Shift X left once (cil EAC) for the next bit position.
- Repeat 8 times, counted by CTR = −8.
Flowchart of the multiplication program
E must be cleared before each circulate (so 0 enters Y and X), and again after the addition (so a carry from the addition does not enter X's shift).
| Location | Hex | Label | Instruction | Comment |
|---|---|---|---|---|
| ORG 100 | ||||
| 100 | 7400 | LOP, | CLE | clear E |
| 101 | 2113 | LDA Y | load multiplier | |
| 102 | 7080 | CIR | transfer multiplier bit to E | |
| 103 | 3113 | STA Y | store shifted multiplier | |
| 104 | 7002 | SZE | check if bit is zero | |
| 105 | 4107 | BUN ONE | bit is one; go to ONE | |
| 106 | 410B | BUN ZRO | bit is zero; go to ZRO | |
| 107 | 2112 | ONE, | LDA X | load multiplicand |
| 108 | 1114 | ADD P | add to partial product | |
| 109 | 3114 | STA P | store partial product | |
| 10A | 7400 | CLE | clear E | |
| 10B | 2112 | ZRO, | LDA X | load multiplicand |
| 10C | 7040 | CIL | shift left | |
| 10D | 3112 | STA X | store shifted multiplicand | |
| 10E | 6111 | ISZ CTR | increment counter | |
| 10F | 4100 | BUN LOP | counter not zero; repeat loop | |
| 110 | 7001 | HLT | counter is zero; halt | |
| 111 | FFF8 | CTR, | DEC −8 | counter |
| 112 | 000F | X, | HEX F | multiplicand (15) |
| 113 | 000B | Y, | HEX B | multiplier (11) |
| 114 | 0000 | P, | HEX 0 | product formed here |
X = 0000 1111 (15), Y = 0000 1011 (11). Trace P for the first four passes.
- Pass 1: Y bit 0 = 1, P = 0 + 15 = 15. X shifts to 30.
- Pass 2: bit 1 = 1, P = 15 + 30 = 45. X = 60.
- Pass 3: bit 2 = 0, P = 45. X = 120.
- Pass 4: bit 3 = 1, P = 45 + 120 = 165. X = 240. Passes 5 to 8 see 0 bits.
The simulation prints X, Y and P after every pass of the loop:
// Mano's multiplication program, run on the basic_computer module of lesson 29
module tb;
reg clk = 0, rst = 1;
wire S;
integer pass = 0;
basic_computer u (clk, rst, 12'h100, S);
always #5 clk = ~clk;
task load(input [11:0] addr, input [15:0] word);
u.M[addr] = word;
endtask
// after each ISZ CTR (end of one pass of the loop) show X, Y and P
always @(negedge clk)
if (u.SC == 0 && u.IR == 16'h6111) begin
pass = pass + 1;
$display("pass %0d: X=%h Y=%h P=%h (%0d)", pass,
u.M[12'h112], u.M[12'h113], u.M[12'h114], u.M[12'h114]);
end
initial begin
load(12'h100, 16'h7400); // LOP, CLE
load(12'h101, 16'h2113); // LDA Y
load(12'h102, 16'h7080); // CIR
load(12'h103, 16'h3113); // STA Y
load(12'h104, 16'h7002); // SZE
load(12'h105, 16'h4107); // BUN ONE
load(12'h106, 16'h410B); // BUN ZRO
load(12'h107, 16'h2112); // ONE, LDA X
load(12'h108, 16'h1114); // ADD P
load(12'h109, 16'h3114); // STA P
load(12'h10A, 16'h7400); // CLE
load(12'h10B, 16'h2112); // ZRO, LDA X
load(12'h10C, 16'h7040); // CIL
load(12'h10D, 16'h3112); // STA X
load(12'h10E, 16'h6111); // ISZ CTR
load(12'h10F, 16'h4100); // BUN LOP
load(12'h110, 16'h7001); // HLT
load(12'h111, 16'hFFF8); // CTR, DEC -8
load(12'h112, 16'h000F); // X, HEX F
load(12'h113, 16'h000B); // Y, HEX B
load(12'h114, 16'h0000); // P, HEX 0
#12 rst = 0;
wait (S == 0);
#10 $display("halted: P = %0d", u.M[12'h114]);
$finish;
end
endmoduleThe output matches the hand trace: X doubles each pass, Y loses one bit each pass, and P is 165 from pass 4 on:
pass 1: X=001e Y=0005 P=000f (15)
pass 2: X=003c Y=0002 P=002d (45)
pass 3: X=0078 Y=0001 P=002d (45)
pass 4: X=00f0 Y=0000 P=00a5 (165)
pass 5: X=01e0 Y=0000 P=00a5 (165)
pass 6: X=03c0 Y=0000 P=00a5 (165)
pass 7: X=0780 Y=0000 P=00a5 (165)
pass 8: X=0f00 Y=0000 P=00a5 (165)
halted: P = 165This program assumes positive operands whose product fits in 16 bits. Signed multiplication needs the signs handled separately (or Booth's algorithm, Unit IV), and a product of two 16-bit numbers needs a double-precision (32-bit) result.
3. Double-Precision Addition
A number that needs 32 bits is stored in two words: the low half (AL) and the high half (AH). To add A and B, add the low halves, then add the high halves plus the carry from the low addition. The carry is in E after ADD; CIL brings it into bit 0 of a cleared AC.
| Label | Instruction | Comment |
|---|---|---|
| LDA AL | load A low | |
| ADD BL | add B low, carry in E | |
| STA CL | store in C low | |
| CLA | clear AC | |
| CIL | circulate to bring the carry into AC(0) | |
| ADD AH | add A high and carry | |
| ADD BH | add B high | |
| STA CH | store in C high | |
| HLT |
A = 0001 F000 (hex) and B = 0002 2000. Find CH and CL.
- Low: F000 + 2000 = 1 1000: CL = 1000, E = 1.
- CLA, CIL: AC = 0001 (the carry).
- High: 0001 + 0001 + 0002 = 0004 = CH.
4. Logic and Shift Operations
Missing operations are built from the existing ones:
- OR by De Morgan, x + y = (x' y')': LDA A, CMA, STA TMP, LDA B, CMA, AND TMP, CMA.
- Logical shift right: CLE, CIR (0 enters at the left). Logical shift left: CLE, CIL.
- Arithmetic shift right: the sign must enter at the left. CLE, SPA, CME, CIR: SPA skips CME when AC is positive, so E = AC(15) before the circulate.
AC = 8E14 (negative). Trace CLE, SPA, CME, CIR.
- CLE: E = 0. SPA: AC(15) = 1, so AC is negative: no skip.
- CME: E = 1.
- CIR: 1000 1110 0001 0100 shifts right with E = 1 entering: 1100 0111 0000 1010 = C70A; E = 0 (old bit 0).
5. Subroutines
A subroutine is a set of instructions used many times from different places. The basic computer calls it with BSA (which stores the return address in its first word) and returns with an indirect BUN through that word.
Write a subroutine SH4 that shifts AC left four times and clears the low 4 bits (a logical shift by 4), and call it twice.
- Main: LDA X / BSA SH4 / STA X / LDA Y / BSA SH4 / STA Y / HLT.
- SH4, HEX 0 (return address) / CIL / CIL / CIL / CIL / AND MSK / BUN SH4 I / MSK, HEX FFF0.
- The AND with FFF0 clears the four bits that came round from E.
5.1 Passing parameters
Data can be passed to a subroutine in AC, or as words placed right after the BSA. The subroutine reads them through its return address (indirect) and increments the return address with ISZ past each parameter.
Main program: LDA X / BSA OR / HEX 3AF6 / STA Y / HLT, with X = 7B95. The subroutine: OR, HEX 0 / CMA / STA TMP / LDA OR I / CMA / AND TMP / CMA / ISZ OR / BUN OR I. Find Y.
- BSA stores the address of HEX 3AF6 (the word after BSA) in OR.
- CMA, STA TMP: TMP = X'. LDA OR I loads 3AF6 (the parameter). CMA, AND TMP, CMA: AC = (X' ∧ P')' = X ∨ P.
- ISZ OR: the return address now points past the parameter, to STA Y.
- 7B95 ∨ 3AF6 = 0111 1011 1001 0101 ∨ 0011 1010 1111 0110 = 0111 1011 1111 0111.
6. Input-Output Programming
A program reads a character by waiting for FGI and prints by waiting for FGO:
| Input a character | Output a character |
|---|---|
| CIF, SKI (check input flag) | LDA CHR (load character) |
| BUN CIF (flag 0: check again) | COF, SKO (check output flag) |
| INP (flag 1: input character) | BUN COF (flag 0: check again) |
| OUT (echo it) | OUT (flag 1: output character) |
| STA CHR (store it) | HLT |
Character packing: two 8-bit characters fit in one 16-bit word. Input the first character, shift AC left 8 times (CIL eight times), then INP the second into AC(0-7), and store the word.
6.1 Interrupt service routine
With interrupts, the main program runs freely and the interrupt cycle (lesson 30) branches to location 1, which holds BUN SRV. The service routine must:
- save AC and E (it will change them),
- check FGI and FGO to find which device needs service, and do the input or output,
- restore E and AC,
- turn interrupts back on (ION),
- return to the interrupted program with BUN ZRO I (location 0 holds the return address).
How does an interrupt routine save and restore E, since there is no instruction that stores E?
- Save: STA SAC (save AC), CIR (E goes into AC(15)), STA SE (save it).
- Restore: LDA SE, CIL (AC(15) goes back into E), LDA SAC.
- CIR and CIL make E part of a 17-bit circulating register EAC.
Key points to remember
- Loops: pointer with indirect addressing + counter set to −N and ISZ.
- Multiplication: cir EAC on Y puts each bit in E; add X when E = 1; cil X; 8 passes.
- Double precision: add low halves, CLA + CIL brings the carry, add high halves.
- Subroutines: BSA call, BUN … I return; parameters after BSA read with LDA X I and skipped with ISZ X.
- I/O: SKI/SKO loops; interrupt routine saves AC and E, services, restores, ION, BUN 0 I.
📝 IPU End-Term Exam Corner (5-10 Marks)
- Q1. Write an assembly program to add 100 numbers stored in consecutive locations. Explain the use of the pointer and counter. (10 Marks)
- Q2. Draw the flowchart and write the program to multiply two positive numbers in the basic computer. Trace it for 15 × 11. (10 Marks)
- Write: the flowchart, the listing with CTR = −8, and the P values 15, 45, 45, 165.
- Q3. Write a program for double-precision addition. (5 Marks)
- Q4. What is a subroutine? Explain how parameters are passed in the basic computer with the OR subroutine. (5 Marks)
- Q5. Write programs to input and output a character. What must an interrupt service routine do? (5 Marks)