University of Auckland · FACULTY OF COMPUTER SCIENCE

COMPSCI110 · Introduction to Computer Systems

- one subject, every graph, every model, every mark
Computer Science14 Chapters7-page Bible
Our own words - no uploaded lecturer files
Updated for this semester
Chapter 4 of 11 · COMPSCI 110

Assembly & Machine Code

Module 4 of University of Auckland COMPSCI110 programs the bare machine of Module 3. You learn the textbook 16-instruction set (LOAD through HALT), how to desk-check a program by hand as the CPU would, how to translate between assembly and machine code (a 4-bit opcode plus a 12-bit address), and how a two-pass assembler builds a symbol table to resolve labels. It carries 3 marks on the final exam and always appears — the instruction-set appendix (Figure 6.5) is supplied inside the test and exam.

In this chapter

What this chapter covers

  • 01The 16-instruction set (Figure 6.5): LOAD/STORE/CLEAR, ADD/SUBTRACT/INCREMENT/DECREMENT, COMPARE, the JUMP family, IN/OUT, HALT — with a single arithmetic register R
  • 02Register-R semantics: ADD X means R + CON(X) → R; COMPARE X sets the GT/EQ/LT condition codes; ADD/SUBTRACT use 16-bit two's complement
  • 03Program structure: .BEGIN … instructions … HALT … .DATA pseudo-ops … .END, and LABEL: .DATA 0 binding a name to an address
  • 04Desk checking: stepping through a program by hand, tracking R, memory cells and the GT/EQ/LT flags after each instruction
  • 05Machine-code format: 4-bit opcode + 12-bit address packed into 16 bits (e.g. IN at address 9 → 1101 0000 0000 1001 = D009₁₆)
  • 06Assembler vs compiler: assembly → machine code is one-to-one (table lookup); high-level → machine code is one-to-many
  • 07The two-pass assembler: pass 1 builds the symbol table (label → location), pass 2 substitutes opcodes and label addresses and handles .DATA
Worked example · free

Assembly → machine code, line by line

Q [4 marks]. Translate this program to machine code (4-bit opcode + 12-bit address, per Figure 6.5), given Y at address 13, THREE at address 14 and X at address 12. Give each line in binary and in hexadecimal.
LOAD Y · ADD THREE · STORE X · HALT
  • +1Look up each opcode in the 16-instruction table: LOAD = 0000, ADD = 0011, STORE = 0001, HALT = 1111.
  • +1Encode each operand as a 12-bit address: Y at 13 = 000000001101; THREE at 14 = 000000001110; X at 12 = 000000001100; HALT takes no operand (000000000000).
  • +1Concatenate opcode (4 bits) + address (12 bits) into 16 bits: LOAD Y = 0000 0000 0000 1101; ADD THREE = 0011 0000 0000 1110; STORE X = 0001 0000 0000 1100; HALT = 1111 0000 0000 0000.
  • +1Group each 16-bit word into 4-bit nibbles and read as hex: 000D₁₆, 300E₁₆, 100C₁₆, F000₁₆.
LOAD Y = 0000 0000 0000 1101 = 000D₁₆; ADD THREE = 0011 0000 0000 1110 = 300E₁₆; STORE X = 0001 0000 0000 1100 = 100C₁₆; HALT = 1111 0000 0000 0000 = F000₁₆.
Sia tip — The opcode must come straight from Figure 6.5 — mis-reading COMPARE (0111) for something else, or swapping ADD (0011) and SUBTRACT (0101), is the classic HIGH-cost slip. Keep the opcode in the top nibble and the address in the low 12 bits, and remember HALT (and .BEGIN/.END/.DATA directives) have no address field. Ask Sia to generate a fresh program and check your binary and hex line by line.
Glossary

Key terms

16-instruction set (Figure 6.5)
The textbook machine's opcode table, from LOAD (0000) and STORE (0001) through ADD (0011), SUBTRACT (0101), COMPARE (0111), the JUMP family (1000–1100), IN (1101), OUT (1110) and HALT (1111). It is supplied as an appendix in the test and exam, so you interpret it — you don't memorise every code — but knowing the common ones speeds you up.
Register R
The machine's single arithmetic register. Because there is only one, arithmetic instructions are implicitly about R: ADD X means R + CON(X) → R, SUBTRACT X means R − CON(X) → R. LOAD/STORE move a value between R and a memory cell. Arithmetic is done in 16-bit two's complement.
Pseudo-op (directive)
An instruction to the assembler rather than the CPU: .BEGIN and .END delimit the program, and .DATA reserves and initialises a memory cell (LABEL: .DATA 0 binds a label to that address). Pseudo-ops are not machine instructions and generate no opcode of their own.
Desk checking
Tracing a program by hand exactly as the CPU would — executing one instruction at a time and recording the value of R, the affected memory cells and the GT/EQ/LT condition codes after each step. It is how you predict a program's output and catch logic errors without running it.
Symbol table
The lookup table an assembler builds mapping each label to the memory address it stands for. It is constructed in the first pass (as the assembler counts instruction locations) so that the second pass can replace every label reference with its numeric address.
Two-pass assembler
The standard assembler design. Pass 1 scans the source and builds the symbol table (label → location counter). Pass 2 looks up each opcode, substitutes label addresses from the symbol table, processes .DATA, and writes the object (machine-code) file. An undefined opcode or label is flagged as an error.
FAQ

Assembly & Machine Code FAQ

How do I translate an assembly instruction to machine code?

Split the instruction into its opcode and its operand. Look the opcode up in Figure 6.5 to get its 4-bit code, encode the operand's address as a 12-bit binary number, and concatenate them into a 16-bit word (opcode in the top 4 bits, address in the low 12). You can then read the 16 bits as four hex nibbles. Instructions with no operand — HALT and the .BEGIN/.END/.DATA directives — leave the address field as zeros.

Why does the machine have only one register R?

It's a deliberately simple teaching architecture. With a single arithmetic register, every arithmetic instruction is implicitly about R (ADD X is R + CON(X) → R), which keeps the instruction format small and the desk-check bookkeeping manageable. Real machines have many registers, but the one-register model makes the fetch–decode–execute cycle and the role of LOAD/STORE easy to trace by hand.

Can AI help me with assembly language in COMPSCI110?

Yes, as a study aid. Sia can desk-check a program with you step by step, tracking R, memory and the GT/EQ/LT flags, translate between assembly and machine code, and explain how the two-pass assembler resolves labels. Use it to learn the method and rehearse on fresh programs — it explains and checks your working but does not do graded assignments or the exam for you, and the University of Auckland's academic-integrity rules apply to controlled assessments.

What does the two-pass assembler actually do, and why two passes?

Two passes solve the forward-reference problem: an instruction may jump to a label defined later in the program, whose address isn't known yet on first read. Pass 1 walks the whole program building the symbol table (every label → its address). Pass 2 then translates each line, and because the symbol table is now complete it can fill in any label's address, forward or backward. It also substitutes opcodes and handles .DATA.

What's the difference between an assembler and a compiler?

An assembler translates assembly to machine code one-to-one — each assembly instruction maps to a single machine instruction, so it is essentially a table lookup plus label resolution. A compiler translates a high-level language to machine code one-to-many — one source statement can become many machine instructions — and it needs full linguistic analysis (syntax and semantics), which is exactly what Module 7 covers.

Study strategy

Exam move

The two examinable skills are desk-checking and translation, and both reward calm, systematic bookkeeping. Practise desk-checking with a fixed layout: a column for R, a column per memory cell, and the GT/EQ/LT flags, updated after every instruction — most errors come from skipping a step or losing track of a flag, not from misunderstanding the instruction. For translation, learn the format cold (opcode top nibble, address low 12 bits) and be fluent looking codes up in Figure 6.5, which is supplied in the exam; drill the common opcodes (LOAD 0000, STORE 0001, ADD 0011, SUBTRACT 0101, COMPARE 0111, HALT 1111) so you're fast. Understand the program skeleton (.BEGIN … HALT … .DATA … .END) and that labels bind to addresses, which sets up the two-pass assembler: pass 1 builds the symbol table, pass 2 substitutes. Because the same style of program recurs across past papers, working two or three full desk-check-then-assemble questions in timed conditions is the highest-yield preparation.

Working through Assembly & Machine Code in COMPSCI 110? Sia is AskSia’s AI Computer Science tutor — ask any COMPSCI 110 Assembly & Machine Code question and get a clear, step-by-step explanation grounded in how COMPSCI 110 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.

A+Everything unlocked
Unlocks this Bible + all 12 of your University of Auckland subjects - and 1,000+ Bibles across every Australian university.
Sia - your COMPSCI110 tutor, unlimited, worked the way the exam marks it
The full 7-page Bible + practice bank with worked solutions
Chrome extension - sync your LMS so Sia knows your deadlines
Bilingual EN / Chinese on every Bible and every Sia answer
$25/ month
30-day money-back · cancel in one tap · how it works
Unlock the full COMPSCI110 Bible + 12 University of Auckland subjects解锁完整 COMPSCI110 Bible + University of Auckland 12 门科目
$25/mo