Skip to content

Commit

Permalink
Expose label->address mapping from the assembler and test it
Browse files Browse the repository at this point in the history
  • Loading branch information
eliben committed Jul 14, 2020
1 parent 68ee3ce commit cd1781b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
3 changes: 2 additions & 1 deletion sample/sample.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ let p = new Parser();
let sl = p.parse(prog);
let asm = new Assembler();
asm.setTracing(true);
let mem = asm.assemble(sl);
let [mem, labelToAddr] = asm.assemble(sl);
console.log(labelToAddr);

// Set up memory access functions for the simulator.
function memoryTo(addr, value) {
Expand Down
2 changes: 1 addition & 1 deletion src/assembler.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Assembler {
assemble(sourceLines) {
this._assembleInstructions(sourceLines);
this._applyFixups();
return this.memory;
return [this.memory, this.labelToAddr];
}

// Set tracing mode: true or false;
Expand Down
27 changes: 25 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function runProg(progText, maxSteps) {
let p = new Parser();
let asm = new Assembler();
let sourceLines = p.parse(progText);
let mem = asm.assemble(sourceLines);
let [mem, labelToAddr] = asm.assemble(sourceLines);

const memoryTo = (addr, value) => {mem[addr] = value;};
const memoryAt = (addr) => {return mem[addr];};
Expand All @@ -35,7 +35,7 @@ function runProg(progText, maxSteps) {
}
}

return [CPU8080.status(), mem];
return [CPU8080.status(), mem, labelToAddr];
}

describe('sim', () => {
Expand Down Expand Up @@ -121,6 +121,29 @@ describe('sim', () => {
assert.equal(state.a, 100);
});

it('labels-to-addr', () => {
// Verifies that labels have been mapped onto addresses as expected.
let [state, mem, labelToAddr] = runProg(`
Start:
mvi a, 50 ; 2 bytes each
mvi b, 20
mvi c, 100
jmp Uno ; 3 bytes
Tres: add b ; 1 bytes for add/hlt each
hlt
Uno: jmp Dos
add c
Dos: jmp Tres
add c
`);
assert.ok(state.halted);

assert.equal(labelToAddr.get('Start'), 0);
assert.equal(labelToAddr.get('Tres'), 9);
assert.equal(labelToAddr.get('Uno'), 11);
assert.equal(labelToAddr.get('Dos'), 15);
});

it('chainjmp', () => {
let [state, mem] = runProg(`
mvi a, 50
Expand Down

0 comments on commit cd1781b

Please sign in to comment.