Play free

2000s · Yale Patt and Sanjay Patel

LC-3

The LC-3, short for Little Computer 3, is the machine thousands of students meet in their first computer systems course. It is small enough to learn in a week, but complete enough to run real programs with subroutines, I/O and an operating system’s worth of service routines.

3 min read · Updated

Designed byYale N. Patt and Sanjay J. Patel
BookIntroduction to Computing Systems: From Bits and Gates to C and Beyond
IntroducedWith the book’s second edition (early 2000s), succeeding the LC-2
Word size16 bits; memory is word-addressed
Address space65,536 words (x0000 to xFFFF)
RegistersR0 to R7, PC, IR, condition codes N, Z, P
Opcodes15 (4-bit opcode field; one value reserved)
I/OMemory-mapped keyboard and display registers

Why it was built

Yale Patt and Sanjay Patel believe students should learn computing from the bottom up: transistors, then gates, then a real machine, then assembly, then C. Most real processors are too big and too messy for a first course, so they designed their own. The LC-3 keeps the parts that teach something and drops the rest. Their textbook builds up to it chapter by chapter, and many universities use it in introductory courses.

How it works

Everything is 16 bits: the words, the addresses and the instructions. Memory holds 65,536 words, and programs usually start at address x3000 (LC-3 writes hex with a leading x). There are eight general-purpose registers, R0 to R7. After any instruction that writes a register, three condition codes record whether the result was Negative, Zero or Positive, and conditional branches test them.

Each instruction uses its top four bits as the opcode. That leaves 12 bits for operands, which is tight, so many instructions use a short signed offset from the program counter rather than a full address.

GroupInstructions
OperateADD, AND, NOT
Data movementLD, LDI, LDR, LEA, ST, STI, STR
ControlBR (with any mix of n, z, p), JMP/RET, JSR/JSRR, TRAP, RTI

The operate group is famously minimal: add, AND and NOT. There is no subtract and no OR. To compute a − b you flip b with NOT, add 1, then add, which is two’s complement in action. OR comes from NOT and AND, using De Morgan’s law. Students learn quickly that a few well-chosen operations go a long way (see logic gates and truth tables).

The data movement instructions show off addressing modes: PC-relative (LD, ST), indirect through a pointer in memory (LDI, STI), base plus offset (LDR, STR), and LEA, which computes an address without touching memory.

TRAPs and I/O

Input and output use memory-mapped device registers: the keyboard status and data registers at xFE00 and xFE02, the display status and data registers at xFE04 and xFE06. Programs usually do not poke them directly, though. They call system service routines with TRAP:

TrapNameWhat it does
x20GETCRead one character into R0
x21OUTPrint the character in R0
x22PUTSPrint the string R0 points to
x23INPrompt, then read and echo a character
x25HALTStop the machine

That gives students a first taste of an operating system: code they did not write, reached through a table, running on their behalf.

Why it matters

The LC-3 is a common first assembly language for computer engineering students, and the book continues past it to show how C maps onto the same machine: the stack, local variables, function calls. A third edition extends the story to C++. Free simulators make it easy to try at home.

The LC-3 in NANDSIM

The museum LC-3 is rebuilt from NAND gates and runs real LC-3 programs. Open it up to inspect R0 to R7, the condition codes and memory as each instruction executes.

Run the LC-3 live