Tutorial
Tutorial
Tutorial/Laboratory 02
You are required for this tutorial to work in a group so get to your colleague next to you
and discuss on the requirements:
The lab exercises are a series of activities, which are carried out by the students
under basic guidelines. So, how is this tutorial conducted? The students are expected to
follow the instructions given in order to identify and locate the required information, to
act upon it, make notes of their observations and offer explanations for these
observations where this requested. In order to be able to do these activities you
should consult the information in Section D above and also frequently refer to the
Appendix for information on various CPU instructions you will be asked to create and
use. Remember, you need to carefully read and understand the instructions before
you attempt each activity.
Now, let us start. First you need to place some instructions in the Instruction Memory
View (see Image 2), representing the RAM in the real machine, before executing any
instructions. To do this, follow the steps below:
In the Program tab (see Image 7), first enter a Program Name, and then enter a Base
Address (this can be any number, but for this exercise use 100). Click on the ADD
button. A new program name will be entered in the Program List view (see Image 6). You
can use the SAVE… button to save instructions in a file. You can also use the LOAD…
button to load instructions from a file.
You are now ready to enter instructions into the CPU Simulator. You do this by
clicking on the ADD NEW… button in the Instructions tab (see Image 8). This will
display the Instructions: CPU0 window. You use this window to select and enter the CPU
instructions. Appendix lists some of the instructions this simulator uses and also gives
examples of their usage.
Now, have a go at the following activities (enter your answers in the text boxes
provided). A word of caution: Regularly save your code in a file in case the simulator
crashes in which case you can restart the simulator and re‐load your file.
1. In the Appendix at the end of this document, locate the instruction, which is used
to store one byte of data in a memory location. Use it to store number 65 in
address location 20 (all numbers are in decimal). This is an example of direct
addressing. Refer to Image 9 to see how to display the contents of data memory.
Make a note below of the instruction used:
STB #65, 20
Double click to
execute
2. Create an instruction to move decimal number 22 to register R01 and make a
note of it below. Execute this instruction and verify the result in R01.
Select Data
Transfer Tab
Select MOV
Enter value
22
Select Register
and choose R01
Double click to
execute
3. Create an instruction to store decimal number 51 in memory location the address
of which is currently stored in register R01. This is an example of indirect
addressing. Note the use of the “@” prefix next to R01 in this case.
Select Data
Transfer Tab
Enter value 51
Select STB
Select Register
and choose R01
Click New
Instruction
Double click to
execute
4. Make a note of what you see in data memory locations 20 and 22 (refer to Image
9 for help information on how to display the data memory).
41 and 33 (HEX)
5. Now, let’s create a loop. First, enter the following code. The # prefix is used to denote
a literal value thus distinguishing it from an address value which does not use it. R01
represents an arbitrary register; you can use any of the registers from R00 to R31.
MOV #0, R01
ADD #1, R01
CMP #5, R01
JNE 0
HLT
6. The above code is not quite ready yet. The JNE instruction uses a numeric value as
the address to jump to. In this case it is 0. This may not always be the case so in
order to make the code more flexible we can use labels to represent instruction
addresses. The simulator allows you to do this. Follow the instructions below for this:
Highlight the above MOV instruction (i.e. the one in the box above) Click on the
INSERT BELOW…. button
Type label name L0 in the box next to the ENTER LABEL button in the window you use
to enter instructions
Click the ENTER LABEL button
The new code should now look like this (modifications are in red colour):
3
4
7. As you can see, the label L0 represents the address of the instruction
immediately below it, i.e. the ADD instruction. So now the JNE instruction can use
L0 as the address to jump to. As the label L0 can represent any address this code
should work anywhere in memory making it very flexible. The $ sign indicates
that L0 is a label. The above code is now ready to run. To run this program,
follow the instructions below:
Click on the RESET PROGRAM button in the CPU Simulator window
Highlight the MOV instruction, i.e. the first instruction of the program Adjust
the speed slider to a value, say, nearest to the value 80
Click on the RUN button
After a short while the program should stop. If it appears to run too long then
click on the STOP button and check your code. Correct it if necessary and
repeat the above instructions once again.
When the program stops make a note of the value in R01 below
5
2
1
8. Now you’ll make a slight modification to the above program. Change the
program code so that the program loop is repeated as long as the value of
R01 is less than or equal to 3 (you may wish to refer to the Appendix for
this) and test it. When you get it right make a note of the value in R01 and
copy the new code below. Now, change the modified instructions back to the
original instructions (you can use the UNDO button for this – see Image 8
above).
1
4
MOV #0, R01
L0:
9. Ok, let’s create a simple subroutine. Enter the following new code. You need to
create a new label L1 at the start of the subroutine. This label represents the
starting address of the subroutine. You must enter the label using the ENTER
LABEL button only as explained in (6). Also, make sure you select the Direct Mem
radio button when entering the first operand value 24 of the OUT instruction:
L1:
OUT 24,0
RET
1
1
3
5
4
7
6
2
3
5
13. We need to make a small change to our subroutine. Currently the OUT
instruction uses direct memory addressing, i.e. the memory address 24 is part of the
instruction. We now wish to make it use indirect addressing in a way similar to that in
(3). So, you’ll need to place the memory address 24 in a register (any spare
register). Then you need to have the OUT instruction use this register indirectly
as the source of the address of the text to display. Run the code to test your
modification. Make a note of the modified part of the program code below. Use the
UNDO button to restore the instructions before this modification:
MOV #24, R02
OUT @R02, 0
5
New code inserted
14. Ok, let’s get a little bit more ambitious as a challenge. Let’s convert the loop into
another subroutine and then call it. So, now we will have two subroutines where one
calls the other. The following code represents this change. Notice that the HLT
instruction is changed to the RET instruction and the new instructions MSF, CAL and
HLT are added together with the new label L2 at the top of the code. CAL $L2
calls the subroutine with the loop and CAL $L1 calls the subroutine that displays the
text.
MSF
CAL $L2
HLT
L2:
MOV #0, R01
L0:
ADD #1, R01
MSF
CAL $L1
CMP #5, R01
JNE $L0
Now, first reset the program then highlight the first MSF instruction. Run the program
and verify the result in the console window as before.
1
1
Here is the edited code
15. Why stop here! Let’s make it a bit more interesting. The above code will do the loop
5 times and this number is fixed. For flexibility we can pass the number of loops as
a parameter to the subroutine (starting at label L2). For this we will use the PSH and
POP instructions (see the Appendix). Modify your code to look like the one below
and run it observing the displays on the console:
MSF
PSH #8
CAL $L2
HLT
L2:
POP R02
MOV #0, R01
L0:
ADD #1, R01
MSF
CAL $L1
CMP R02, R01
JNE $L0
2
3
3
Here is the output
16. Examine the above code and briefly explain how the parameter passing works:
8 is pushed on the stack and is popped in subroutine L2, there it is used to compare with the
value in R01.
It showed on above OUTPUT that 8 is the value on top of the stack which Explains why it is
executes last as Program Stack is LIFO , Last In First Out ,it has been pushed as Last In.
17. Finally, as a real challenge, modify the above code so that a second parameter is
passed to the subroutine (starting at label L2) in the same way as the first
parameter is passed. The second parameter is used to initialise the register R01 to
the value of this second parameter. Copy the modified code only to the point of the
MSF
PSH #8
PSH #0
CAL $L2
HLT
L2
POP R01
POP R02
L0
4
Part 2: Linux/Unix Operating System
5
APPENDIX A CPU SIMULATOR
A. Introduction
Objectives
At the end of this lab you should be able to:
Use direct and indirect addressing modes of accessing data in memory
Create an iterative loop of instructions
Display text on console using an IO instruction
Create a sub‐routine, call and return from subroutine
Pass parameters to a subroutine
The computer architecture tutorials are supported by simulators, which are created to
underpin theoretical concepts normally covered during the lectures. The simulators
provide visual and animated representation of mechanisms involved and enable the
students to observe the hidden inner workings of systems, which would be difficult or
impossible to do otherwise. The added advantage of using simulators is that they allow
the students to experiment and explore different technological aspects of systems
without having to install and configure the real systems.
C. Basic Theory
6
This section includes some basic information on the simulator, which should enable the
students to use the simulator. The tutor(s) will be available to help anyone
experiencing difficulty in using the simulator. The simulator for this lab is an
application running on a PC running MS Windows operating system.
The main simulator window is composed of several views, which represent different
functional parts of the simulated processor. These are shown in Image 1 below and are
composed of
7
CPU Instruction memory
Special CPU registers
CPU (general purpose) registers
Program stack
Program creation and running features
Memory in which data is stored
Input, output console
8
instructions. The instructions are displayed as addresses: the physical address
sequences of low‐level instruction (PAdd) and the logical address
mnemonics (assembler‐level format) and not (LAdd). This view also displays
as binary code. This is done for clarity and the base address (Base)
makes code more readable by humans. against each instruction. The
sequence of instructions
belonging to the same program
Each instruction is associated with two will have the same base
address.
2. Special CPU registers view This view shows the set of CPU registers,
which have pre‐defined specialist functions: PC:
Program Counter contains the address of the
next instruction to be executed.
IR: Instruction Register contains the
instruction currently being executed.
SR: Status Register contains information
pertaining to the result of the last executed
instruction.
SP: Stack Pointer register points to the value
maintained at the top of the program stack
(see below).
BR: Base Register contains current base
address.
MAR: Memory Address Register contains
the memory address currently being
Image 3 ‐ Special accessed.
CPU registers view Status bits: OV: Overflow; Z: Zero; N:
Negative
9
3. CPU registers view
10
Image 5 ‐ Program stack view
11
Image 8 – Add
program instructions
tab
12
Image 9 ‐ Program data memory view
13
The CPU instructions that access that part of the memory containing data can write or read the
data in addressed locations. This data can be seen in the memory pages window shown in Image
9 above. You can display this window by clicking the SHOW PROGRAM DATA MEMORY…
button shown in Image 6 above. The Ladd (logical address) column shows the starting
address of each line in the display. Each line of the display represents 8 bytes of data. Columns
B0 through to B7 represent bytes 0 to 7 on each line. The Data column shows the displayable
characters corresponding to the 8 bytes. Those bytes that correspond to non‐displayable
characters are shown as dots. The data bytes are displayed in hex format only. For example,
in Image 9, there are non‐zero data bytes in address locations 19 and 37. These data bytes
correspond to displayable characters capital A and B.
To change the values of any bytes, first select the line(s) containing the bytes. Then use the
information in the Initialize Data frame to modify the values of the bytes in the selected line(s)
as Integer, Boolean or String formats. You need to click the UPDATE button to make the
change.
8. IO console view
14
Instruction Description
Data transfer instructions
Move data to register; move register to register e.g.
MOV #2, R01 moves number 2 into register R01
MOV
MOV R01, R03 moves contents of register R01 into register R03
Arithmetic instructions
Add number to register; add register to register e.g.
ADD #3, R02 adds number 3 to contents of register R02 and stores the
result in register R02.
ADD
ADD R00, R01 adds contents of register R00 to contents of register R01 and
stores the result in register R01.
15
e.g.
JMP 100 unconditionally jumps to address location 100