Lesson 32 +40 XP

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.
SolvedExample 1 · Adding a list of numbers

Write a program to add 5 numbers stored from location 150 onwards (12, −3, 40, 7, 100) and store the sum.

  1. Initialise PTR ← 150 (from ADS) and CTR ← −5 (from NBR); clear AC.
  2. 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.
  3. Store AC in SUM.
AnswerSUM = 12 − 3 + 40 + 7 + 100 = 156 = 009C
LocationHexLabelInstructionComment
ORG 100
100210BLDA ADSfirst address of operands
101310CSTA PTRinitialise pointer
102210DLDA NBRload minus 5
103310ESTA CTRinitialise counter
1047800CLAclear AC
105910CLOP,ADD PTR Iadd an operand to AC
106610CISZ PTRincrement pointer
107610EISZ CTRincrement counter
1084105BUN LOPrepeat loop
109310FSTA SUMstore sum
10A7001HLT
10B0150ADS,HEX 150first address of operands
10C0000PTR,HEX 0pointer
10DFFFBNBR,DEC −5constant to initialise the counter
10E0000CTR,HEX 0counter
10F0000SUM,HEX 0sum
150 to 154DEC 12, −3, 40, 7, 100the operands
VERILOG
// 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
endmodule

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

OUTPUT
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 = 156

2. 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 programFlowchart 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).

LocationHexLabelInstructionComment
ORG 100
1007400LOP,CLEclear E
1012113LDA Yload multiplier
1027080CIRtransfer multiplier bit to E
1033113STA Ystore shifted multiplier
1047002SZEcheck if bit is zero
1054107BUN ONEbit is one; go to ONE
106410BBUN ZRObit is zero; go to ZRO
1072112ONE,LDA Xload multiplicand
1081114ADD Padd to partial product
1093114STA Pstore partial product
10A7400CLEclear E
10B2112ZRO,LDA Xload multiplicand
10C7040CILshift left
10D3112STA Xstore shifted multiplicand
10E6111ISZ CTRincrement counter
10F4100BUN LOPcounter not zero; repeat loop
1107001HLTcounter is zero; halt
111FFF8CTR,DEC −8counter
112000FX,HEX Fmultiplicand (15)
113000BY,HEX Bmultiplier (11)
1140000P,HEX 0product formed here
SolvedExample 2 · Hand trace of 15 × 11

X = 0000 1111 (15), Y = 0000 1011 (11). Trace P for the first four passes.

  1. Pass 1: Y bit 0 = 1, P = 0 + 15 = 15. X shifts to 30.
  2. Pass 2: bit 1 = 1, P = 15 + 30 = 45. X = 60.
  3. Pass 3: bit 2 = 0, P = 45. X = 120.
  4. Pass 4: bit 3 = 1, P = 45 + 120 = 165. X = 240. Passes 5 to 8 see 0 bits.
AnswerP = 15, 45, 45, 165, then stays 165

The simulation prints X, Y and P after every pass of the loop:

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

The output matches the hand trace: X doubles each pass, Y loses one bit each pass, and P is 165 from pass 4 on:

OUTPUT
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 = 165
⚠️ Warning

This 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.

LabelInstructionComment
LDA ALload A low
ADD BLadd B low, carry in E
STA CLstore in C low
CLAclear AC
CILcirculate to bring the carry into AC(0)
ADD AHadd A high and carry
ADD BHadd B high
STA CHstore in C high
HLT
SolvedExample 3 · Double-precision numbers

A = 0001 F000 (hex) and B = 0002 2000. Find CH and CL.

  1. Low: F000 + 2000 = 1 1000: CL = 1000, E = 1.
  2. CLA, CIL: AC = 0001 (the carry).
  3. High: 0001 + 0001 + 0002 = 0004 = CH.
AnswerC = 0004 1000

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.
SolvedExample 4 · Arithmetic shift right with the program

AC = 8E14 (negative). Trace CLE, SPA, CME, CIR.

  1. CLE: E = 0. SPA: AC(15) = 1, so AC is negative: no skip.
  2. CME: E = 1.
  3. CIR: 1000 1110 0001 0100 shifts right with E = 1 entering: 1100 0111 0000 1010 = C70A; E = 0 (old bit 0).
AnswerAC = C70A, the arithmetic right shift of 8E14

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.

SolvedExample 5 · Subroutine to shift left four times

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.

  1. Main: LDA X / BSA SH4 / STA X / LDA Y / BSA SH4 / STA Y / HLT.
  2. SH4, HEX 0 (return address) / CIL / CIL / CIL / CIL / AND MSK / BUN SH4 I / MSK, HEX FFF0.
  3. The AND with FFF0 clears the four bits that came round from E.
Answercalled with BSA SH4, returns with BUN SH4 I

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.

SolvedExample 6 · OR subroutine with a parameter after BSA (Mano)

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.

  1. BSA stores the address of HEX 3AF6 (the word after BSA) in OR.
  2. CMA, STA TMP: TMP = X'. LDA OR I loads 3AF6 (the parameter). CMA, AND TMP, CMA: AC = (X' ∧ P')' = X ∨ P.
  3. ISZ OR: the return address now points past the parameter, to STA Y.
  4. 7B95 ∨ 3AF6 = 0111 1011 1001 0101 ∨ 0011 1010 1111 0110 = 0111 1011 1111 0111.
AnswerY = 7BF7

6. Input-Output Programming

A program reads a character by waiting for FGI and prints by waiting for FGO:

Input a characterOutput 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:

  1. save AC and E (it will change them),
  2. check FGI and FGO to find which device needs service, and do the input or output,
  3. restore E and AC,
  4. turn interrupts back on (ION),
  5. return to the interrupted program with BUN ZRO I (location 0 holds the return address).
SolvedExample 7 · Saving and restoring E

How does an interrupt routine save and restore E, since there is no instruction that stores E?

  1. Save: STA SAC (save AC), CIR (E goes into AC(15)), STA SE (save it).
  2. Restore: LDA SE, CIL (AC(15) goes back into E), LDA SAC.
  3. CIR and CIL make E part of a 17-bit circulating register EAC.
Answercirculate E into AC with CIR and store; restore with CIL

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

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