Question: Computer Architecture problem Purpose : Write a simple machine program that takes the numbers -1, -2, -3, -4, -5, -6, -7, -8, -9 and -10,
Computer Architecture problem
Purpose: Write a simple machine program that takes the numbers -1, -2, -3, -4, -5, -6, -7, -8, -9 and -10, sums all of them together, and stores their sum in a memory location.
Procedure and Guidlines
In this lab, you should make use of the CTR, known as the counter. The CTR register can be used with the simple machine's decrement instruction to subtract 1 from the contents of CTR. The CTR's contents can be moved to the accumulator with a simple machine instruction, and you should use the accumulator to store the values -1, -2, -3, -4, -5, -6, -7, -8, -9, and -10 into ten separate memory locations. The ten memory locations can be at memory addresses you choose. You should chose memory addresses that are at address value 010 and higher.
Note: When performing instructions to move the accumulator's value into the CTR, decrementing the value in the CTR, and adding the value of the CTR to the value in the accumulator, you should use these instructions with the memory address 000. The memory address 000 for these instructions indicates that no memory location is needed in executing the instruction.
The program should contain comments a description of how your program works to add all of the numbers together and store their sum in a memory location. Additionally, each line of the program should have a comment explaining what the instruction on that line is doing.
Here are some sample simple machine programs that have comments and instructions to give you a feel for the types of comments and instructions: add4
sub4
add4
#
# this program adds four unsigned integers
# since addition is commutative, we can take
# a simple shortcut: e.g.,
# accum = accum + memory
# is the same as
# accum = memory + accum
#
0x100a # (0) LOAD ZERO - set accumulator to zero
0x800b # ADD INT1 - add first int
0x800c # ADD INT2 - add second int
0x800d # ADD INT3 - add third int
0x800e # Add INT4 - add fourth int
0x200f # (5) STORE RESULT - store result
0x0
0x0
0x0 # note some area is left between instructions and data
0x0 # just in case we want more instructions later.
0x0 # (0xA) ZERO: - the constant value zero
0x4 # (0xB) INT1:
0x8 # (0xC) INT2:
0x10 # (0xD) INT3:
0x20 # (0xE) INT4: fourth int
0x0 # (0xF) RESULT:
sub4
#
# this program subtracts four integers from zero.
# since subtraction is not commutative, we must
# do it the long way, storing the intermediate result
# each time. (Remember SUB NUM subtracts the contents
# of the accumulator from NUM and puts the result in the
# accumulator.)
#
# RESULT = 0
0x1010 # LOAD ZERO - set accumulator to zero
0x2015 # STORE RESULT
# RESULT -= INT1
0x1011 # LOAD INT1 - load first int
0x9015 # SUB RESULT - subtract from result
0x2015 # (4) STORE RESULT - store in result
# and repeat the previous three instruction three more times
# RESULT -= INT2
0x1012 # load second int
0x9015 # subtract from result
0x2015 # store in result
# RESULT -= INT3
0x1013 # load third int
0x9015 # subtract from result
0x2015 # (0xA) store in result
# RESULT -= INT4
0x1014 # load fourth int
0x9015 # subtract from result
0x2015 # store in result
0x0 # (0xe) HALT
0x0 # (0xf) pad
0x0 # (0x10) ZERO:
0x4 # INT1: first int
0x8 # INT2: second int
0x10 # INT3: third int
0x20 # INT4: fourth int
0x0 # RESULT:
The simple machine you should use to write this programming assignment can be found here:
http://devinriley.github.io/SimpleMachine/ (Links to an external site.)Links to an external site.
When you write your programs for this simple machine, do not place comments in the program. This simple machine does not take comments and will not perform your program if you include them as you are writing and running your program.
Here is a document giving a review of the simple machine and the instructions it uses: SimpleMachineSummary.pdf
Recommentation -- you may wish to write out your program's logic in simple machine assembly language in a seperate file than the one you will turn in. After you have written the assembly language program, change the program into simple machine machine language. Once your program is working in machine language you can then retranslate the machine language back into assembly language, and use the assembly language in comments such as were in add4
sub4
. You can then turn in the machine language, retranslated assembly language and your own descriptions as your comments in the file you turn in.
This process is similar to what an assembler does: takes assembly language and translates it into machine language for executing a program on a computer, allowing you to write assembly source code that it automatically translates into machine language for you.
The Simple Machine
The simple machine has a main memory consisting of 16-bit words.
The processor of our machine has seven 16-bit registers:
The pc or program counter holds the address of the next instruction to execute.
The ir or instruction register hold the current instruction being executed.
The accum or accumulator is the only true data register. All loads, stores, adds and subtracts reference the accumulator as at least one of the operands.
ctr is the counter register. Although data can be moved from the accum to the ctr (using the MVAC instruction) and the ctr can be added to the accumulator (using ADDC), its use mainly is as a simple counter. ctr is decremented by one using the DEC instruction, and all conditional branch instructions use it for comparisons. mar is the memory address register. This is the register that indicates the address of the next memory word to be referenced (either loaded from or stored to) areg is the address register. It is used solely for indirect memory operations. The LIA and SIA instructions use the address in areg as the source (LIA) or target (SIA) address for the data being placed in (LIA) or coming from (SIA) the accumulator. mbr is the memory buffer register. This register is the window to memory. When memory is retrieved, the word loaded goes here. When memory is stored, the word stored comes from here.
Besides having a very limited register set, our simple machine also has limited data pathways. These are
Simple Machine Instructions
Instruction words are 16-bits, divided into 4 bits of opcode, 2 bits reserved, and 10 bits of address: Below is a list of the opcodes for our class machine (from sim.h):
HALT 0x0 LOAD 0x1 STORE 0x2 ADDC 0x3 MVAC 0x4
# halt the machine # load accumulator from memory # store accumulator to memory # add counter to accumulator # move accumulator to counter
JEQ JLT JMP ADD SUB DEC
0x5 # jump to address if counter equals 0
0x6 # jump to address if counter is less than 0
0x7 # jump to address
0x8 # accumulator = memory + accumulator
0x9 # accumulator = memory - accumulator
0xA # decrement counter
from the instruction and loads it into the
# LA accumulator (load address)
takes the memory address
LA 0xB # LIA uses the memory address (load indirect address)
LIA 0xC # SIA stores the value in the areg (store indirect address)
in the areg
accumulator
to load memory into the accumulator
in the memory address found in the
SIA 0xD # MVAA moves the value in the accumulator to the areg
MVAA 0xE
Let's think about what a couple of these instructions actually do, and express them in our register transfer language. At the beginning of execution of each instruction, the instruction is in the instruction register.
| Instr. | Encoding | What's required |
| LOAD 4 | 0x1004 | Extract the address (4) from the instruction. get the memory at address 4 (put in mbr) Transfer it to the accumulator |
| JEQ 0x24 | 0x5024 | If the counter register is zero: set the pc to the address in the instruction |
| MVAC | 0x4000 | Move the accumulator to the counter |
Simple Machine Programs
Our Simple Machine is, of course, fictitious. Programs for the simulator consist of a sequence of up to 256 16-bit words. (Note that this is not the limit of the address capability of the Simple Machine (although there is a limit), but this is a reasonable limit for a program for our simulator.) When we create a program for the simulator, it first translates it from text to numbers. We represent the numeric data (the 16-bit words) in hexadecimal notation. For example, 0x4000 represents the MVAC instruction.
Following is a sample program for this machine.
# # this first test case simply copies # a data word from one location to another # the first word is always address 0, the # second at address 1, etc. # 0x1003 # (address 0) load word to copy from address 3 0x2004 # (address 1) store it in new location (address 4) 0x0000 # halt 0x4040 # (address 3) the word to copy 0x0000 # (address 4) the location to copy to
Let's look briefly at this program:
The first instruction (at address 0) has opcode 1 (load) and address 3. This loads the 16-bit word from
address 3 into the accumulator.
The next instruction (at address 1) has opcode 2 (store) and address 4. The accumulator is stored at
address 4.
The last instruction (at address 2) has opcode 0 (halt). Each program must end with this instruction.
The remaining words comprise the data area, which starts at address 3.
Note that in this program, every line of a Simple Machine program must begin with either a comment character # (for annotations) or a 0x (for the encoding of an instruction). Comments are allowed on instructions to the right of the encoded instruction. If a line begins with whitespace it will be interpreted as the end of the program.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
