Lab 4 Worksheet
Lab 4 Worksheet
pdf,
Computer Cycling. Lab11_Manual.pdf, Low-level Languages.
Pseudocode.
[80 marks] In all of the problems, you must show your work to qualify for the mark.
Type your answer in this worksheet after each question. Submit the PDF version of the completed
worksheet with D2L.
Introduction
Super Simple CPU is a simple CPU simulator with 16 memory locations. Memory addresses are from 0 to FF or
15.
Each word in memory is 16 bits or 2 bytes. It is either instruction or data. Numbers are stored in two's
complement notation.
The input and output devices are merely text areas to the left.
There are three registers:
PC (Program Counter): contains the address of the next instruction to be executed
The Instruction Register (IR): contains a copy of the instruction being executed
The Accumulator (A register): used to hold data and results of operations
Each instruction is 16 bits containing:
4-bit opcode: represents the instruction
12-bit operand holds one of the following:
o a constant (like LDI)
o the address of where the operand is found (like ADD, SUB, STO, and LOD)
o nothing (like STP, IN, OUT)
As the program runs, it cycles through the basic instruction cycle, which is called the fetch-execute cycle. The
steps are shown in the blue text area in the middle.
In Super Simple CPU, the instructions are shown in machine language (binary codes) as well as assembly
(mnemonic codes).
Learning Objectives
At the end of this lab, you should be able to:
analyze the fetch-execute cycle of computers
analyze machine language programs
analyze assembly language programs
understand, write simple pseudocodes
1
Lab Readings
1. Chapter 5 - Computer Components
2. Chapter 6 - Pseudocodes
3. Lab 7 Computer Cycling (Lab7_Manual.pdf)
4. Lab 11 Low-Level Languages (Lab11_Manual.pdf)
Lab Questions
1. Lab 7 Computer Cycling (Lab7_Manual.pdf)
a. [3] Exercise 1 – Note there are 4 instructions in this program (and not 3 as stated in the pdf file), STP is
an instruction and causes one fetch-execute cycle.
b. [6] Repeat Exercise 1 for examples 2, 3 in the super simple cpu applet.
c. [3] Exercise 2
d. [4] Exercise 3
2. [2] What is the printout of the following pseudocode if the user inputs 4 and 10?
2
Display "Enter two numbers: "
Input num1
Input num2
Set sum to num1 + num2
Set average to sum / 2
Display "Average of " + num1 + " and " + num2 + " is " + average
3. [2] (Input and Output) Write a pseudocode for example 2 of Super Simple CPU Applet.
3
a. [4] Trace the code, write the values of Accumulator, Memory (ONE), and Memory (TWO) in a table
(sample table is shown below), and record their changes after each instruction.
LDI 6 6 1 2
4
6. Given the following "Super Simple CPU" program:
LOD X ; Load X into accumulator
ADD X ; Add the value in memory location X to accumulator
ADD X ; Add the value in memory location X to accumulator
SUB Y ; Subtract the value in memory location Y from
accumulator
DONE STO Z ; Store accumulator in memory location Z
STP ; Stop the program
X DAT 3 ; A data value, the constant 3
Y DAT 5 ; A data value, the constant 5
Z DAT 0 ; A data value, the constant 0
a. [4] Trace the code, write the values of Accumulator, Memory (X), Memory (Y), and Memory (Z) in a table
shown below (sample table).
LOD X 3 3 5 0
d. [4] Exercise 3
5
8. [3] (Counting Loop) Write a pseudocode that performs example 5 of Super Simple CPU Applet.
9. [8] Given the following "Super Simple CPU" program, assume three different values for INPUT (1, 2, or 3).
For each input value, trace the code by writing the values of Accumulator, Memory (ONE), Memory (TWO),
and OUTPUT in a table (shown below) and recording their changes after each instruction. You should have 3
trace tables for 3 different values of input.
INP ; Assume three different values for input (1,2,3)
SUB ONE
JZR ZERO
SUB TWO
JNG NEG
ADD TWO
OUT
STP
NEG ADD TWO
OUT
STP
ZERO OUT
STP
ONE DAT 1 ; A data value, the constant 1
TWO DAT 2 ; A data value, the constant 2
6
10. Given the following pseudocode:
Set num to 10
WHILE (num is greater than 0)
Print num
Set num to num – 2
a. [4] Trace the program by writing the values of variables, conditions, and printout in a table (shown below).
c. [1] Explain the function of the program in one sentence. (Hint: For the function of the program, you can
explain the printout of the program).
7
11. Given the following pseudocode, assume the sequence of input numbers is: 4, 5, -1, 2, -4, -2, 6, 8. (Note: The
program may not read all these numbers).
Set sum to 0
Set count to 1
WHILE (count <= 4)
Read number
Set sum to sum + number
Increment count
Write "Sum is " + sum
[4] Trace the program by writing the values of variables, conditions, and printout in a table (shown below).
[1] Explain the function of the program in one sentence. (Hint: For the function of the program, you can
explain the printout of the program in terms of input values).
8
12. Given the following pseudocode, assume the sequence of input numbers is: 4, 5, -1, 2, -4, -2, 6, 8, -1, 0.
(Note: The program may not read all these numbers).
Set sum to 0
Set count to 1
WHILE (count <= 4)
Read number
IF (number > 0)
Set sum to sum + number
Increment count
Write "Sum is " + sum
[4] Trace the program by writing the values of variables, conditions, and printout in a table (shown below).
[1] Explain the function of the program in one sentence. (Hint: For the function of the program, you can
explain the printout of the program in terms of input values).