CS-150 – Low-Level Programming: Machine & Assembly Language Vocabulary

Goals

  • Understand machine-language instructions within the \text{fetch \rightarrow decode \rightarrow execute} cycle
  • Learn basic Pep/9 architecture, instruction set, addressing modes, I/O and simple assembly programs

Computer Architecture Recap

  • Stored-program (von Neumann): data & instructions both stored as bit patterns in the same memory
  • CPU repeatedly performs: Fetch → Decode → Execute on instruction at \text{PC}
  • Abstraction levels: High-level → Assembly → Machine code

Machine Language Basics

  • Binary-coded instructions directly recognised by CPU hardware
  • CPU-specific; each instruction performs one low-level task
  • Early programmers wrote machine code because no higher-level tools existed

Pep/9 Virtual Computer

  • 16-bit hypothetical architecture (40 instructions) used for teaching
  • Key registers:
    • \text{PC} (16 b) – address of next instruction
    • \text{IR} (8 b opcode + 16 b operand copy)
    • Accumulator A (16 b) – arithmetic/data register
  • Memory: 64\,\text{KB}, byte-addressable (0x0000 – 0xFFFF)

Instruction Format

  • Opcode (4 b) | Addressing-mode + Register bit (4 b) | 0–16 b operand
  • Addressing modes covered:
    • Immediate 00b – operand is data (e.g., load 5) • Direct 01b – operand is memory address (e.g., load word at 0x000C)

Select Opcodes (mnemonics shown for clarity)

  • 0000_2 STOP
  • 1100_2 LDWA (load word A)
  • 1101_2 LDBA (load byte A)
  • 1110_2 STWA (store word)
  • 1111_2 STBA (store byte)
  • 0110_2 ADDA (add word)
  • 0111_2 SUBA (subtract word)

Memory-Mapped I/O

  • Input device: address 0xFC15 (read one byte via LD*
  • Output device: address 0xFC16 (write one byte via ST*)

Mini Program Patterns

  • Load immediate 5: LDWA 0x0005,i → accumulator = 5
  • Add immediate 3: ADDA 0x0003,i → accumulator += 3
  • Store direct to 0x000C: STWA 0x000C,d
  • STOP ends execution

"Hello World" Outline (byte version)

  1. LDBA 0x0048,i ; 'H'
  2. STBA 0xFC16,d
  3. Repeat for each ASCII byte ("ello World") then STOP

Assembly Language & Assembler

  • Assembly = mnemonic names + addressing modes; still one-to-one with machine code
  • Assembler translates to object code; pseudo-ops instruct assembler (not CPU)

Pep/9 Key Pseudo-ops

  • .END end of source
  • .ASCII "text\x00" null-terminated string
  • .WORD value allocate word & initialise
  • .BLOCK n reserve n bytes (uninitialised)

Labels & Control Flow

  • Label syntax: name: at start of line; refers to that memory address
  • Branch instructions (use condition flags):
    BR target unconditional
    BRLT target if A < 0
    BREQ target if A = 0
  • Loops formed by subtracting a counter and branching back while condition true Example delay loop:
    1. LDWA 0x0005,i ; iterations
    2. loop: SUBA 0x0001,i
    3. BRGT loop
    4. STOP

Example Programs (conceptual)

  • Add 5 + 3 and store at 0x000C: LDWA 5,iADDA 3,iSTWA 0x000C,dSTOP
  • Echo positive number: read with DECI, output with DECO only if BRGT
  • Range-check 10–19: subtract 10 twice, branch tests, adjust and output
  • Sum two inputs, print error string if sum negative (uses .ASCII, labels, conditional branch)

ASCII Quick Reference

  • 'H' = 0x48, 'e' = 0x65, 'l' = 0x6C, 'o' = 0x6F, space = 0x20

Takeaways

  • Machine code executes directly but is unreadable; assembly provides minimal abstraction with mnemonics, labels, and pseudo-ops
  • Understanding opcode layout and addressing modes is crucial for decoding/encoding instructions
  • Pep/9 offers a compact environment to practise registers, memory, I/O, branching, loops, and assembler workflow