tiny8.assembler module

Simple assembler for tiny8 - converts assembly text into program tuples.

This module provides a tiny assembler that tokenizes and parses a simple assembly syntax and returns a list of instructions and a label mapping.

class tiny8.assembler.AsmResult(program=<factory>, labels=<factory>, pc_to_line=<factory>, source_lines=<factory>)[source]

Bases: object

Result of assembling source code.

Parameters:
program

List of instruction tuples (mnemonic, operands).

Type:

list[tuple[str, tuple]]

labels

Mapping from label names to program counter addresses.

Type:

dict[str, int]

pc_to_line

Mapping from program counter to original source line number.

Type:

dict[int, int]

source_lines

Original source text split into lines for display.

Type:

list[str]

program: list[tuple[str, tuple]]
labels: dict[str, int]
pc_to_line: dict[int, int]
source_lines: list[str]
__init__(program=<factory>, labels=<factory>, pc_to_line=<factory>, source_lines=<factory>)
Parameters:
Return type:

None

tiny8.assembler.parse_asm(text)[source]

Parse assembly source text into a program listing and a label table.

The function scans the given assembly text line-by-line and produces an AsmResult containing the parsed program, labels, source line mapping, and original source text.

Parameters:

text (str) – Assembly source code as a single string. May contain comments, blank lines and labels.

Returns:

AsmResult object containing program, labels, pc_to_line mapping, and source_lines.

Return type:

AsmResult

Note

  • Comments begin with ‘;’ and extend to the end of the line.

  • Labels may appear on a line by themselves or before an instruction with the form “label: instr …”.

  • Registers are encoded as (“reg”, N) where N is the register number.

  • Numeric operands are parsed using _parse_number; non-numeric tokens are preserved as strings (symbols) for later resolution.

tiny8.assembler.assemble(text)[source]

Parse assembly source text and return parsed instructions and label map.

Parameters:

text (str) – Assembly source code as a single string. May contain multiple lines, labels and comments.

Returns:

AsmResult object containing program, labels, source line mapping, and original source text.

Raises:

Exception – Propagates parsing errors from the underlying parser.

Return type:

AsmResult

Example

>>> src = "start: MOV R1, 5\nJMP start"
>>> result = assemble(src)
>>> result.labels
{'start': 0}
tiny8.assembler.assemble_file(path)[source]

Assemble the contents of a source file.

Parameters:

path (str) – Path to the source file to assemble.

Returns:

The result produced by calling assemble(source_text).

Raises:
  • FileNotFoundError – If the specified file does not exist.

  • OSError – For other I/O related errors when opening or reading the file.

  • Exception – Any exception raised by assemble(…) will be propagated.

Return type:

AsmResult

Note

The file is opened in text mode and read entirely into memory.

Example

>>> result = assemble_file("program.asm")