Your task is to create a program which simulates a computer running a machine language program which is to be read from disk. You will have three (3) class periods to complete your work.
Further, you are to write two machine language programs to run in your simulator:
I am providing a program and two machine language programs, add.sim and bigger.sim, which work. This is a solution to a problem in Deitel & Deitel. This simulator uses a 4-digit decimal value in each memory location. Locations which contain program code consist of a two digit operator and a two-digit operand. A data location contains a signed four-digit decimal value.
You are to use this program as the basis for developing a simulator for a machine language described below.
struct operator {
unsigned char opCode;
char name[24];
int numbytes;
};
The Op codes you are to handle are the following:
| Constant | Hex Value | Bytes | Meaning |
| READNUM | 10 | 2 | Read numeric value from keyboard & store in RAM |
| WRITENUM | 11 | 2 | Write numeric value from RAM to screen |
| READSTR | 12 | 2 | Read string from keyboard & store in RAM |
| WRITESTR | 13 | 2 | Write string from RAM to screen |
| WRITENL | 14 | 1 | Write newline to screen |
| LOADA | 20 | 2 | Load int from RAM into accumulator |
| STOREA | 21 | 2 | Store int from A into RAM |
| LOADAIMM | 22 | 3 | Load hardset int into accumulator |
| LOADX | 23 | 2 | Load int from RAM into X |
| STOREX | 24 | 2 | Store int from X into RAM |
| LOADXIMM | 25 | 3 | Load hardset int into X |
| LOADABYTE | 26 | 2 | Load byte from RAM into accumulator |
| STOREABYTE | 27 | 2 | Store byte from A into RAM |
| LOADAIMMBYTE | 28 | 2 | Load hardset byte into accumulator |
| LOADAXBYTE | 29 | 1 | Load byte from RAM into A using X as address |
| STOREAXBYTE | 2A | 1 | Store byte from A into RAM using X as address |
| MOVEATOX | 2C | 1 | Move value in A to X |
| MOVEXTOA | 2D | 1 | Move value in X to A |
| ADDX | 30 | 1 | Add X to A |
| SUBTRACTX | 31 | 1 | Subtract X from A |
| MULTIPLYX | 32 | 1 | Multiply X with A |
| DIVIDEX | 33 | 1 | Divide X into A |
| ADDMEM | 34 | 2 | Add value in memory to A |
| SUBTRACTMEM | 35 | 2 | Subtract value in memory from A |
| MULTIPLYMEM | 36 | 2 | Multiply value in memory with A |
| DIVIDEMEM | 37 | 2 | Divide value in memory into A |
| ADDIMM | 38 | 3 | Add hardset value to A |
| SUBTRACTIMM | 39 | 3 | Subtract hardset value from A |
| MULTIPLYIMM | 3A | 3 | Multiply hardset value with A |
| DIVIDEIMM | 3B | 3 | Divide hardset value into A |
| INCA | 40 | 1 | Increment A |
| INCX | 41 | 1 | Increment X |
| DECA | 42 | 1 | Decrement A |
| DECX | 43 | 1 | Decrement X |
| BRANCH | 50 | 2 | Unconditional branch to RAM location |
| BRANCHNEG | 51 | 2 | Branch to RAM location if A is negative |
| BRANCHZERO | 52 | 2 | Branch to RAM location if A is zero |
| BRANCHPOS | 53 | 2 | Branch to RAM location if A is positive |
| HALT | 54 | 1 | Terminate program |
N.B. Op codes 29 and 2A have been redefined (to function like pointers) and 2B has been eliminated.
Simple adder program:
13 16 // Prompt for x 10 29 // Read number into x 13 16 // Prompt for y 10 2B // Read number into y 20 29 // Load x into A 34 2B // Add y to A 21 2D // Store sum in mem z 13 2F // Output string 11 2D // Output sum 13 3B // Output . 14 // NL 54 // HALT 54 79 70 65 20 69 6E 20 61 20 6E 75 6D 62 65 72 3A 20 00 // Prompt 00 00 // x 00 00 // y 00 00 // z 54 68 65 20 73 75 6D 20 69 73 20 00 // Result string 2E 00 // Period