a lightweight LC-3 (Little Computer 3) emulator written in C
gcc -Wall -Wextra -o lc3 src/main.c
./lc3 path/to/your/program.obj- Full LC-3 instruction set
.objimage loading- 16-bit memory and register model
- Memory-mapped keyboard I/O
- Non-blocking input using
select - Terminal input control via
termios - Built-in trap handling (
GETC,OUT,PUTS,IN,PUTSP,HALT)
- General-purpose registers:
R0–R7 - Program Counter:
PC - Condition Register:
COND
- 16-bit address space
- Memory-mapped registers:
KBSR(Keyboard Status Register)KBDR(Keyboard Data Register)
The emulator follows a standard fetch–decode–execute loop:
- Fetch instruction from memory using
PC - Decode opcode and operands
- Execute instruction
- Update condition flags
- Advance
PC
- Arithmetic:
ADD,AND,NOT - Control flow:
BR,JMP,JSR,JSRR - Memory access:
LD,LDI,LDR,LEA,ST,STI,STR - System calls:
TRAP
The emulator implements LC-3 trap routines directly in the VM:
| Trap | Description |
|---|---|
GETC |
Read a single character |
OUT |
Output a character |
PUTS |
Output a null-terminated string |
IN |
Prompt and read a character |
PUTSP |
Output packed characters |
HALT |
Stop execution |
I will write about it here.