You are on page 1of 6

LABORATORY EXERCISE #6: Investigating CPU Instructions

OBJECTIVES
At the end of this laboratory exercise, you should be able to:

 Enter CPU instructions using the CPU simulator


 Describe the effect of compare instruction on CPU status flags Z and N
 Construct a loop and explain jump instructions that use Z and N flags
 Use direct addressing to access memory location(s)
 Use indirect direct addressing to access memory location(s)
 Construct a subroutine and call it
 Pass parameter(s) to the subroutine

I. MATERIALS NEEDED
DESCRIPTION QUANTITY
Laptop 1
CPU-OS Simulator 1

II. ACTIVITIES
1. Save your work at regular intervals.

2. The CPU simulator is work in progress so it may crash from time to time. If
this happens then restart it and load your latest code.

3. The simulator is there to help you understand the theory covered in lectures
so make the most of it.
Learning objective: To enter CPU instructions and describe effect of compare instruction on
CPU status flags Z and N.

In theory In practice
The CPU instruction sets are often 1. Enter the following code and run it:
grouped together into categories
containing instructions with related MOV #1, R01
functions. MOV #2, R02
ADD R01, R02
This exercise familiarizes you with the
way the instructions are entered using 2. Add the following code and note the states of the status
the simulator. flags Z and N after each compare instruction is executed:

The most encountered instructions CMP #3, R02 Z N


involve data movements, e.g. the MOV CMP #1, R02 Z N
instruction. CMP #4, R02 Z N
Another common instruction is the
comparison instruction, e.g. CMP. This Explain your observations of the states of the status flags
instruction sets or resets the status flags after the compare instructions above:
Z and N as a record of the result of the
comparison operation.
3. Save the above code

Learning objective: To construct a loop and explain jump instructions that use Z and N flags
In theory In practice
The computer programs often contain 4. Add the following code and run it (ask your tutor how to
loops where the same sequence of code enter a label):
is repeatedly executed until or while
certain condition is met. Loops (or MOV #0, R01
Label1
iterative statements) are the most useful
ADD #1, R01
features of programming languages.
CMP #3, R01
At the instruction level, loops use JLT $Label1
conditional jump instructions to jump
back to the start of the loop or to jump Summarize what the above code is doing in plain
out of the loop. English:

This exercise is created to demonstrate


the use of the jump instruction which
often uses the result of a compare
instruction. Such jump instructions use
the Z and the N status flags to jump or 5. Modify the above code such that R01 is incremented by 1
not jump. until it reaches the value of 4. Copy the code below:

6. Save the above code

Learning objective: To use direct addressing to access memory location(s)


In theory In practice
Although instructions moving data to or 7. Add the following code and run:
from registers are the fastest
instructions, it is still necessary to move STB #h41, 16
data in or out of the main memory LDB 16, R03
(RAM) which is a much slower process. ADD #1, R03
STB R03, 17
Examples of instructions used to store
into or get data out of memory are Make a note of what you see in the program’s data area:
explored here. The method used here
uses the direct addressing method, i.e.
the memory address is What is the significance of h in h41 above?
directly specified in the instruction itself.

Modify the above code to store the next two characters in


the alphabet. Write down the modified code below:

8. Save the above code.


Learning objective: To use indirect addressing to access memory location(s)
In theory In practice
There are circumstances which make 9. Add the following code and run:
direct addressing unsuitable and
inflexible method to use. Label2
MOV #16, R03
In these cases, indirect addressing is a MOV #h41, R04
more suitable and flexible method to Label3
use. In indirect addressing, the address STB R04, @R03
of the memory location is not directly ADD #1, R03
included in the instruction but is stored ADD #1, R04
in a register which is the used in the CMP #h4F, R04
instruction. JNE $Label3

This exercise introduces an indirect Make a note of what you see in the program’s data area:
method of accessing memory. This is
called register indirect addressing. There
is also the memory indirect addressing Explain the significance of @ in @R03 above:
but this is left as an exercise for you.

10. Save the above code.

Learning objective: To construct a subroutine and call it


In theory In practice
Another very useful feature of 11. Add the following code but do NOT run it yet:
programming languages is subroutines.
These contain sequences of instructions MSF
which can be executed many times. If a CAL $Label2
subroutine was not available the same
sequence of instructions would have Now convert the code in (6) above into a subroutine by
been repeated many times increasing inserting a RET as the last instruction in the subroutine.
the code size.
Make a note of the contents of the PROGRAM STACK
At instruction level, a subroutine is after the instruction MSF is executed.
called by an instruction such as CAL.
This effectively is a jump instruction but
Make a note of the contents of the PROGRAM STACK
it also saves the address of the next
after the instruction CAL is executed:
instruction. This is later used by a
subroutine return instruction, e.g. RET
to return to the previous sequence of What is the significance of the additional information?
instructions.

12. Save the above code.


Learning objective: To pass parameter(s) to the subroutine
In theory In practice
Useful as they are the subroutines 13. Let’s make the above subroutine a little more flexible. Suppose
are not very flexible without the we wish to change the number of characters stored when
use of the subroutine parameters. calling the subroutine. Modify the calling code in (7) as below:
These are values passed to the
MSF
subroutine to be consumed inside
PSH #h60
the subroutine.
CAL $Label2
The common method of passing
parameters to the subroutines is Now modify the subroutine code in (6) as below and run the
using the program stack. The above calling code:
parameters are pushed on the
Label2
stack before the call instruction.
MOV #16, R03
These are then popped off the
MOV #h41, R04
stack inside the subroutine and POP R05
consumed. Label3
This exercise is created to STB R04, @R03
ADD #1, R03
demonstrate this mechanism. ADD #1, R04
CMP R05, R04
JNE $Label3
RET

Add a second parameter to change the starting address of the


data as a challenge and write the code in the box above.

13. Save the above code.


Appendix ‐ Simulator Instruction Sub‐set
Instruction Description
Data transfer instructions
MOV Move data to register; move register to register
e.g.
MOV #2, R01 moves number 2 into register R01
MOV R01, R03 moves contents of register R01 into register R03
LDB Load a byte from memory to register
e.g.
LDB 1022, R03 loads a byte from memory address 1022 into R03
LDB @R02, R05 loads a byte from memory the address of which is in R02
LDW Load a word (2 bytes) from memory to register
Same as in LDB but a word (i.e. 2 bytes) is loaded into a register
STB Store a byte from register to memory
STB R07, 2146 stores a byte from R07 into memory address 2146
STB R04, @R08 stores a byte from R04 into memory address of which is in
R08
STW Store a word (2 bytes) from register to memory
Same as in STB but a word (i.e. 2 bytes) is loaded stored in memory
PSH Push data to top of hardware stack (TOS); push register to TOS
e.g.
PSH #6 pushes number 6 on top of the stack
PSH R03 pushes the contents of register R03 on top of the stack
POP Pop data from top of hardware stack to register
e.g.
POP R05 pops contents of top of stack into register R05
Note: If you try to POP from an empty stack you will get the error message
“Stack underflow”.

Arithmetic instructions
ADD 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 R00, R01 adds contents of register R00 to contents of register R01
and stores the result in register R01.

SUB Subtract number from register; subtract register from register

MUL Multiply number with register; multiply register with register

DIV Divide number with register; divide register with register

Control transfer instructions


JMP Jump to instruction address unconditionally
e.g.
JMP 100 unconditionally jumps to address location 100 where there is
another instruction
JLT Jump to instruction address if less than (after last comparison)

JGT Jump to instruction address if greater than (after last comparison)


JEQ Jump to instruction address if equal (after last comparison instruction) e.g.
JEQ 200 jumps to address location 200 if the previous comparison
instruction result indicates that the two numbers are equal, i.e. the Z status
flag is set (the Z box will be checked in this case).

JNE Jump to instruction address if not equal (after last comparison)

MSF Mark Stack Frame instruction is used in conjunction with the


CAL instruction. e.g.
MSF reserve a space for the return address on program stack CAL
1456 save the return address in the reserved space and jump to
subroutine in address location 1456

CAL Jump to subroutine address (saves the return address on program stack)
This instruction is used in conjunction with the MSF instruction. You’ll need
an MSF instruction before the CAL instruction. See the example above

RET Return from subroutine (uses the return address on stack)

SWI Software interrupt (used to request OS help)

HLT Halt simulation

Comparison instruction
CMP Compare number with register; compare register with register
e.g.
CMP #5, R02 compare number 5 with the contents of register R02
CMP R01, R03 compare the contents of registers R01 and R03
Note:
If R01 = R03 then the status flag Z will be set, i.e. the Z box is checked. If
R03 > R01 then non of the status flags will be set, i.e. none of the status
flag boxes are checked.
If R01 > R03 then the status flag N will be set, i.e. the N status box is
checked.

Input, output instructions


IN Get input data (if available) from an external IO device

OUT Output data to an external IO device


e.g.
OUT 16, 0 outputs contents of data in location 16 to the console (the
second parameter must always be a 0)

Source:
 http://en.freedownloadmanager.org/Windows-PC/CPU-OS-Simulator-
FREE.html
 http://www.teach-sim.com/
 http://softdeluxe.com/CPU-OS-Simulator-1264697/download/

You might also like