Skip to content

Commit 7aa6ec0

Browse files
committed
Added documentation for stepwise execution. Modified the callback parameters for stepwise execution to look more like the table
1 parent b8d4234 commit 7aa6ec0

File tree

4 files changed

+65
-23
lines changed

4 files changed

+65
-23
lines changed

README.md

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ This project is a small simulator that combines BASIC like line numbers and asse
1313
4. [Instruction Set](#instruction-set)
1414
5. [Documentation](#documentation)
1515
6. [Error Handling](#error-handling)
16+
1617
### Installation
1718

1819
`npm install https://github.com/step/assembly_simulator.git`
@@ -406,20 +407,60 @@ There is also a stack that you can `PUSH` and `POP` from.
406407
4. The line number from the source file or program that was executed in `SL`
407408
5. The actual instruction being executed in `INST`
408409

410+
411+
#### Stepwise Execution
412+
413+
The Machine has the capability of executing stepwise. This feature allows one to 'step through' the program.
414+
415+
```javascript
416+
const machine = new Machine();
417+
let program = fs.readFileSync("add.asm","utf8");
418+
let callBack = (state) => {
419+
let { A, B, C, D } = state;
420+
let { EQ, NE, GT, LT } = state;
421+
let { PRN } = state;
422+
let { CL, NL } = state;
423+
let { SL, INST } = state;
424+
console.log(`A : ${A}, B : ${B}, C : ${C}, D : ${D}`);
425+
console.log(`EQ : ${EQ}, NE : ${NE}, GT : ${GT}, LT : ${LT}`);
426+
console.log(`CL : ${CL}, NL : ${NL}`);
427+
console.log(`SL : ${SL}, INST : ${INST}`);
428+
console.log(`INST : ${INST}`);
429+
}
430+
let executor;
431+
try {
432+
machine.load(program);
433+
executor = machine.executeStepWise();
434+
} catch(e) {
435+
console.log("Error on ", e.lineNumber, e.instruction);
436+
}
437+
438+
try {
439+
machine.nextStep();
440+
machine.nextStep();
441+
} catch(e) {
442+
// do whatever with the error
443+
}
444+
```
445+
446+
As shown above, in order to use it, you are required to pass a callback. The callback will be called with the same fields that `getTable` fetches. The important thing to note here is that once the machine executes the program completely, the callback will no longer be called no matter how many times you call `nextStep`.
447+
448+
This manner of execution is very useful to debug things like infinite loops.
449+
409450
### Error Handling
410451

411-
All errors currently are encapsulated by the `InvalidInstructionException` class. All exceptions have two pieces of information currently.
452+
All errors currently are encapsulated by the `InvalidInstructionException` class. All exceptions have two pieces of information currently.
412453

413-
1. Source line number where the exception occurred in `lineNumber`
414-
2. The offending instruction in `instruction`
454+
1. Source line number where the exception occurred in `lineNumber`
455+
2. The offending instruction in `instruction`
415456

416-
Always surround your machine's `load` and `execute` in try catch blocks.
457+
Always surround your machine's `load` and `execute` in try catch blocks.
417458

418-
```javascript
419-
try {
420-
machine.load(program);
421-
machine.execute();
422-
} catch(e) {
423-
console.log("Error on ", e.lineNumber, e.instruction);
424-
}
425-
```
459+
```javascript
460+
try {
461+
machine.load(program);
462+
machine.execute();
463+
} catch(e) {
464+
console.log("Error on ", e.lineNumber, e.instruction);
465+
}
466+
```

src/lines.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ class Lines {
1515
let { regs, flags, stack } = initState;
1616
let state = { regs, flags, halt: false };
1717
let lineNumbers = this.lines.map(l => l.getLineNumber());
18-
console.log(this.fnTable);
1918
let programCounter = new ProgramCounter(lineNumbers, this.fnTable);
2019
let executor = () => {
2120
let line = this.lines[programCounter.getCurrentLineIndex()];

src/machine.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class Machine {
6969
return this.stack.asArray();
7070
}
7171

72-
_addToTable({ regs, flags, nextLine, currLine, prn, srcLine, instruction }) {
72+
_toRow({ regs, flags, nextLine, currLine, prn, srcLine, instruction }) {
7373
let { A, B, C, D } = regs;
7474
let { EQ, NE, GT, LT } = flags;
7575
let row = {
@@ -87,7 +87,11 @@ class Machine {
8787
SL: srcLine,
8888
INST: instruction
8989
};
90-
this.table.push(row);
90+
return row;
91+
}
92+
93+
_addToTable(state) {
94+
this.table.push(this._toRow(state));
9195
}
9296

9397
getTable() {
@@ -139,7 +143,7 @@ class Machine {
139143
this._reset();
140144
let wrappedCb = state => {
141145
this._updateCurrentExecState(state);
142-
cb(state);
146+
cb(this._toRow(state));
143147
};
144148
let regs = this.getRegs();
145149
let flags = this.getFlags();

test/testMachine.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,7 @@ describe('Machine step wise execution', () => {
128128
const machine = new Machine();
129129
const program = ['10 START', '30 MOV A,10', '40 STOP'];
130130
let actualRegs = {};
131-
const cb = state => {
132-
let { A, B, C, D } = state.regs;
131+
const cb = ({ A, B, C, D}) => {
133132
actualRegs = { A, B, C, D };
134133
};
135134
machine.load(stitch(program));
@@ -146,11 +145,10 @@ describe('Machine step wise execution', () => {
146145
const machine = new Machine();
147146
const program = ['10 START', '30 JMP 50', '40 MOV A,10', '50 STOP'];
148147
let actualCurrLine, actualNextLine, actualRegs;
149-
const cb = ({ nextLine, currLine, regs }) => {
150-
let { A, B, C, D } = regs;
148+
const cb = ({ NL, CL, A, B, C, D }) => {
151149
actualRegs = { A, B, C, D };
152-
actualCurrLine = currLine;
153-
actualNextLine = nextLine;
150+
actualCurrLine = CL;
151+
actualNextLine = NL;
154152
};
155153
machine.load(stitch(program));
156154
machine.executeStepWise(cb);
@@ -179,7 +177,7 @@ describe('Machine step wise execution', () => {
179177
'60 STOP'
180178
];
181179
let actualFlags;
182-
let cb = ({ flags }) => (actualFlags = flags);
180+
let cb = ({ EQ, NE, GT, LT }) => (actualFlags = { EQ, NE, GT, LT});
183181
machine.load(stitch(program));
184182
machine.executeStepWise(cb);
185183
machine.nextStep();

0 commit comments

Comments
 (0)