Java Reference
In-Depth Information
Your application should simulate the memory of the Simpletron with a one-dimensional array
mem-
ory
that has 100 elements. Now assume that the simulator is running, and let's examine the dialog
as we enter the program of Fig. 7.35 (Exercise 7.36):
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 value 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:
*** Program loading completed ***
*** Program execution begins ***
The SML program has now been placed (or loaded) in array
memory
. Now the Simpletron exe-
cutes the SML program. Execution begins with the instruction in location
00
and, as in Java, con-
tinues sequentially, unless directed to some other part of the program by a transfer of control.
Use the variable
accumulator
to represent the accumulator register. Use the variable
instruc-
tionCounter
to keep track of the location in memory that contains the instruction being per-
formed. Use the variable
operationCode
to indicate the operation currently being performed (i.e.,
the left two digits of the instruction word). Use the variable
operand
to indicate the memory loca-
tion on which the current instruction operates. Thus,
operand
is the rightmost two digits of the
instruction currently being performed. Do not execute instructions directly from memory. Rather,
transfer the next instruction to be performed from memory to a variable called
instructionRegis-
ter
. Then pick off the left two digits and place them in
operationCode
, and pick off the right two
digits and place them in
operand
. When the Simpletron begins execution, the special registers are
all initialized to zero.
Now, let's walk through execution of the first SML instruction,
+1009
in memory location
00
.
This procedure is called an
instruction-execution cycle.
The
instructionCounter
tells us the location of the next instruction to be performed. We
fetch
the contents of that location from
memory
by using the Java statement
instructionRegister = memory[instructionCounter];
The operation code and the operand are extracted from the instruction register by the statements
operationCode = instructionRegister /
100
;
operand = instructionRegister %
100
;
Now the Simpletron must determine that the operation code is actually a
read
(versus a
write
, a
load
, and so on). A
switch
differentiates among the 12 operations of SML. In the
switch
state-
ment, the behavior of various SML instructions is simulated as shown in Fig. 7.36. We discuss
branch instructions shortly and leave the others to you.
When the SML program completes execution, the name and contents of each register as well
as the complete contents of memory should be displayed. Such a printout is often called a com-
puter dump (no, a computer dump is not a place where old computers go). To help you program
your dump method, a sample dump format is shown in Fig. 7.37. A dump after executing a
Simpletron program would show the actual values of instructions and data values at the moment
execution terminated.