Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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.

 

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).

.

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.

4. Input

Your program will take as input an SML program followed by any input for that SML program.

The input file will start with the SML program, one instruction per line. Following the last line of the SML program will be the number -99999, which is not part of the SML program. If the SML program expects any input (i.e., if it has any READ instructions) then input for the SML program, one input value per line, immediately follows the -99999 line. For example, below is the input file for the first program from the previous section. It adds -5 and 15.

0107 0108 1207 2108 1109 0209 3400 0000 0000 0000 -99999 -5 15 

Note that each line of the input file, other than -99999 which is used to denote the end of program and not intended to be placed into the Simplesim's memory, fits into a single word. Note also that not all SML programs require input (those that do not have READ instructions). In that case there would be no data after the -99999 line.

All input files to your program have -99999 after the last SML instruction. For those SML programs that do not require input, (those that do not have READ instructions), -99999 is simply the last line of the input file.

5. Output

Each time a READ instruction is executed your program must print the value that was read. For example, the two values read in the program from the previous section are -5 and 15. As each value is read, your program should print output that looks exactly like this.

READ: -0005 READ: +0015 

For each WRITE instruction your program must print the value of the word in that memory location. For example, from the program in the previous section, the sum 10 is printed exactly like this.

+0010 

When the HALT statement is executed, your program should print the following line:

*** Simplesim execution terminated *** 

At the end of any execution your program should dump the entire contents of the Simplesim. This means dumping the contents of all five registers and all 100 words of the Simplesim's memory.

Assuming that the name of your program is simplesim and the name of the SML program file above is sum.sml, then the output of your program must look exactly like this:

z123456@turing:~/csci241/Assign4$ ./simplesim < sum.sml READ: -0005 READ: +0015 +0010 *** Simplesim execution terminated *** REGISTERS: accumulator: +0010 instruction_counter: 06 instruction_register: +3400 operation_code: 34 operand: 00 MEMORY: 0 1 2 3 4 5 6 7 8 9 0 +0107 +0108 +1207 +2108 +1109 +0209 +3400 -0005 +0015 +0010 10 +4444 +4444 +4444 +4444 +4444 +4444 +4444 +4444 +4444 +4444 20 +4444 +4444 +4444 +4444 +4444 +4444 +4444 +4444 +4444 +4444 30 +4444 +4444 +4444 +4444 +4444 +4444 +4444 +4444 +4444 +4444 40 +4444 +4444 +4444 +4444 +4444 +4444 +4444 +4444 +4444 +4444 50 +4444 +4444 +4444 +4444 +4444 +4444 +4444 +4444 +4444 +4444 60 +4444 +4444 +4444 +4444 +4444 +4444 +4444 +4444 +4444 +4444 70 +4444 +4444 +4444 +4444 +4444 +4444 +4444 +4444 +4444 +4444 80 +4444 +4444 +4444 +4444 +4444 +4444 +4444 +4444 +4444 +4444 90 +4444 +4444 +4444 +4444 +4444 +4444 +4444 +4444 +4444 +4444 z123456@turing:~/csci241/Assign4$ 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

SQL Antipatterns Avoiding The Pitfalls Of Database Programming

Authors: Bill Karwin

1st Edition

1680508989, 978-1680508987

More Books

Students also viewed these Databases questions

Question

What is a cookie?

Answered: 1 week ago

Question

How can the Internet be helpful in a job search? (Objective 2)

Answered: 1 week ago