Exercises 1
You now need to enter instructions into the CPU simulator. You do this by first creating a new
program and then clicking on the ADD NEW… button in the Edit Program tab. This will display the
Instructions: CPU0 window. Use this window to enter the instructions. You’ll find a list of useful CPU
simulator instructions and examples at the end of the above document. You can also edit the
instructions by using the edit commands available in the Edit Program tab.
1. Execute the above instruction by simply double clicking on it in the CPU INSTRUCTIONS
IN MEMORY (RAM) view.
3. Execute it.
4. Observe the contents of R00 and R01 in the Register Set view.
6. Execute it.
8. Create an instruction that pushes the above result to the top of the program stack, and
then execute it. Observe the result in PROGRAM STACK (RAM) view.
9. Create an instruction to push number -2 on top of the stack and execute it.
10. Create an instruction to compare the values in registers R00 and R01.
12. Record the status (i.e. set or reset) of the Z/N flags of the status register. If the box is
checked then means set otherwise means not set.
15. Observe the value in the PC register. What address is it pointing to? Explain.
The program counter register is set to zero because we have jumped at the start of program.
16. Create an instruction to pop the value on top of the program stack into register R02.
17. Execute it.
18. Create an instruction to pop the value on top of the program stack into register R03.
20. Execute the last instruction again. What happened? Briefly explain.
The stack is empty because we already popped all values from stack and the value of R03 is set to zero.
21. Create a compare instruction that compares values in registers R04 and R05.
22. Manually insert two equal values in registers R04 and R05.
25. Manually insert a value in register R05 greater than that in register R04.
No flags are set because they were neither equal nor R05 was less than R04
28. Manually insert a value in register R04 greater than that in register R05.
31. Create an instruction that will conditionally jump to the first instruction if the values in
registers R04 and R05 are equal (Note: You will need to execute the compare instruction
first before you execute the jump instruction if you change values in R04 and R05)
32. Test this instruction by manually putting values in registers R04 and R05 and then first
executing the compare instruction and then executing the jump instruction (i.e. You will
need to execute the compare instruction first before you execute the jump instruction
every time you change values in R04 and R05)
Exercises 2
You now need to enter instructions into the CPU simulator. You do this by first creating a new
program and then clicking on the ADD NEW… button in the Edit Program tab. This will display the
Instructions: CPU0 window. Use this window to enter the instructions. You can also edit the
instructions by using the edit commands available in the Edit Program tab. You’ll find a list of useful
CPU simulator instructions and examples in CPU Simulator Instruction Set.
When a new program is created by the simulator a dedicated memory space is also created for it. The
program can then store data in this memory space using the store instruction. It can also load data
from this memory into a register using the load instruction. This tutorial uses these instructions. To
see the contents of the program’s memory click the SHOW PROGRAM DATA MEMORY… button.
You’ll also need to see the simulated console during the tutorial. The console is used to display text
and input data. You can show the console window by clicking on the INPUT OUTPUT… button.
1. In the Appendix, locate the instruction, which is used to store a 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.
JNE 0
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 the case so in order
to make the code more flexible we will use labels to represent instruction
addresses. The simulator allows you to do this. Follow the instructions below to
do this:
Type label name L1 in the box next to the ENTER LABEL button L1
The new code should look like this: CMP #5, R01
JNE 0
HLT
The new code should look like this: CMP #5, R01
JNE $L1
7. As you can see, the label L1 represents the address of the instruction immediately
below it, i.e. the ADD instruction. So now the JNE instruction can use L1 as the
address to jump to. As the label L1 can represent any address this code should
work anywhere in memory making it very flexible.
The above code is now ready to run. To run this program follow the instructions below:
Highlight the MOV instruction, i.e. the first instruction of the program
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.
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. Make a note of the new code below:
9. Let’s create a simple subroutine. Enter the following new code. You need to create
a new label L0 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:
L0
OUT 24, 0
10.The above subroutine code simply displays the text starting at data memory
location 24 and returns (see RET instruction in appendix). For it to work there
needs to be some text in location 24. You can do this manually by following the
steps below:
In the displayed window highlight the line 0024 under LAdd column
11.Now, a subroutine is of no use by itself. For it to be useful your program must call
it using the instructions MSF followed by CAL. The CAL instruction needs to
include the starting address of the subroutine. Let’s modify our code so that the
above subroutine is called and it displays the text repeatedly in a loop. For
example, using the code in (6) the modified program should look something like
this (you need to modify the code you created in (7) above):
L1
MSF
CAL $L0
12.The above code is now ready to run. In order to see the displayed text you need to
show the console window. Click on the INPUT OUTPUT… button which will
display the simulated console window. To run this program follow the
instructions below:
Highlight the MOV instruction, i.e. the first instruction of the program
2. Write a conditional statement such that if R02 is less than or equal to (<=) R01
then R03 is set to -5. (Use R01 as the first operand and R02 as the second operand)
3. Write a conditional statement such that if R01 = 0 then R03 is set to 5 else R03 is
set to R01 plus 1.
4. Write a loop that repeats 5 times where R02 is incremented by 2 every time the
loop repeats.
5. Write a loop that repeats while R04 is > 0. Set the initial value of R04 to 8.
6. Write a loop that repeats until R05 is > R09. Set the initial values of R05 to 0 and
R09 to 12.
B. Instructions for writing to and reading from memory (RAM) :
The following instructions access the program’s data memory. You can display this memory so that
you can observe the results by referring to Image 1 and the related text in section D above.
9. Locate the instruction that stores a byte in memory and use it to store number 65
in memory address location 20 (this uses memory direct addressing method).
10. Move number 51 into register R04. Use the store instruction to store the
contents of R04 in memory location 21 (this uses register direct addressing method).
11. Move number 22 into register R04. Use this information to indirectly store
number 58 in memory (hint: you will need to use the ‘@’ prefix for this – see the list
of instructions in appendix) - (this uses register indirect addressing method).
12. Locate the instruction that loads a byte from memory into a register. Use this to
load the number in memory address 22 into register R10.
C. Putting it together:
13. Challenge #2: write a loop in which 10 numbers from 48 to 57 are stored as
single bytes in memory starting from memory address 24. You should use register
indirect addressing method of storing the numbers in memory (see exercise 11
above).
14. Challenge #3: write a loop in which the numbers stored in memory by the
program in (13) above are copied to a different part of the memory starting from
address location 80