This is my input file named test1:

00 READ 9 01 READ 10 02 LOAD 9 03 SUB 10 04 BRNG 7 05 WRIT 9 06 HALT 99 07 WRIT 10 08 HALT 99 09 SET 0 10 SET 0

These commands are supposed to read two values from the user and outputs the larger of the two.

I compiled on unix using the command:

gcc -Wall -ansi -pedantic -std=c99 computer.c

and the program compiles just fine.

I ran it using the command:

./a.out < test1

Here is my header file named computer.h:

int dataDump (int memory [], int* accumulator, int* instructionCounter, int* instructionRegister, int* operationCode, int* operand); int compile (int memory [], int* accumulator, int* instructionCounter, int* instructionRegister, int* operationCode, int* operand); int execute (int memory [], int* accumulator, int* instructionCounter, int* instructionRegister, int* operationCode, int* operand);

Here is my source code file named computer.c:

#include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #include "computer.h" int main () { int memory [100] = {0}; int accumulator = 0; int instructionCounter = 0; int instructionRegister = 0; int operationCode = 0; int operand = 0; compile(memory, &accumulator, &instructionCounter, &instructionRegister, &operationCode, &operand); execute(memory, &accumulator, &instructionCounter, &instructionRegister, &operationCode, &operand); return 0; } //This function works correctly int compile (int memory [], int* accumulator, int* instructionCounter, int* instructionRegister, int* operationCode, int* operand) { char word[4]; while ((*operationCode = scanf("%d %s %d", accumulator, word, operand)) > 0) { if (strncmp("SET", word, 4) != 0 && *operand > 99) { printf("ERROR Word overflow"); exit(1); } if (strncmp("SET", word, 4) == 0 && *operand > 9999) { printf("ERROR Word overflow"); exit(1); } if (strncmp("READ", word, 4) == 0) memory[*accumulator] = 1000 + *operand; else if (strncmp("WRIT", word, 4) == 0) memory[*accumulator] = 1100 + *operand; else if (strncmp("PRNT", word, 4) == 0) memory[*accumulator] = 1200 + *operand; else if (strncmp("LOAD", word, 4) == 0) memory[*accumulator] = 2000 + *operand; else if (strncmp("STOR", word, 4) == 0) memory[*accumulator] = 2100 + *operand; else if (strncmp("SET", word, 3) == 0) memory[*accumulator] = *operand; else if (strncmp("ADD", word, 3) == 0) memory[*accumulator] = 3000 + *operand; else if (strncmp("SUB", word, 3) == 0) memory[*accumulator] = 3100 + *operand; else if (strncmp("DIV", word, 3) == 0) memory[*accumulator] = 3200 + *operand; else if (strncmp("MULT", word, 4) == 0) memory[*accumulator] = 3300 + *operand; else if (strncmp("MOD", word, 3) == 0) memory[*accumulator] = 3400 + *operand; else if (strncmp("BRAN", word, 4) == 0) memory[*accumulator] = 4000 + *operand; else if (strncmp("BRNG", word, 4) == 0) memory[*accumulator] = 4100 + *operand; else if (strncmp("BRZR", word, 4) == 0) memory[*accumulator] = 4200 + *operand; else if (strncmp("HALT", word, 4) == 0) { memory[*accumulator] = 9900 + *operand; *instructionCounter = 1; } // add HALT error and word not found error } if (*operationCode == 0) { printf("ERROR Undefined use"); exit(1); } if (*instructionCounter == 0) { printf("ERROR No HALT"); exit(1); } return 0; } // the bug is in this function int execute (int memory [], int* accumulator, int* instructionCounter, int* instructionRegister, int* operationCode, int* operand) { *accumulator = 0; *instructionCounter = 0; *instructionRegister = 0; *operationCode = 0; *operand = 0; for (*instructionCounter = 0; *instructionCounter < 100; *instructionCounter += 1) { *instructionRegister = memory[*instructionCounter]; *operationCode = *instructionRegister / 100; *operand = *instructionRegister % 100; switch (*operationCode) { case 10: //printf("got here"); scanf("%d", &memory[*operand]); // won't stop to scan in input here // I even tried putting a space before the %d and it still won't work // I also tried fflush(stdin) before scanf and it still wouldn't stop break; case 11: printf("%d", memory[*operand]); break; case 12: while (memory[*operand] != 0) { if (memory[*operand] / 100 == 10 || (memory[*operand] / 100 >= 65 && memory[*operand] / 100 <= 90)) { printf("%c", memory[*operand] / 100); } else { printf("ERROR Unknown Character"); exit(1); } if (memory[*operand] % 100 == 10 || (memory[*operand] % 100 >= 65 && memory[*operand] % 100 <= 90)) { printf("%c", memory[*operand] % 100); } else if (memory[*operand] % 100 == 0) { break; } else { printf("ERROR Unknown Character"); exit(1); } *operand += 1; } break; case 20: *accumulator = memory[*operand]; break; case 21: memory[*operand] = *accumulator; break; case 30: *accumulator += memory[*operand]; break; case 31: *accumulator -= memory[*operand]; break; case 32: if (memory[*operand] != 0) { *accumulator /= memory[*operand]; } else { printf("ERROR Divide 0"); } break; case 33: *accumulator *= memory[*operand]; break; case 34: if (memory[*operand] != 0) { *accumulator %= memory[*operand]; } else { printf("ERROR Divide 0"); } break; case 40: *instructionCounter = memory[*operand] - 1; break; case 41: if (*accumulator < 0) { *instructionCounter = memory[*operand] - 1; } break; case 42: if (*accumulator == 0) { *instructionCounter = memory[*operand] - 1; } break; case 99: dataDump(memory, accumulator, instructionCounter, instructionRegister, operationCode, operand); return 0; break; default: printf("ERROR Unknown command"); exit(1); break; } } return 0; } //This function works correctly int dataDump (int memory [], int* accumulator, int* instructionCounter, int* instructionRegister, int* operationCode, int* operand) { printf("

REGISTERS:

"); printf("%-25s%+05d

", "accumulator", *accumulator); printf("%-28s%02d

","instructionCounter", *instructionCounter); printf("%-25s%+05d

","instructionRegister", *instructionRegister); printf("%-28s%02d

","operationCode", *operationCode); printf("%-28s%02d

", "operand", *operand); printf("MEMORY:

"); for(*accumulator = 0; *accumulator <= 9; *accumulator += 1) { printf("%5d ", *accumulator); } for(*accumulator = 0; *accumulator < 100; *accumulator += 1) { if(*accumulator % 10 == 0) printf("

%2d ", *accumulator); printf("%+05d ", memory[*accumulator]); } printf("

"); return 0; }

This is the output: