Skip to content

Commit 11fd3b3

Browse files
committed
Switched the order of error checks to make the program out of bounds
error come before the stack overflow error. If the pc goes too far it could trigger both and it is important to know that the pc went out of bounds instead of seeing stack overflow in that case.
1 parent 4530500 commit 11fd3b3

File tree

1 file changed

+10
-30
lines changed

1 file changed

+10
-30
lines changed

main.c

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,8 @@ const char* TEST_FILE = "test.vm";
1616
const bool DEBUGGING = false;
1717
#endif
1818

19-
// TODO setup the program to take a filename as an argument
20-
// TODO setup an assembler (can use a different langauge).
21-
22-
// NOTE new OP codes must be added at the end of the enum. The testing program
23-
// will be broken if they are inserted in the middle.
24-
25-
// NOTE Fixed point numbers are scaled by 1000 and only come signed
26-
// max number is MAX_INT / 1000 and smallest num is 0.001
27-
28-
// NOTE PUSH, GET, CALL, and RET are all operations that must be given arguments
29-
// in the program because the nature of these instructions is that these values
30-
// will never be dynamic. If you want to push a value you will need to know
31-
// that value (if you know the address of that value it is a load now). GET you
32-
// need to have knowledge of the expected argument layout for the procedure you're
33-
// in. CALL you must know the label of the procedure when the program is written
34-
// or at least assembled. For CALL and RET it is necessary to know the number
35-
// of arguments that you will pass and return when calling or writing the
36-
// procedure.
37-
// All other instructions can operate on data they know nothing about so it must
38-
// be pushed or loaded onto the stack for them to get.
39-
4019
// Macros //
20+
4121
// NOTE these rely on the memory block being named mem and the stack pointer
4222
// being named sp. Also on the functions get/set_dword and get/set_qword and
4323
// the typedefs for signed types SWORD SDWORD SQWORD. All of these are
@@ -309,7 +289,15 @@ int main( int argc, char *argv[] ) {
309289

310290
while (run) {
311291
/// Error Checking ///
312-
if (sp > bp) {
292+
if (pc >= prog_len) {
293+
fprintf(stderr,
294+
"Error: Program counter out of bounds: pc=0x%04x, prog_end=0x%04x\n",
295+
pc, prog_len);
296+
fprintf(stderr, "Stack: ");
297+
display_range(mem, sp, bp);
298+
fprintf(stderr, "\n");
299+
exit(EXIT_POB);
300+
} else if (sp > bp) {
313301
fprintf(stderr, "Error: Stack Underflow: sp=0x%04x, bp=0x%04x\n", sp, bp);
314302
fprintf(stderr, "Stack: ");
315303
display_range(mem, sp, bp);
@@ -319,14 +307,6 @@ int main( int argc, char *argv[] ) {
319307
fprintf(stderr, "Error: Stack Overflow: sp=0x%04x, brk=0x%04x, prg=0x%04x\n",
320308
sp, brk, prog_len);
321309
exit(EXIT_SOF);
322-
} else if (pc >= prog_len) {
323-
fprintf(stderr,
324-
"Error: Program counter out of bounds: pc=0x%04x, prog_end=0x%04x\n",
325-
pc, prog_len);
326-
fprintf(stderr, "Stack: ");
327-
display_range(mem, sp, bp);
328-
fprintf(stderr, "\n");
329-
exit(EXIT_POB);
330310
}
331311

332312
if (DEBUGGING) {

0 commit comments

Comments
 (0)