Skip to content

Commit

Permalink
fixed bug causing crash on last instruction (sometimes)
Browse files Browse the repository at this point in the history
  • Loading branch information
christo committed Dec 23, 2024
1 parent 445740a commit f446ca3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# TODO

* [ ] fix crash disassembling hesmon - happens at last byte
* [x] fix crash disassembling hesmon - happens at last byte
* [ ] remove excessive cleverness in low branches of front-end disassembly render call tree
* [ ] implement Tracer and tests
* [x] Tracer can execute simplest instruction traces, recording executed indices
Expand Down
1 change: 1 addition & 0 deletions client/src/machine/asm/DefaultDialect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class DefaultDialect implements Dialect {
}

parseLine(line: string, parserState: ParserState): [InstructionLike, ParserState] {
// work in progres - assembler
if (parserState === ParserState.READY) {
// LINE_BEGIN [label] [instruction | directive] [comment] LINE_END
const m = line.match(`^([A-Za-z_]\w*:)?\h*()?\h*(\\\*.*\*/|;.*)$`)
Expand Down
29 changes: 24 additions & 5 deletions client/src/machine/asm/Disassembler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,14 @@ class Disassembler {
} else {
// if there are not enough bytes for this whole instruction, return a ByteDeclaration for this byte
// we don't yet know if an instruction will fit for the next byte
const instLen = Mos6502.ISA.numBytes(opcode) || 1;
const instLen = this.iset.numBytes(opcode) || 1;

if (this.bytesLeftInFile() < instLen) {
const bytesLeft = this.bytesLeftInFile();
if (bytesLeft < instLen) {
lc.addComments("instruction won't fit");
instLike = new ByteDeclaration(this.eatBytes(1), lc);
} else {
// console.log(`bytes left: ${bytesLeft} instruction len: ${instLen}`);
instLike = this.edictAwareInstruction(opcode, lc);
}
}
Expand Down Expand Up @@ -146,8 +148,26 @@ class Disassembler {
}
}
// by now we know we must consume the current byte
this.currentIndex++;
return this.mkInstruction(currentByte, lc);
{
const numInstructionBytes = this.iset.numBytes(currentByte) || 1
const bytesRemaining = this.fb.getBytes().length - this.currentIndex;
if (numInstructionBytes <= bytesRemaining) {
// default operands are 0
let firstOperandByte = 0;
let secondOperandByte = 0;
if (numInstructionBytes === 2) {
firstOperandByte = this.fb.read8(this.currentIndex + 1);
} else if (numInstructionBytes === 3) {
secondOperandByte = this.fb.read8(this.currentIndex + 2);
}
const il = new FullInstruction(this.iset.instruction(currentByte), firstOperandByte, secondOperandByte);
this.currentIndex += (numInstructionBytes); // already consumed opcode
return new FullInstructionLine(il, lc);
} else {
console.error(`bytes remaining: ${bytesRemaining} instruction bytes: ${numInstructionBytes}`);
throw Error(`Not enough bytes to disassemble instruction at index ${this.currentIndex}`);
}
}
}

hasNext() {
Expand Down Expand Up @@ -304,7 +324,6 @@ class Disassembler {
this.currentIndex += (numInstructionBytes - 1); // already consumed opcode
return new FullInstructionLine(il, labelsComments);
} else {
debugger;
console.error(`bytes remaining: ${bytesRemaining} instruction bytes: ${numInstructionBytes}`);
throw Error(`Not enough bytes to disassemble instruction at index ${this.currentIndex}`);
}
Expand Down

0 comments on commit f446ca3

Please sign in to comment.