eight_bit_computer.assembler module

Process assembly code and output machine code.

eight_bit_computer.assembler.process_assembly_lines(lines, variable_start_offset=0)[source]

Parse, assemble and generate machine code.

Parameters:
  • lines (list(str)) – The lines that made up the assembly file to be assembled.
  • variable_start_offset (int) (optional) – How far to offset the first variable in data memory from 0.
Returns:

The assembly file converted to an equivalent list of dictionaries with information about what each line was resolved to.

Return type:

list(dict)

Raises:

AssemblyError – If there was an error assembling the machine code.

eight_bit_computer.assembler.process_line(line)[source]

Process a single line of assembly.

Parameters:line (str) – The line of assembly to process. This line has already been cleaned (excess whitespace and comments removed).
Returns:A dictionary of information about this line. See the get_assembly_line_template() documentation for more information about what is in the dictionary.
Return type:dict
eight_bit_computer.assembler.clean_line(line)[source]

Clean a line of assembly ready for further processing.

Removes leading and trailing whitespace, comments, and excess whitespace between tokens.

Parameters:line (str) – The line to clean.
Returns:The cleaned line.
Return type:str
eight_bit_computer.assembler.remove_comments(line)[source]

Remove comments from a line.

A comment is anything on the line after and including an occurrence of //.

Parameters:line (str) – line to remove comments from.
Returns:The line with comments removed.
Return type:str
eight_bit_computer.assembler.remove_excess_whitespace(line)[source]

Remove excess whitespace from a line.

Parameters:line (str) – line to remove excess whitespace from.
Returns:The line with excess whitespace removed.
Return type:str
eight_bit_computer.assembler.machine_code_bytes_from_line(line)[source]

Get machine code bytes that describe this line.

Uses all the defined instructions and defers the work of parsing to them. See get_machine_code_byte_template() for information on machine code dictionaries from instructions.

Expects the passed in line to be a valid line of machine code. That is, the passed in line should be translatable to valid machine code.

Parameters:

line (str) – Line to parse.

Returns:

Machine code byte information dictionaries.

Return type:

list(dict)

Raises:
  • LineProcessingError – Failure to extract machine code or matching
  • multiple operations.
eight_bit_computer.assembler.validate_and_identify_constants(machine_code_bytes)[source]

Validate and identify constants from assembly code.

Assumed constants are returned from the instruction parsers. This function then validates them to make sure they are correct and determines what kind of constant they are.

See get_mc_byte_template() for information on machine code dictionaries from instructions.

This function modifies the passed in machine code templates list in place.

Parameters:machine_code_bytes (list(dict)) – The machine code byte dicts as returned by an instruction line parser.
Raises:LineProcessingError – Invalid constants were specified.
eight_bit_computer.assembler.assign_machine_code_byte_indexes(assembly_lines)[source]

Assign indexes to the machine code bytes.

This modifies the passed in list of assembly lines, adding data to it.

Parameters:
  • assembly_lines (list(dict)) – Lines of assembly to add label
  • to. (information) –
eight_bit_computer.assembler.assign_labels(assembly_lines)[source]

Assign labels to the lines for later reference

This modifies the passed in list of assembly lines, adding data to it.

Parameters:
  • assembly_lines (list(dict)) – Lines of assembly to add label
  • to. (information) –
eight_bit_computer.assembler.resolve_labels(assembly_lines)[source]

Resolve labels to indexes in the machine code bytes.

This modifies the passed in list of assembly line dictionaries.

Parameters:assembly_lines (list(dict)) – List of assembly lines to resolve label references in.
eight_bit_computer.assembler.create_label_map(assembly_lines)[source]

Create a map of labels to machine code byte indexes.

Parameters:assembly_lines (list(dict)) – List of assembly lines to create a label map for.
Returns:str): Dictionary of label names to machine code indexes.
Return type:dict(str
eight_bit_computer.assembler.resolve_numbers(assembly_lines)[source]

Resolve number constants to machine code byte values.

This modifies the passed in list of assembly line dictionaries.

Parameters:assembly_lines (list(dict)) – List of assembly lines to resolve numbers for.
eight_bit_computer.assembler.resolve_variables(assembly_lines, variable_start_offset)[source]

Resolve variable constants to indexes in data memory.

This modifies the passed in list of assembly line dictionaries.

Parameters:
  • assembly_lines (list(dict)) – List of assembly lines to resolve variables in.
  • variable_start_offset (int) – An offset into data memory for where to start storing the variables.
eight_bit_computer.assembler.create_variable_map(assembly_lines, variable_start_offset)[source]

Create a map of variables to indexes in data memory.

Parameters:
  • assembly_lines (list(dict)) – List of assembly lines to create a variable map for.
  • variable_start_offset (int) – An offset into data memory for where to start storing the variables.
Returns:

str): Dictionary of variable names to machine code indexes.

Return type:

dict(str