(Exercise 8.18 machine-language programming + Exercise 8.19 Computer)
Here comes the interesting part! I have created a virtual computer named Simpletron with c++! It is a simple machine which has memory locations, each can store a four digit signed decimal integers ( -9999 to +9 999). When running a program, the Machine Load "program" to the "start" of its memory (location) and run the Instructio n stored in it one by one.
Although it is simple, we can still run program on it! Programs in Simpletron are Witten into SML, which stands for Simpletron Machine. Each instruction written in SML occupies one memory location to Simpletron, thus, it is a signed four integer. The two digits of each instruction are the operation code this specifies to be operation (performed shown). The last two digits are the operand, which are the address of the memory location. (Remember?) There are locations, which can is indexed 0 to 99)
Ok, now we have a brief knowledge of SML. We can try writing programs in SML now! (It's miserable, trust me XD)
The books provide some simple programs that can is written in SML. I have written down my programs below. Take a look in them if you are interested. Also try to write a more complicated program yourself! You'll miss C + + once you trying to the program in SML though. LOL
ADD sum until a negative number is entered Location number instruction +1011 Read N +2011 Load N +4110 Bra nch negative to +2112 Store S +1011 Read N +2011 Load N +4109 Branch negative to modified +3012 ADD S +4003 Branch to +1112 Write S +4300 Halt +0000 Variable N +0000 Variable S Add sum of SE VEN numbers Location number instruction +1011 Read N +2012 Load s +3011 Add N +2112 Store s 04 +201 3 Load i +3114 subtract one +2113 Store I +4209 Branch Zero to +4000 Branch to +1112 Write s +4300 Halt +0000 Variable N +0000 Variable S +0007 Variable I +0001 One find the largest nu Mber in N numbers Location number instruction +1018 Read N-+2018 Load N +4217 Branch Zero to 17 03 +1019 Read largest +2018 Load n +3121 subtract one +4216 Branch zero to +2118 Store n +2019 Load La
Rgest +1020 Read X+3120 Subtract X +4113 Branch negative to +4004 Branch to +2020 Load X +2119 Store Largest +4004 Branch to +1119 Write largest +4300 Halt +0000 Variable N +0000 Variable largest 20 +0 Variable X +0001 One
Okay, we have the program now. wait! How do we run those programs? We don ' t have the Simpletron machine in reality! Don ' t be nervous, guys. I am going to show you I write a simpletron simulator with C + +. Here's how it looks (the testing's the same with the one above):
The Simpletron machine has some register units to store neccessary information to run programs. We use a accumulator register to store the result of calculation, a instruction register to store the current Instructio N and a counter register to indicate the current location of execution. For example, where we need to add the "Integer at location" at location together, we-i-load the FIR St Integer to the accumulator. (instruction:+2010) Then we add the second integer to the accumulator. (instruction:+3011) The "result is" now in the accumulator and we store it at location for later use. (instruction:+2112) Note then each instruction, the value in the counter register is increased by 1, which tells the computer to run the Instruction at the next memory location.
I created a Simpletronsimulator class to represent the virtual computer. And I used a lot of static const int to represent the operation code in the table above so I can recognize different Operation with a switch easily. Okay, enough talk. The details are all in the sourcecode. I promise that's IS's a very easy and clear implementation!
Exercise 8.19 Computer Simulator #include <iostream> #include <iomanip> using namespace std;
To simulate a type of computer called Simpletron class Simpletronsimulator {public:simpletronsimulator ();
void Printconsolemessage ();
void Printerrormessage ();
void Inputsmlprogram ();
void Executesmlprogram ();
void Printregisterandmemorydump ();
Private://memory static const int memorysize = 100;
int memory[memorysize]; BOOL Setmemory (int index);
False when-99999 Sentinel is entered bool validinput (int input);
registers int accumulator;
int instructioncounter;
int instructionregister;
int Operationcode;
int operand;
Operation code static const int READ = 10;
static const int WRITE = 11;
static const int LOAD = 20;
static const int STORE = 21;
static const int ADD = 30;
static const int subtract = 31;
static const int DIVIDE = 32;
static const int MULTIPLY = 33;
static const int BRANCH = 40;
static const int BRANCHNEG = 41; Static const int Branchzero = 42;
static const int HALT = 43;
};
int main () {Simpletronsimulator simulator;
Simulator.printconsolemessage ();
return 0;
} simpletronsimulator::simpletronsimulator () {accumulator = 0;
Instructioncounter = 0;
for (int i=0; i<100; i++) {memory[i] = 0; } void Simpletronsimulator::p rintconsolemessage () {cout << "* * * Welcome to simpletron!
"<< Endl;
cout << "* * * Please enter the your program one instruction * * * * * << Endl; cout << "* * * (or data word) at a time.
I'll Type the * * * * << Endl; cout << "* * * location number and a question mark (?).
"<< Endl; cout << "* * * then type the word for that location.
"<< Endl;
cout << "* * * Type the sentinel-99999 to stop entering * * * * << Endl; cout << "* * * your program.
"<< Endl << Endl;
Inputsmlprogram ();
cout << Endl; cout << "* * * Program Loading completed * * *" << endL
cout << Execution Begins * * * * * * * << Endl;
Executesmlprogram ();
cout << Execution Completed * * * * * * * << Endl;
cout << Endl;
Printregisterandmemorydump ();
} void Simpletronsimulator::inputsmlprogram () {bool inputcontinue = true; for (int counter=0; inputcontinue counter++) inputcontinue = setmemory (counter); Setmemory return False when-99999 are entered} void Simpletronsimulator::executesmlprogram () {int temp;/For Temp
Ory use does {//load necessary data into registers instructionregister = Memory[instructioncounter]; Operationcode = instructionregister/100; The two digits as code operand = instructionregister% 100;
Last two digits as location switch (operationcode) {case read:cout << "?";
CIN >> Temp;
Memory[operand] = temp;
instructioncounter++;
Break
Case Write:cout << showpos << Memory[operand] << Endl; Instructioncounter++;
Break
Case load:accumulator = Memory[operand];
instructioncounter++;
Break
Case Store:memory[operand] = accumulator;
instructioncounter++;
Break
Case Add:accumulator + = Memory[operand];
instructioncounter++;
Break
Case Subtract:accumulator-= Memory[operand];
instructioncounter++;
Break
Case Divide:accumulator/= Memory[operand];
instructioncounter++;
Break
Case Multiply:accumulator *= Memory[operand];
instructioncounter++;
Break
Case branch:instructioncounter = operand;
Break
Case BRANCHNEG:IF (Accumulator < 0) {instructioncounter = operand;
else {instructioncounter++;
} break;
Case BRANCHZERO:IF (accumulator = = 0) {instructioncounter = operand;
else {instructioncounter++;
} break;
Case Halt:break;
Default:break;
} while (Operationcode!= HALT); } void Simpletronsimulator::p rintregisterandmemoRydump () {cout << Setfill (' 0 ') << internal;//Fill 0 between ' + ' and number cout << "registers:" &L
t;< Endl;
cout << "accumulator\t\t\t" << showpos << setw (5) << accumulator << Endl;
cout << "instructioncounter\t\t" << noshowpos << setw (2) << instructioncounter << Endl;
cout << "instructionregister\t\t" << showpos << setw (5) << instructionregister << Endl;
cout << "operationcode\t\t\t" << noshowpos << setw (2) << operationcode << Endl;
cout << "operand\t\t\t\t" << setw (2) << operand << Endl;
cout << Endl << "MEMORY:" << Endl;
cout << Setfill (') << SETW (2) << ';
for (int i=0; i<=9; i++)//column number cout << ' << setw (5) << i;
cout << Endl; for (int i=0; i<=90; i+=10)//loop from 0 to {cout << Noshowpos << Setfill (') << SETW (2) << i; Row number cout << Setfill (' 0 ') << showpos;
formating for (int j=0; j<=9; J + +) {cout << ' << setw (5) << memory[i+j];
} cout << Endl;
} bool Simpletronsimulator::setmemory (int index) {int input;
cout << Setfill (' 0 ');
Do//prompt again if not valid {cout << SETW (2) << index << "?";
CIN >> input;
while (!validinput (input));
if (input = = -99999) {return false;
else {Memory[index] = input;
return true; BOOL Simpletronsimulator::validinput (int input) {//valid when input is a 4-digit number or-99999 Sentinel Retu
RN ((input>=-9999 && input<=9999) | | input==-99999);
}
If you have any bugs or have any doubts, please feel free to contact me!