26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 7 Arrays 375<br />

Code and “pick off” the right two digits and place them in operand. Each of the preceding registers<br />

should have a corresponding JTextField in which its current value can be displayed at all<br />

times. When Simpletron begins execution, the special registers are all initialized <strong>to</strong> 0.<br />

Now, let us “walk through” execution of the first SML instruction, +1009 in memory location<br />

00. This is called an instruction execution cycle.<br />

The instructionCounter tells us the location of the next instruction <strong>to</strong> be performed. We<br />

fetch the contents of that location from memory by using the <strong>Java</strong> statement<br />

instructionRegister = memory[ instructionCounter ];<br />

The operation code and the operand are extracted from the instruction register by the statements<br />

operationCode = instructionRegister / 100;<br />

operand = instructionRegister % 100;<br />

Now the Simpletron must determine that the operation code is actually a read (versus a write, a load,<br />

etc.). A switch differentiates among the twelve operations of SML.<br />

In the switch structure, the behavior of various SML instructions is simulated as shown in<br />

Fig. 7.36. We discuss branch instructions shortly and leave the others <strong>to</strong> the reader.<br />

When the SML program completes execution, the name and contents of each register as well as<br />

the complete contents of memory should be displayed. Such a prin<strong>to</strong>ut is often called a computer<br />

dump (and, no, a computer dump is not a place where old computers go). To help you program your<br />

dump method, a sample dump format is shown in Fig. 7.37. Note that a dump after executing a Simpletron<br />

program would show the actual values of instructions and data values at the moment execution<br />

terminated. The sample dump assumes the output will be sent <strong>to</strong> the display screen with a series<br />

of System.out.print and System.out.println method calls. <strong>How</strong>ever, we encourage<br />

you <strong>to</strong> experiment with a version that can be displayed on the applet using a JTextArea or an<br />

array of JTextField objects.<br />

Let us proceed with the execution of our program’s first instruction, namely the +1009 in location<br />

00. As we have indicated, the switch statement simulates this by prompting the user <strong>to</strong> enter<br />

a value in<strong>to</strong> the input dialog, reading the value, converting the value <strong>to</strong> an integer and s<strong>to</strong>ring it in<br />

memory location memory[ operand ]. Since your Simpletron is event driven, it waits for the user<br />

<strong>to</strong> type a value in<strong>to</strong> the input JTextField and press the Enter key. The value is then read in<strong>to</strong><br />

location 09.<br />

At this point, simulation of the first instruction is completed. All that remains is <strong>to</strong> prepare the<br />

Simpletron <strong>to</strong> execute the next instruction. Since the instruction just performed was not a transfer of<br />

control, we need merely increment the instruction counter register as follows:<br />

++instructionCounter;<br />

Instruction Description<br />

read: Display an input dialog with the prompt “Enter an integer.” Convert the<br />

input value <strong>to</strong> an integer and s<strong>to</strong>re it in location memory[ operand ].<br />

load: accumula<strong>to</strong>r = memory[ operand ];<br />

add: accumula<strong>to</strong>r += memory[ operand ];<br />

Fig. Fig. 7.36 7.36 Behavior of several SML instructions in the Simpletron.<br />

© Copyright 1992–2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!