Let’s create a computer called the Simpletron. As its name implies, it’s a simple machine, but powerful. The Simpletron runs programs written in the only language it directly understands: Simpleton Machine Language, or SML for short. The Simpletron contains an accumulator—a special register into which information is put before the Simpletron uses it in calculations or examines it in various ways. All the information in the Simpletron is handled in terms of words. A word is a signed four-digit decimal number, such as +3364, -1293, +0007 and -0001. The Simpletron is equipped with a 100-word memory, and these words are referenced by their location numbers 00, 01, …, 99.
Before running an SML program, we must load, or place, the code into memory. The first instruction (or statement) of every SML program is always placed in location 00. The simulator will start executing at this location.
Each instruction written in SML occupies one word of the Simpletron’s memory (hence, instructions are signed four-digit decimal numbers). We shall assume that the sign of an SML instruction is always plus, but the sign of a data word may be either plus or minus. Each location in the Simpletron’s memory may contain an instruction, a data value used by a program or an unused (and hence undefined) area of memory. The first two digits of each SML instruction are the operation code specifying the operation to be performed. SML operation codes are summarized in table below.
Operation Code Meaning Input/output operations: const int READ = 10; Read a word from the keyboard into a specific location in memory. const int WRITE = 11; Write a word from a specific location in memory to the screen. Load/store operations: const int LOAD = 20; Load a word from a specific location in memory into the accumulator. const int STORE = 21; Store a word from the accumulator into a specific location in memory. Arithmetic operations: const int ADD = 30; Add a word from a specific location in memory to the word in the accumulator (leave the result in the accumulator). const int SUBTRACT = 31; Subtract a word from a specific location in memory from the word in the accumulator (leave the result in the accumulator). const int DIVIDE = 32; Divide a word from a specific location in memory into the word in the accumulator (leave result in the accumulator).
const int MULTIPLY = 33; Multiply a word from a specific location in memory by the word in the accumulator (leave the result in the accumulator). Transfer of control operations: const int BRANCH = 40; Branch to a specific location in memory. const int BRANCHNEG = 41; Branch to a specific location in memory if the accumulator is negative. const int BRANCHZERO = 42; Branch to a specific location in memory if the accumulator is zero. const int HALT = 43; Halt. The program has completed its task.
The last two digits of an SML instruction are the operand—the address of the memory location containing the word to which the operation applies.
Following is a program that inputs two numbers from the keyboard and calculates the sum
Location Code Instruction 00 +1007 (Read A) 01 +1008 (Read B) 02 +2007 (Load A) 03 +3008 (Add B) 04 +2109 (Store C) 05 +1109 (Write C) 06 +4300 (Halt) 07 +0000 (Variable A) 08 +0000 (Variable B) 09 +0000 (Result C)
The instruction +1007 reads the first number from the keyboard and places it into location 07 (which has been initialized to 0). Then instruction +1008 reads the next number into location 08. The load instruction, +2007, puts the first number into the accumulator, and the add instruction, +3008, adds the second number to the number in the accumulator. All SML arithmetic instructions leave their results in the accumulator. The store instruction, +2109, places the result in memory location 09, from which the write instruction, +1109, takes the number and displays it (as a signed four-digit decimal number). The halt instruction, +4300, terminates execution.
The second SML program below reads two numbers from the keyboard and determines and displays the larger value. Note the use of the instruction +4107 as a conditional transfer of control, much the same as C#’s if statement.
Location Code Instruction 00 +1009 (Read A) 01 +1010 (Read B) 02 +2009 (Load A) 03 +3110 (Subtract B) 04 +4107 (Branch negative to 07) 05 +1109 (Write A) 06 +4300 (Halt)
07 +1110 (Write B) 08 +4300 (Halt) 09 +0000 (Variable A) 10 +0000 (Variable B)
Now write SML programs to accomplish each of the following tasks:
a) Use a sentinel-controlled loop to read positive numbers and compute and display their sum. Terminate input when a negative number is entered. b) Use a counter-controlled loop to read seven numbers, some positive and some negative, then compute and display their average. c) Read a series of numbers, then determine and display the largest number. The first number read indicates how many numbers should be processed.
How will this program run? When you run your Simpletron simulator, it should begin by displaying
*** Welcome to Simpletron! *** *** Please enter your program one instruction *** *** ( or data word ) at a time into the input *** *** text field. I will display the location *** *** number and a question mark (?). You then *** *** type the word for that location. Enter *** *** -99999 to stop entering your program. ***
Your app should simulate the memory of the Simpletron with a one-dimensional array memory of 100 elements. Now assume that the simulator is running, and let’s examine the dialog as we enter the program mentioned above
00 ? +1009 01 ? +1010 02 ? +2009 03 ? +3110 04 ? +4107 05 ? +1109 06 ? +4300 07 ? +1110 08 ? +4300 09 ? +0000 10 ? +0000 11 ? -99999
Your program should display the memory location followed by a question mark. Each of the values to the right of a question mark is input by the user. When the sentinel value -99999 is input, the program should display the following: