Special Section: Building Your Own Computer

Special Section Building Your Own Computer

In the next several problems, we take a temporary diversion away from the world of high-level-language programming. We "peel open" a computer and look at its internal structure. We introduce machine-language programming and write several machine-language programs. To make this an especially valuable experience, we then build a computer (using software-based simulation) on which you can execute your machine-language programs!

8.18

(Machine-Language Programming) Let us create a computer we will call the Simpletron. As its name implies, it is a simple machine, but, as we will soon see, it is a powerful one as well. The Simpletron runs programs written in the only language it directly understands, that is, Simpletron Machine Language, or SML for short.

The Simpletron contains an accumulatora "special register" in which information is put before the Simpletron uses that information in calculations or examines it in various ways. All information in the Simpletron is handled in terms of words. A word is a signed four-digit decimal number, such as +3364, -1293, +0007, -0001, etc. 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 program 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; thus, instructions are signed four-digit decimal numbers. 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 that specifies the operation to be performed. SML operation codes are shown in Fig. 8.39.

Figure 8.39. Simpletron Machine Language (SML) operation codes.

(This item is displayed on page 465 in the print version)

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 and 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 result in accumulator).

const int SUBTRACT = 31;

Subtract a word from a specific location in memory from the word in the accumulator (leave result in accumulator).

const int DIVIDE = 32;

Divide a word from a specific location in memory into the word in the accumulator (leave result in accumulator).

const int MULTIPLY = 33;

Multiply a word from a specific location in memory by the word in the accumulator (leave result in 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;

Haltthe program has completed its task.

 

The last two digits of an SML instruction are the operandthe address of the memory location containing the word to which the operation applies.


Now let us consider two simple SML programs. The first SML program (Fig. 8.40) reads two numbers from the keyboard and computes and prints their sum. The instruction +1007 reads the first number from the keyboard and places it into location 07 (which has been initialized to zero). Instruction +1008 reads the next number into location 08. The load instruction, +2007, places (copies) 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 (copies) the result back into memory location 09. Then the write instruction, +1109, takes the number and prints it (as a signed four-digit decimal number). The halt instruction, +4300, terminates execution.


Figure 8.40. SML Example 1.

Location

Number

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 SML program in Fig. 8.41 reads two numbers from the keyboard, then determines and prints the larger value. Note the use of the instruction +4107 as a conditional transfer of control, much the same as C++'s if statement.

Figure 8.41. SML Example 2.

Location

Number

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:

  1. Use a sentinel-controlled loop to read positive numbers and compute and print their sum. Terminate input when a negative number is entered.
  2. Use a counter-controlled loop to read seven numbers, some positive and some negative, and compute and print their average.
  3. Read a series of numbers, and determine and print the largest number. The first number read indicates how many numbers should be processed.
 
8.19

(Computer Simulator) It may at first seem outrageous, but in this problem you are going to build your own computer. No, you will not be soldering components together. Rather, you will use the powerful technique of software-based simulation to create a software model of the Simpletron. You will not be disappointed. Your Simpletron simulator will turn the computer you are using into a Simpletron, and you actually will be able to run, test and debug the SML programs you wrote in Exercise 8.18.

When you run your Simpletron simulator, it should begin by printing

*** Welcome to Simpletron! ***

*** Please enter your program one instruction ***
*** (or data word) at a time. I will type the ***
*** location number and a question mark (?). ***
*** You then type the word for that location. ***
*** Type the sentinel -99999 to stop entering ***
*** your program. ***
 

Your program should simulate the Simpletron's memory with a single-subscripted, 100-element array memory. Now assume that the simulator is running, and let us examine the dialog as we enter the program of Example 2 of Exercise 8.18:

00 ? +1009
01 ? +1010
02 ? +2009
03 ? +3110
04 ? +4107
05 ? +1109
06 ? +4300
07 ? +1110
08 ? +4300
09 ? +0000
10 ? +0000
11 ? -99999

*** Program loading completed ***
*** Program execution begins ***
 

Note that the numbers to the right of each ? in the preceding dialog represent the SML program instructions input by the user.

The SML program has now been placed (or loaded) into array memory. Now the Simpletron executes your SML program. Execution begins with the instruction in location 00 and, like C++, continues sequentially, unless directed to some other part of the program by a transfer of control.

Use variable accumulator to represent the accumulator register. Use variable counter to keep track of the location in memory that contains the instruction being performed. Use variable operationCode to indicate the operation currently being performed (i.e., the left two digits of the instruction word). Use variable operand to indicate the memory location 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 instructionRegister. 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 Simpletron begins execution, the special registers are all initialized to zero.

Now let us "walk through" the execution of the first SML instruction, +1009 in memory location 00. This is called an instruction execution cycle.

The counter tells us the location of the next instruction to be performed. We fetch the contents of that location from memory by using the C++ statement

instructionRegister = memory[ counter ];
 

The operation code and 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, etc.). A switch differentiates among the 12 operations of SML.

