lc2asm.js Documentation

<- Back to the documentation index

Methods

lc2.lex(str)
Given an input string containing valid LC-2 assembler code, returns an array of Lexeme objects as described in the lexer.js documentation.
lc2.parse(lexemes)
Given an input array of Lexeme objects corresponding to a valid LC-2 assembler code, return a Code object containing only the lines attribute. Note that this step also converts lexemes of type 'NUM' to have val attribute equal to the value of the number, and converts lexemes of type 'REG' to have val attribute equal to the value of the register.
lc2.run_directives(ob)
Given a Code object with the lines attribute, run and remove each of the assembler directive lines. At the end of this method, the only remaining lines will be those with CPU instructions each of which will have an address property which gives its calculated address in memory. Additionally, any bytecode or symbols introduced by assembler directives will appear in the bytecode or symbols attributes respectively.
lc2.build_symbol_table(ob)
Given a Code object with the lines attribute containing no assembler directives, build the symbol table mapping symbols in the assembler source to final memory locations and add this to the Code object as the symbol attribute.
lc2.translate(ob)
Given a Code object with lines and symbol attributes, translate the lines to a mapping from memory locations to bytes corresponding to the lines of code. This method returns only the mapping from memory locations to bytes.
lc2.assemble(str)
Given a string containing LC-2 assembly code, return an object mapping memory locations to the bytecode representation of the code, suitable for loading into the lc2.js simulator with lc2.load_program(prg) in lc2.js.

Code Object Properties

lines
Lexemes arranged by line in the source file.
symbols
A mapping from symbol strings to final memory locations.
bytecode
A mapping from memory locations to bytes corresponding to the lines of code.