Advanced CPU Instructions: Laboratory Exercise #5
Advanced CPU Instructions: Laboratory Exercise #5
Score: _____
Laboratory Exercise #5
Advanced CPU Instructions
Objectives:
enter CPU instructions and describe the effects of COMPARE instruction on CPU status flags Z and
N,
use the MOV instruction in doing programs
construct a loop and explain JUMP instructions’ use on Z and N flags,
use direct and indirect addressing to access memory location(s),
construct a subroutine and call it, and
pass parameter(s) to the subroutine
Materials:
CPU-OS Simulator
Basic Principles:
The CPU instruction sets are often grouped together into categories that contains instructions with related
functions. The most encountered instruction is the MOV instruction which involves data movements.
Another instruction is the comparison instruction, e.g. CMP which involves setting or resetting of the status
flags Z and N.
The most useful feature of programming languages are the Loops or Iterative statement which involves
repetition of execution of codes until or while a condition is met. At the instruction level, it uses conditional
jump instructions to jump back to the start of the loop or to jump out of the loop.
Although instructions moving data to or from registers are the fastest instructions, it is still necessary to move
data in or out of the main memory (RAM). This method uses the direct addressing method in which memory
address is directly specified in the instruction itself.
But there are some instances or circumstances in which direct addressing is unsuitable and inflexible method
to use. In that case, an indirect addressing is use in which, the address of the memory location is not directly
included in the instruction but is stored in a register.
Another very useful feature of programming languages is subroutines which involves execution of sequences
of instructions many times. If however, it was not available, the same sequence of instructions would have
been repeated many times which increases the code size.
At the instruction level, a subroutine is called by an instruction known as CAL. This effectively jumps an
instruction but also saves an address of the next instruction. This is later used by a subroutine return
instruction, e.g. RET to return to the previous sequence of instructions.
Useful as they are, the subroutines are not very flexible without the use of the subroutine parameters.
Subroutine parameters are values that are passed to a subroutine to be consumed inside it. The common
method of passing it to the subroutines is through a program stack. The parameters are pushed on the stack
before the call instruction. These are then popped off the stack inside the subroutine and consumed.
Procedures:
1. Enter the following codes below and run it using a CPU-OS Simulator. Note the states of the status
flags Z and N after each compare instruction is executed and explain your observations.
2. Add the following code below and run it. Summarize what the code is doing in plain English:
To create a label, insert an instruction using the miscellaneous tab, use the NOP, write label 1 and
click the ENTER LABEL button.
3. Modify the above code by incrementing R01 to 1. By doing such, the last value of R01 becomes 4.
4. Add the following code and run. Click “SHOW PROGRAM DATA MEMORY…” button to see result
and make a note of what you see in the data.
STB #h41, 16
LDB 16, R03
ADD #1, R03
STB R03, 17
What is the significance of h in h41 above? Modify the above code to store the next two characters in
the alphabet. Write down the modified code below:
Label2
MOV #16, R03
MOV #h41, R04
Label3
STB R04, @R03
ADD #1, R03
ADD #1, R04
CMP #h4F, R04
JNE $Label3
6. Convert program code in Number 5 into a subroutine by means of adding the following code before
Label2 and by inserting a RET as the last instruction in the subroutine.
MSF
CAL $Label2
Make a note of the contents of the PROGRAM STACK after an instruction MSF and an instruction
CAL are executed. What is the significance of the additional information?
.
7. Let’s make the above subroutine a little more flexible. Suppose we wish to change the number of
characters stored when calling the subroutine. Modify the calling and subroutine code in (6) as below:
MSF
PSH #h60
CAL $Label2
Label2
MOV #16, R03
MOV #h41, R04
POP R05
Label3
STB R04, @R03
ADD #1, R03
ADD #1, R04
CMP R05, R04
JNE $Label3
RET
Add a second parameter to change the starting address of the data as a challenge.
MSF
PSH #h60
PSH #16
CAL $Label2
Label2
POP R03
MOV #h41, R04
POP R05
Label3
STB R04, @R03
ADD #1, R03
ADD #1, R04
CMP R05, R04
JNE $Label3
RET