In the switch statement, the behavior of various SML instructions is simulated as shown in Fig. 8.42 (we leave the others to the reader).

Figure 8.42. Behavior of SML instructions.

read:

cin >> memory[ operand ];

load:

accumulator = memory[ operand ];

add:

accumulator += memory[ operand ];

branch:

We will discuss the branch instructions shortly.

halt:

This instruction prints the message

 

*** Simpletron execution terminated ***

 

The halt instruction also causes the Simpletron to print the name and contents of each register, as well as the complete contents of memory. Such a printout is often called a computer dump (and, no, a computer dump is not a place where old computers go). To help you program your dump function, a sample dump format is shown in Fig. 8.43. Note that a dump after executing a Simpletron program would show the actual values of instructions and data values at the moment execution terminated. To format numbers with their sign as shown in the dump, use stream manipulator showpos. To disable the display of the sign, use stream manipulator noshowpos. For numbers that have fewer than four digits, you can format numbers with leading zeros between the sign and the value by using the following statement before outputting the value:

cout << setfill( '0' ) << internal;
 

Figure 8.43. A sample dump.

 REGISTERS:
 accumulator +0000
 counter 00
 instructionRegister +0000
 operationCode 00
 operand 00

 MEMORY:
 0 1 2 3 4 5 6 7 8 9
 0 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000
 10 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000
 20 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000
 30 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000
 40 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000
 50 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000
 60 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000
 70 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000
 80 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000
 90 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000
 

Parameterized stream manipulator setfill (from header ) specifies the fill character that will appear between the sign and the value when a number is displayed with a field width of five characters but does not have four digits. (One position in the field width is reserved for the sign.) Stream manipulator internal indicates that the fill characters should appear between the sign and the numeric value.


Let us proceed with the execution of our program's first instruction+1009 in location 00. As we have indicated, the switch statement simulates this by performing the C++ statement

cin >> memory[ operand ];
 

A question mark (?) should be displayed on the screen before the cin statement executes to prompt the user for input. The Simpletron waits for the user to type a value and press the Enter key. The value is then read into location 09.

At this point, simulation of the first instruction is complete. All that remains is to prepare the Simpletron to execute the next instruction. The instruction just performed was not a transfer of control, so we need merely increment the instruction counter register as follows:

++counter;
 

This completes the simulated execution of the first instruction. The entire process (i.e., the instruction execution cycle) begins anew with the fetch of the next instruction to execute.

Now let us consider how to simulate the branching instructions (i.e., the transfers of control). All we need to do is adjust the value in the instruction counter appropriately. Therefore, the unconditional branch instruction (40) is simulated in the switch as

counter = operand;
 

The conditional "branch if accumulator is zero" instruction is simulated as

if ( accumulator == 0 )
 counter = operand;
 

At this point, you should implement your Simpletron simulator and run each of the SML programs you wrote in Exercise 8.18. You may embellish SML with additional features and provide for these in your simulator.

Your simulator should check for various types of errors. During the program loading phase, for example, each number the user types into the Simpletron's memory must be in the range -9999 to +9999. Your simulator should use a while loop to test that each number entered is in this range and, if not, keep prompting the user to reenter the number until the user enters a correct number.

During the execution phase, your simulator should check for various serious errors, such as attempts to divide by zero, attempts to execute invalid operation codes, accumulator overflows (i.e., arithmetic operations resulting in values larger than +9999 or smaller than -9999) and the like. Such serious errors are called fatal errors. When a fatal error is detected, your simulator should print an error message such as

*** Attempt to divide by zero ***
*** Simpletron execution abnormally terminated ***
 

and should print a full computer dump in the format we have discussed previously. This will help the user locate the error in the program.

Introduction to Computers, the Internet and World Wide Web

Introduction to C++ Programming

Introduction to Classes and Objects

Control Statements: Part 1

Control Statements: Part 2

Functions and an Introduction to Recursion

Arrays and Vectors

Pointers and Pointer-Based Strings

Classes: A Deeper Look, Part 1

Classes: A Deeper Look, Part 2

Operator Overloading; String and Array Objects

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

Templates

Stream Input/Output

Exception Handling

File Processing

Class string and String Stream Processing

Web Programming

Searching and Sorting

Data Structures

Bits, Characters, C-Strings and structs

Standard Template Library (STL)

Other Topics

Appendix A. Operator Precedence and Associativity Chart

Appendix B. ASCII Character Set

Appendix C. Fundamental Types

Appendix D. Number Systems

Appendix E. C Legacy Code Topics

Appendix F. Preprocessor

Appendix G. ATM Case Study Code

Appendix H. UML 2: Additional Diagram Types

Appendix I. C++ Internet and Web Resources

Appendix J. Introduction to XHTML

Appendix K. XHTML Special Characters

Appendix L. Using the Visual Studio .NET Debugger

Appendix M. Using the GNU C++ Debugger

Bibliography



C++ How to Program
C++ How to Program (5th Edition)
ISBN: 0131857576
EAN: 2147483647
Year: 2004
Pages: 627

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net