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:
objectResult of assembling source code.
- Parameters:
- 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:
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:
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:
Note
The file is opened in text mode and read entirely into memory.
Example
>>> result = assemble_file("program.asm")