Loading lessons...
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:
| Level | Example | Translated by |
|---|---|---|
| Binary code | 0010 0000 0000 0100 | (none: this is what memory holds) |
| Octal or hexadecimal code | 2004 | a simple conversion program |
| Symbolic code (assembly language) | LDA A | an assembler |
| High-level language | C = A + B | a 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.
Write a program to add two numbers stored in memory and store the sum, in hex, symbolic and high-level form.
- High-level:
C = A + B. - Symbolic: LDA A / ADD B / STA C / HLT, then A, B, C as data words.
- With ORG 0, the symbols get A = 004, B = 005, C = 006, so the hex program is 2004, 1005, 3006, 7001.
- With A = 83 and B = −23: data words 0053 and FFE9; C ends up 003C (60).
2. Assembly Language of the Basic Computer
Each line of an assembly program has three fields:
| Field | Contents |
|---|---|
| Label | empty, or a symbolic address of up to three characters ending with a comma |
| Instruction | a machine instruction or a pseudo-instruction |
| Comment | empty, or text starting with a slash / (ignored by the assembler) |
The instruction field holds one of:
- 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; - a register-reference or I/O instruction: a symbol with no address:
CLA,INP; - a pseudo-instruction: a direction to the assembler itself, not a machine instruction.
| Pseudo-instruction | Meaning |
|---|---|
| ORG N | the next instruction or data word goes at hexadecimal location N |
| END | end of the symbolic program |
| DEC N | the signed decimal number N is converted to binary (2's complement) |
| HEX N | the 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:
| Location | Hex | Label | Instruction | Comment |
|---|---|---|---|---|
| ORG 100 | origin of program is location 100 | |||
| 100 | 2107 | LDA SUB | load subtrahend to AC | |
| 101 | 7200 | CMA | complement AC | |
| 102 | 7020 | INC | increment AC (now 2's complement) | |
| 103 | 1106 | ADD MIN | add minuend to AC | |
| 104 | 3108 | STA DIF | store difference | |
| 105 | 7001 | HLT | halt computer | |
| 106 | 0053 | MIN, | DEC 83 | minuend |
| 107 | FFE9 | SUB, | DEC −23 | subtrahend |
| 108 | 0000 | DIF, | HEX 0 | difference stored here |
| END | end 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).
How is the line PL3,LDA SUB I stored?
- 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.
- Pairs per word: 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:
- First pass: assign a location to every line and build the address symbol table (each label with its binary address). Nothing is translated yet.
- Second pass: translate each instruction, using the tables, and store the binary code at its location.
Flowcharts 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:
| Table | Holds |
|---|---|
| Pseudo-instruction table | ORG, END, DEC, HEX and what to do for each |
| MRI table | the 7 memory-reference symbols with their 3-bit opcodes |
| Non-MRI table | the 18 register-reference and I/O symbols with their 16-bit codes |
| Address symbol table | built 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).
Build the address symbol table for the subtraction program, and show how it is stored as characters.
- LC starts at 100 (ORG 100). Lines without labels at 100 to 105.
- Labels: MIN at LC = 106, SUB at 107, DIF at 108.
- Stored as characters with a comma, then the address: MIN, = 4D49 4E2C 0106; SUB, = 5355 422C 0107; DIF, = 4449 462C 0108.
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?
- First pass: LDA at 0, ADD at 1, STA at 2, HLT at 3; A = 004, B = 005, C = 006, X = 007.
- LDA A: opcode 010, address 004: 2004. ADD B I: I = 1, opcode 001, address 005: 9005. STA C: 3006. HLT: 7001.
- Data: A = −5 = FFFB, B = 0007, C = 0000, X = 12 = 000C.
- Run: AC = −5. ADD B I: EA = M[5] = 7, so AC = −5 + M[7] = −5 + 12 = 7. C = 0007.
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:
// 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
endmoduleThe difference is 006A16 = 106, as expected. The 29 clock cycles are LDA 6 + CMA 4 + INC 4 + ADD 6 + STA 5 + HLT 4:
MIN=0053 SUB=ffe9 -> DIF=006a (106) E=0
halted at PC=106 after 29 clock cyclesKey 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).
📝 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)