Skip to content

Soorya005/Assembler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

8-Bit Computer Assembler

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.

Overview

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.

Features

  • 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

Supported Instructions

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

Assembly Language Syntax

Labels

label_name:

Instructions

LDA 10      ; Load value 10
ADD 0x05    ; Add hexadecimal value
JMP start   ; Jump to label 'start'
OUT         ; Output (no operand)
HLT         ; Halt (no operand)

Comments

; This is a comment
LDA 5  ; Load 5 into accumulator

Example Programs

Simple Counter Program

Create 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

Fibonacci Sequence

; 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    ; Continue

Compilation

gcc assembler.c -o assembler

Or with warnings enabled:

gcc -Wall -Wextra assembler.c -o assembler

Usage

  1. Create your assembly program in a file named program.asm
  2. Run the assembler:
    ./assembler
  3. View the generated machine code output
  4. Load the machine code into your 8-bit computer's memory

Output Format

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

Technical Details

Two-Pass Assembly Process

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

8-Bit Computer Architecture

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

Limitations

  • 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)

Hardware Integration

This assembler is part of a complete 8-bit computer system that includes:

Hardware Components (Hardware Part)

  • ALU (Arithmetic Logic Unit)
  • Registers (Accumulator, Program Counter)
  • Control Unit
  • Memory (RAM/ROM)
  • Clock circuit
  • Input/Output interfaces

Software Components (This Project)

  • Assembler (this tool)
  • Assembly language specification
  • Example programs
  • Testing utilities

Workflow: From Code to Hardware

  1. Write assembly program (program.asm)
  2. Assemble to machine code (this tool)
  3. Program EEPROM with machine code
  4. Install EEPROM in 8-bit computer
  5. Run program on hardware

Error Handling

The assembler will report errors for:

  • Missing program.asm file
  • Unknown label references
  • Invalid instruction mnemonics
  • Invalid operand formats

Project Structure

.
├── assembler.c      # Main assembler source code
├── program.asm      # Input assembly program
└── README.md        # This file

8-Bit Computer Project

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.

Project Goals

  • 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

License

This project is open source and available for educational purposes.

Contributing

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

Author

Soorya A P

Part of the 8-bit computer from scratch project.

Resources

Recommended Learning

  • Ben Eater's 8-bit computer series
  • "The Elements of Computing Systems" (nand2tetris)
  • Digital logic design fundamentals
  • Assembly language programming concepts

Related Projects

  • Hardware schematics (coming soon)
  • Instruction set simulator
  • Debugger/emulator
  • Additional example programs

Future Enhancements

Assembler Features

  • 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

Instruction Set Extensions

  • 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

Development Tools

  • Computer emulator/simulator
  • Interactive debugger
  • Memory viewer
  • Step-through execution
  • Automated testing framework

Building computers from scratch, one instruction at a time.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors