Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

This assignment is the first in a sequence of three. It is not strictly necessary to complete this one in order to do the other

This assignment is the first in a sequence of three. It is not strictly necessary to complete this one in order to do the other two, but the understanding you gain in completing this assignment will make writing the third assignment (a major project) much easier. You will need to complete the second in the sequence in order to do the third.

We start by "peeling open" a computer, look at its internal structure, and introducing machine language (assembler-level) programming. Your assignment is to write a program that simulates a computer, one that is capable of executing machine language programs.

1. Initial Setup

  1. Log in to Unix.

  2. Run the setup script for Assignment 4 by typing:

     setup 4 

2. Description of the Simplesim Computer

In this assignment you will write a program to simulate a fictional computer that we will call the Simplesim. As its name implies it is a simple machine. All information in the Simplesim is handled in terms of words. A word is a signed four-digit decimal (base 10) number such as +3364, -1293, +0007, -0001, 0000, etc. The Simplesim is equipped with memory and five registers.

  • The Simplesim has a 100-word memory and these words are referenced by their location numbers 00, 01, . . . , 99. Each word in the Simplesim's memory (always a single signed four-digit decimal number) may be interpreted as an instruction to be executed, a data value, or may be uninitialized.

  • The first register is the accumulator, which is just large enough to hold a single word. Words from memory must be placed into the accumulator in order to perform arithmetic on them or test their values. All arithmetic and branching is done using the accumulator.

  • The second register is the instruction counter, which is just large enough to hold a memory location (a two digit number, 00, 01, ... , 99). The instruction counter is used to hold the memory location of the next instruction to be executed.

  • The third register is the instruction register, which, like the accumulator, is just large enough to hold a single word. The instruction register is used to hold a copy of the instruction (a word that was pulled out of memory) that is currently being executed.

  • The fourth and fifth registers are the operation code and operand, respectively. Each one is just large enough to hold half of a word (a two digit decimal number). The operation code and operand registers are used to "split" the instruction register in half, with the 2 leftmost digits and sign of the instruction register going into the operation code and the 2 rightmost digits going into the operand. For example, if the instruction register had +1009, the operation code would have +10 and the operand would have 09. Likewise, if the instruction register had -1201, the operation code would have -12 and the operand would have 01.

3. The Simplesim Machine Language (SML)

Each instruction written in the Simplesim Machine Language (SML) occupies one word of the Simplesim's memory (and hence instructions are signed four-digit decimal numbers). The two leftmost digits of each SML instruction are the operation code (opcode), which specifies the operation to be performed. The two rightmost digits of an SML instruction are the operand, which is the memory location containing the word to which the operation applies. The complete set of SML instructions is described in the table that follows.

Operation Code Meaning
Input / Output Operations:
#define READ 01 Read a word into a specific memory location.
#define WRITE 02 Print a word from a specific memory location.
Store / Load Operations:
#define STORE 11 Store the word in the accumulator into a specific memory location.
#define LOAD 12 Load a word from a specific memory location into the accumulator.
Arithmetic Operations:
#define ADD 21 Add a word in a specific memory location to the word in the accumulator (leave result in accumulator).
#define SUBTRACT 22 Subtract a word in a specific memory location from the word in the accumulator (leave result in accumulator).
#define MULTIPLY 23 Multiply a word in a specific memory location by the word in the accumulator (leave result in accumulator).
#define DIVIDE 24 Divide a word in a specific memory location into the word in the accumulator (leave result in accumulator).
Transfer of Control Operations:
#define BRANCH 31 Branch to a specific memory location.
#define BRANCHZERO 32 Branch to a specific memory location if the accumulator is zero.
#define BRANCHNEG 33 Branch to a specific memory location if the accumulator is negative.
#define HALT 34 Halt, i.e., the program has completed its task.

We illustrate how the Simplesim executes SML programs (using the instructions from the table above) with the use of two example SML programs. Consider the following SML program which reads two numbers and computes and prints their sum.

