Skip to content

Commit

Permalink
First instruction has now also 0 as adress
Browse files Browse the repository at this point in the history
+ Minor code style improvements
  • Loading branch information
dgc08 committed Jun 15, 2024
1 parent 9edb7c9 commit 4325810
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
4 changes: 2 additions & 2 deletions js/flowchart.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function generateFlowChartCode() {
code = `start=>start: Start\n`;

source.forEach((cmd, index) => {
const line = index + 1;
const line = index;
let nodetype = 'operation';

if (cmd.startsWith('dec')) {
Expand All @@ -75,7 +75,7 @@ function generateFlowChartCode() {

code += `\nstart->node1\n`;
source.forEach((cmd, index) => {
const line = index + 1;
const line = index;

if (cmd.startsWith('tst')) {
code += `node${line}(yes)->node${line + 1}\n`;
Expand Down
18 changes: 11 additions & 7 deletions js/simulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ function executeLine() {
shouldExecute = false;
return;
}
if (line > code.length) {
if (line > code.length-1) {
shouldExecute = false;
return;
}

// setup pointer
const pointer = document.getElementById('pointer');

const content = code[line - 1];
const content = code[line];
const cmd = content.substring(0, 3);
const param = parseInt(content.substring(3, content.length));

Expand All @@ -74,7 +74,7 @@ function executeLine() {
for (let i = 0; i < lines.length; i++) {
const lineContent = lines[i];
if (lineContent != '' && !lineContent.startsWith(';')) linesOfCode++; // check if this line is code
if (linesOfCode == line) {
if (linesOfCode-1 == line) {
pointer.style.top = i * 20 - 4 + 'pt';
break;
}
Expand All @@ -84,11 +84,16 @@ function executeLine() {
switch (cmd) {
case 'jmp':
line = param;
break;
return;

case 'tst':
// If register doesnt exist or is null, skip next line
if (registers[param] == null || registers[param] == 0) line++;
checkRegisterCount(param);
if (registers[param]==0)
line++;
if (registers[param] == null){
line++;
registers[param] = 0;
}
break;

case 'hlt':
Expand Down Expand Up @@ -117,7 +122,6 @@ async function execute() {
processLine();
await sleep(sleepDuration);
}
await sleep(sleepDuration);
resetSimulator();
}

Expand Down

0 comments on commit 4325810

Please sign in to comment.