Lesson 31 +35 XP

Programming the Basic Computer: Machine Language, Assembly Language & the Two-Pass Assembler

Programming the Basic Computer: Machine Language, Assembly Language & the Two-Pass Assembler

The hardware of the basic computer is complete. To use it we need programs.

This lesson shows the levels at which a program can be written, the rules of the basic computer's assembly language, and how a two-pass assembler translates a symbolic program into the binary words that the hardware of lessons 25 to 30 executes. We then run an assembled program on the Verilog model.

1. Levels of Programming Language

A program is a list of instructions. The hardware only executes binary instruction words, but people write programs at higher levels:

LevelExampleTranslated by
Binary code0010 0000 0000 0100(none: this is what memory holds)
Octal or hexadecimal code2004a simple conversion program
Symbolic code (assembly language)LDA Aan assembler
High-level languageC = A + Ba compiler

Machine language means the binary (or its octal/hex shorthand) form. An assembly language uses a symbolic name for each operation and for each address, with one assembly line per machine instruction. A high-level statement usually becomes many machine instructions.

SolvedExample 1 · One program at four levels

Write a program to add two numbers stored in memory and store the sum, in hex, symbolic and high-level form.

  1. High-level: C = A + B.
  2. Symbolic: LDA A / ADD B / STA C / HLT, then A, B, C as data words.
  3. With ORG 0, the symbols get A = 004, B = 005, C = 006, so the hex program is 2004, 1005, 3006, 7001.
  4. With A = 83 and B = −23: data words 0053 and FFE9; C ends up 003C (60).
Answer2004 1005 3006 7001 0053 FFE9 0000 (locations 000 to 006)

2. Assembly Language of the Basic Computer

Each line of an assembly program has three fields:

FieldContents
Labelempty, or a symbolic address of up to three characters ending with a comma
Instructiona machine instruction or a pseudo-instruction
Commentempty, or text starting with a slash / (ignored by the assembler)

The instruction field holds one of:

  1. a memory-reference instruction: a symbol, a symbolic address (a label used somewhere in the program), and an optional I for indirect: ADD OPR, ADD PTR I;
  2. a register-reference or I/O instruction: a symbol with no address: CLA, INP;
  3. a pseudo-instruction: a direction to the assembler itself, not a machine instruction.
Pseudo-instructionMeaning
ORG Nthe next instruction or data word goes at hexadecimal location N
ENDend of the symbolic program
DEC Nthe signed decimal number N is converted to binary (2's complement)
HEX Nthe hexadecimal number N is converted to binary

2.1 An example program: subtraction

Mano's example computes the difference MIN − SUB with MIN = 83 and SUB = −23. The assembler assigns each line a location counter (LC) value; ORG 100 makes the first instruction go at location 100:

LocationHexLabelInstructionComment
ORG 100origin of program is location 100
1002107LDA SUBload subtrahend to AC
1017200CMAcomplement AC
1027020INCincrement AC (now 2's complement)
1031106ADD MINadd minuend to AC
1043108STA DIFstore difference
1057001HLThalt computer
1060053MIN,DEC 83minuend
107FFE9SUB,DEC −23subtrahend
1080000DIF,HEX 0difference stored here
ENDend of symbolic program

The program computes 83 + 23 = 106 = 006A16, because subtracting -23 is adding 23.

3. The Assembler

An assembler reads the symbolic program (the source program) and produces the binary program (the object program).

3.1 How the source is stored

The source is typed in and stored in memory as ASCII characters, two 8-bit characters per 16-bit word. Each line ends with the code for carriage return (CR = 0D).

SolvedExample 2 · A line of code in memory

How is the line PL3,LDA SUB I stored?

  1. ASCII codes: P 50, L 4C, 3 33, comma 2C, L 4C, D 44, A 41, space 20, S 53, U 55, B 42, space 20, I 49, CR 0D.
  2. Pairs per word: 504C, 332C, 4C44, 4120, 5355, 4220, 490D.
Answer7 words: 504C 332C 4C44 4120 5355 4220 490D

The assembler scans these characters, recognises the symbols, and builds its tables.

3.2 Why two passes?

A memory-reference instruction such as LDA SUB needs the address of SUB, but SUB may be defined later in the program (here at location 107, after the instruction that uses it). So the assembler reads the program twice:

  1. First pass: assign a location to every line and build the address symbol table (each label with its binary address). Nothing is translated yet.
  2. Second pass: translate each instruction, using the tables, and store the binary code at its location.

Flowcharts of the first and second passFlowcharts of the first and second pass

The location counter (LC) holds the location of the line being processed. It starts at 0, is set by ORG, and is incremented after every line that occupies a word (an instruction or a DEC/HEX constant).

First pass in detail: for each line, if it has a label, store the label and the current LC value in the address symbol table. If the line is ORG, set LC to the operand and read the next line (no increment). If it is END, go to the second pass. Otherwise increment LC.

Second pass uses four tables:

TableHolds
Pseudo-instruction tableORG, END, DEC, HEX and what to do for each
MRI tablethe 7 memory-reference symbols with their 3-bit opcodes
Non-MRI tablethe 18 register-reference and I/O symbols with their 16-bit codes
Address symbol tablebuilt in the first pass

For each line: a pseudo-instruction is carried out (ORG sets LC, END stops, DEC and HEX convert their operand and store it at LC). An MRI is assembled from the opcode (MRI table), the 12-bit address (address symbol table) and the I bit.

A non-MRI is looked up and stored. Anything else is reported as an error in the line of code (for example an undefined symbol or a misspelled instruction).

SolvedExample 3 · First pass: the address symbol table

Build the address symbol table for the subtraction program, and show how it is stored as characters.

  1. LC starts at 100 (ORG 100). Lines without labels at 100 to 105.
  2. Labels: MIN at LC = 106, SUB at 107, DIF at 108.
  3. Stored as characters with a comma, then the address: MIN, = 4D49 4E2C 0106; SUB, = 5355 422C 0107; DIF, = 4449 462C 0108.
AnswerMIN = 106, SUB = 107, DIF = 108
SolvedExample 4 · Second pass: assembling with indirect addressing

Assemble: ORG 0 / LDA A / ADD B I / STA C / HLT / A, DEC −5 / B, HEX 7 / C, DEC 0 / X, DEC 12 / END. What is in C after it runs?

  1. First pass: LDA at 0, ADD at 1, STA at 2, HLT at 3; A = 004, B = 005, C = 006, X = 007.
  2. LDA A: opcode 010, address 004: 2004. ADD B I: I = 1, opcode 001, address 005: 9005. STA C: 3006. HLT: 7001.
  3. Data: A = −5 = FFFB, B = 0007, C = 0000, X = 12 = 000C.
  4. Run: AC = −5. ADD B I: EA = M[5] = 7, so AC = −5 + M[7] = −5 + 12 = 7. C = 0007.
Answer2004 9005 3006 7001 FFFB 0007 0000 000C; C = 7
⚠️ Warning

The label field may hold at most three characters and ends with a comma; the I for indirect comes after the address. Writing LDA I SUB or a 4-letter label is an error in the basic computer's assembly language.

4. Verilog: Running the Assembled Program

The testbench below loads the assembler's output for the subtraction program into the memory of the basic_computer module of lesson 29 and runs it until HLT:

VERILOG
// Runs on the basic_computer module of lesson 29
module tb;
    reg clk = 0, rst = 1;
    wire S;
    integer cycles = 0;
    basic_computer u (clk, rst, 12'h100, S);
    always #5 clk = ~clk;
    always @(posedge clk)
        if (!rst && S) cycles = cycles + 1;
    task load(input [11:0] addr, input [15:0] word);
        u.M[addr] = word;
    endtask
    initial begin
        // output of the assembler for the subtraction program
        load(12'h100, 16'h2107); // LDA SUB
        load(12'h101, 16'h7200); // CMA
        load(12'h102, 16'h7020); // INC
        load(12'h103, 16'h1106); // ADD MIN
        load(12'h104, 16'h3108); // STA DIF
        load(12'h105, 16'h7001); // HLT
        load(12'h106, 16'h0053); // MIN, DEC 83
        load(12'h107, 16'hFFE9); // SUB, DEC -23
        load(12'h108, 16'h0000); // DIF, HEX 0
        #12 rst = 0;
        wait (S == 0);
        #10;
        $display("MIN=%h SUB=%h  ->  DIF=%h (%0d)  E=%b", u.M[12'h106], u.M[12'h107],
                 u.M[12'h108], u.M[12'h108], u.E);
        $display("halted at PC=%h after %0d clock cycles", u.PC, cycles);
        $finish;
    end
endmodule

The difference is 006A16 = 106, as expected. The 29 clock cycles are LDA 6 + CMA 4 + INC 4 + ADD 6 + STA 5 + HLT 4:

OUTPUT
MIN=0053 SUB=ffe9  ->  DIF=006a (106)  E=0
halted at PC=106 after 29 clock cycles
💡 Tip

Key points to remember

  • Levels: binary, hex, symbolic (assembler), high-level (compiler).
  • Fields: label (≤ 3 characters + comma), instruction, comment (/).
  • Pseudo-instructions: ORG N, END, DEC N, HEX N.
  • First pass: LC and the address symbol table. Second pass: translate with the pseudo, MRI, non-MRI and symbol tables.
  • Source is stored as ASCII, two characters per word, lines end with CR (0D).
🎯 END-TERM EXAM PREP

📝 IPU End-Term Exam Corner (5-10 Marks)

  • Q1. Differentiate machine language, assembly language and high-level language with an example. (5 Marks)
  • Q2. Explain the rules of the assembly language of the basic computer and its pseudo-instructions. (5 Marks)
  • Q3. What is a two-pass assembler? Draw the flowcharts of the first and second pass and explain the tables used. (10 Marks)
  • Write: why two passes (forward references), LC, the address symbol table, the MRI and non-MRI tables, error detection.
  • Q4. Write an assembly program to subtract two numbers and give its hexadecimal translation. (5 Marks)
  • Q5. Show how the line PL3,LDA SUB I is stored in memory. (2.5 Marks)