Memory Location Word Instruction
00 +0107 (Read A)
01 +0108 (Read B)
02 +1207 (Load A)
03 +2108 (Add B)
04 +1109 (Store C)
05 +0209 (Write C)
06 +3400 (Halt)
07 +0000 (Variable A)
08 +0000 (Variable B)
09 +0000 (Result C)

Execution always begins at memory location 00. The word at memory location 00 (+0107) is read and interpreted as an instruction. The leftmost two digits of the word (01) represent the instruction and the rightmost two digits (07) represent the instruction's operand. The first instruction is a READ operation. This reads a single word from the input file (explained in Section 4) and stores it in the memory location defined by the operand, in this case memory location 07. READ and WRITE instructions always operate on memory locations. This completes the execution of the first instruction. Processing continues by executing the next instruction found at memory location 01.

The next instruction (+0108) reads a second word from the input file and stores it in memory location 08. The next instruction (+1207) is a LOAD operation with operand 07. It takes the word found at memory location 07 (the operand) and places it into the accumulator (recall that the accumulator is one of the five registers described in Section 1). All LOAD and STORE operations move data in and out of the accumulator.

The next instruction (+2108) is an ADD instruction with operand 08. All SML arithmetic instructions are performed using the word in the accumulator and the word identified by the operand and the result is always left in the accumulator. This instruction takes the word stored in memory location 08 (the operand), adds it to the value in the accumulator, and leaves the sum in the accumulator.

The next instruction (+1109) is a STORE instruction which, like all STORE instructions, takes the word in the accumulator (the sum of the two input values) and stores it in the memory location identified by the instruction's operand, in this case memory location 09. Then +0209, a WRITE instruction, prints (output is explained in Section 5) the word found in memory location 09, which - again - is the sum of the two input values. Finally instruction +3400, the HALT instruction, is executed which simply terminates the SML program (operand 00 is ignored for this instruction).

Note that a single word in memory can be used to store a single instruction that is to be executed or a single variable that should never be interpreted as an instruction. None of the memory locations after the HALT instruction (memory locations 07-09) were executed; however, they were important in the computation. Those words were used to store the program's variables and temporary results.

All SML programs will "partition" the Simplesim's memory in this way. The first words of memory (always starting at memory location 00) are the "instructions" of the program and following that, after the HALT instruction, is the "data" part of the program. The intention, of course, is that only the "instructions" of the program are to be executed, i.e., each word interpreted as an SML instruction.

Now consider this second SML program that reads two numbers and prints the larger of the two.

Memory Location Word Instruction
00 +0109 (Read A)
01 +0110 (Read B)
02 +1209 (Load A)
03 +2210 (Subtract B)
04 +3207 (Branch negative to 07)
05 +0209 (Write A)
06 +3400 (Halt)
07 +0210 (Write B)
08 +3400 (Halt)
09 +0000 (Variable A)
10 +0000 (Variable B)

The first two instructions (+0109 and +0110) read two values and store them in memory locations 09 and 10, respectively. +1209 places the word at memory location 09 (the first input value) into the accumulator. +2210, a SUBTRACT instruction, takes the word at memory location 10 (the second input value), subtracts it from the accumulator, and leaves the result in the accumulator.

+3207 (BRANCHNEG) is a conditional branch instruction, much like an "if" statement in C++. All conditional branch instructions are based on the accumulator. The BRANCH instruction, which acts like a "goto", is the only branch instruction that ignores the accumulator; it is simply an unconditional branch.

If the value in the accumulator is negative, which in this case means the second input value was the largest, then the next instruction that gets executed is the one at memory location 07 (the operand). If the value in the accumulator is 0 or greater, meaning the first input value was greater than or equal to the second, then execution continues with the next statement, i.e., no branching. If the branch was taken, then the value at memory location 10 (the second input value) is printed and the program terminates. Otherwise the value at memory location 09 (the first input value) is printed and the program terminates.

Note how the SML program is written. It "partitions" the Simplesim's memory into two distinct parts; the "program" (locations 00-08) and the "data" (locations 09-10). This SML program, unlike the first, has two HALT instructions. This is okay; only one of them will be executed. The point is that HALT instructions are used to prevent the execution of the program from wandering into the "data" portion of the program.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions