Skip to content

Commit

Permalink
Add support for the 'dw' directive
Browse files Browse the repository at this point in the history
  • Loading branch information
eliben committed Jul 16, 2020
1 parent b6d61a3 commit f4c5370
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ Links:
* Online assembler with "live" assmelby:
https://svofski.github.io/pretty-8080-assembler/
* Online assembler: https://www.asm80.com/onepage/asm8080.html
* Intel 8080-8085 assembly manual:
https://www.tramm.li/i8080/Intel%208080-8085%20Assembly%20Language%20Programming%201977%20Intel.pdf
14 changes: 14 additions & 0 deletions src/assembler.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class Assembler {
this.tracing = false;
}

// Assembles the given source lines. Returns [memory, labelToAddr].
// memory is the assembled memory array. labelToAddr is a Map object mapping
// labels to addresses in memory.
assemble(sourceLines) {
this._assembleInstructions(sourceLines);
this._applyFixups();
Expand Down Expand Up @@ -189,6 +192,17 @@ class Assembler {
// Pseudo-instruction that simply assigns its immediate args to memory.
return sl.args.map((arg) => {return this._argImm(sl, arg);});
}
case 'dw': {
// Pseudo-instruction that treats immediate args as 2-byte words, and
// assigns them to memory in little-endian.
let enc = [];
for (let arg of sl.args) {
let argEnc = this._argImm(sl, arg);
enc.push(argEnc & 0xFF);
enc.push((argEnc >> 8) & 0xFF);
}
return enc;
}
case 'dcr': {
this._expectArgsCount(sl, 1);
let r = this._argR(sl, sl.args[0]);
Expand Down
16 changes: 16 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,22 @@ describe('sim', () => {
assert.equal(state.a, 33);
});

it('array-dw', () => {
let [state, mem, labelToAddr] = runProg(`
hlt
myArray:
dw 2030h, 5060h
`);

assert.ok(state.halted);
let arrAddr = labelToAddr.get('myArray');
assert.equal(mem[arrAddr], 0x30);
assert.equal(mem[arrAddr+1], 0x20);
assert.equal(mem[arrAddr+2], 0x60);
assert.equal(mem[arrAddr+3], 0x50);
});

it('mult', () => {
let [state, mem] = runProg(`
mvi b, 44
Expand Down

0 comments on commit f4c5370

Please sign in to comment.