A two-pass assembler written in C for an 8-bit computer built from scratch. This is the software component of a custom 8-bit computer project, translating assembly language into machine code with binary output visualization.
This assembler is designed specifically for a custom-built 8-bit computer architecture. It translates assembly language programs into executable machine code that can be loaded into the computer's memory. The assembler uses a two-pass approach to resolve labels and generate the final binary output.
- Two-Pass Assembly: First pass collects label addresses, second pass generates machine code
- Label Support: Define and reference labels for jumps and data
- Binary Visualization: Outputs machine code in both hexadecimal and binary format
- Comment Support: Lines can include semicolon (
;) comments - Error Handling: Detects unknown labels and provides error messages
- Hardware-Ready Output: Generates machine code that can be directly loaded into the 8-bit computer's ROM/RAM
The instruction set is designed to match the custom 8-bit computer's architecture:
| Mnemonic | Opcode | Size | Description |
|---|---|---|---|
LDA |
0x01 | 2 | Load accumulator from memory/immediate |
STA |
0x02 | 2 | Store accumulator to memory |
ADD |
0x03 | 2 | Add to accumulator |
SUB |
0x04 | 2 | Subtract from accumulator |
JMP |
0x05 | 2 | Unconditional jump to address |
JZ |
0x06 | 2 | Jump if zero flag is set |
OUT |
0x07 | 1 | Output accumulator value |
HLT |
0xFF | 1 | Halt execution |
label_name:LDA 10 ; Load value 10
ADD 0x05 ; Add hexadecimal value
JMP start ; Jump to label 'start'
OUT ; Output (no operand)
HLT ; Halt (no operand); This is a comment
LDA 5 ; Load 5 into accumulatorCreate a file named program.asm:
; Simple counter program
start:
LDA 0 ; Load 0
STA 20 ; Store at address 20
loop:
LDA 20 ; Load counter
ADD 1 ; Increment
STA 20 ; Store back
SUB 10 ; Compare with 10
JZ end ; Jump if zero
JMP loop ; Continue loop
end:
OUT ; Output result
HLT ; Halt; Generate Fibonacci sequence
LDA 0 ; First number
OUT
STA 30 ; Store in memory
LDA 1 ; Second number
OUT
STA 31 ; Store in memory
loop:
LDA 30 ; Load first number
ADD 31 ; Add second number
OUT ; Display result
STA 32 ; Store result
LDA 31 ; Move second to first
STA 30
LDA 32 ; Move result to second
STA 31
JMP loop ; Continuegcc assembler.c -o assemblerOr with warnings enabled:
gcc -Wall -Wextra assembler.c -o assembler- Create your assembly program in a file named
program.asm - Run the assembler:
./assembler
- View the generated machine code output
- Load the machine code into your 8-bit computer's memory
The assembler displays the machine code with:
- Memory address (hexadecimal)
- Binary representation of each byte (ready for ROM programming)
- Total program size
Example output:
===== MACHINE CODE OUTPUT =====
0x00 00000001
0x01 00000000
0x02 00000010
0x03 00010100
...
Program size: 16 bytes
The binary output can be used to:
- Program EEPROMs for the computer's ROM
- Load into RAM during testing
- Verify hardware implementation
Pass 1: Symbol Resolution
- Scans the source file to identify labels
- Records the address of each label
- Calculates instruction positions
- Validates label syntax
Pass 2: Code Generation
- Generates machine code for each instruction
- Resolves label references to actual addresses
- Produces the final binary output
- Validates operand ranges
This assembler targets an accumulator-based 8-bit computer with:
- 8-bit data bus: All data operations are 8 bits wide
- 8-bit address space: 256 bytes of addressable memory (0x00-0xFF)
- Accumulator register: Primary register for arithmetic operations
- Zero flag: Set when accumulator result is zero
- Program counter: Automatic instruction sequencing
- Von Neumann architecture: Unified memory for code and data
- Maximum program size: 256 bytes
- Maximum labels: 64
- Maximum line length: 128 characters
- Operands are 8-bit values (0-255)
- No stack operations (can be added as future enhancement)
This assembler is part of a complete 8-bit computer system that includes:
- ALU (Arithmetic Logic Unit)
- Registers (Accumulator, Program Counter)
- Control Unit
- Memory (RAM/ROM)
- Clock circuit
- Input/Output interfaces
- Assembler (this tool)
- Assembly language specification
- Example programs
- Testing utilities
- Write assembly program (
program.asm) - Assemble to machine code (this tool)
- Program EEPROM with machine code
- Install EEPROM in 8-bit computer
- Run program on hardware
The assembler will report errors for:
- Missing
program.asmfile - Unknown label references
- Invalid instruction mnemonics
- Invalid operand formats
.
├── assembler.c # Main assembler source code
├── program.asm # Input assembly program
└── README.md # This file
This assembler is the software toolchain for a custom 8-bit computer built from scratch. The hardware implementation includes discrete logic chips, registers, ALU, and control circuitry, while this assembler provides the means to write and compile programs for the system.
- Learn computer architecture from the ground up
- Understand how software and hardware interact
- Build a fully functional computer from basic components
- Create a complete development toolchain
This project is open source and available for educational purposes.
Contributions are welcome! Feel free to:
- Report bugs
- Suggest new instructions for the instruction set
- Improve error handling
- Add new features
- Share programs written for this architecture
Soorya A P
Part of the 8-bit computer from scratch project.
- Ben Eater's 8-bit computer series
- "The Elements of Computing Systems" (nand2tetris)
- Digital logic design fundamentals
- Assembly language programming concepts
- Hardware schematics (coming soon)
- Instruction set simulator
- Debugger/emulator
- Additional example programs
- Support for directives (
.org,.byte,.word,.data) - Multiple operand addressing modes (immediate, direct, indirect)
- Macro support for code reuse
- Output to binary file for EEPROM programming
- More detailed error messages with line numbers
- Symbol table output and listing files
- Disassembler functionality
- Stack operations (PUSH, POP)
- More conditional jumps (JC, JN, JNZ)
- Bitwise operations (AND, OR, XOR, NOT)
- Input instruction (IN)
- Shift/rotate instructions
- Call/return for subroutines
- Computer emulator/simulator
- Interactive debugger
- Memory viewer
- Step-through execution
- Automated testing framework
Building computers from scratch, one instruction at a time. ⚡