Sie sind auf Seite 1von 13

Computer Organization & Architecture

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.

Please record your work in the boxes as you go along.

1. Create an instruction that moves number 5 to register R00.

1. Execute the above instruction by simply double clicking on it in the CPU INSTRUCTIONS
IN MEMORY (RAM) view.

2. Create an instruction that moves number 8 to register R01.

3. Execute it.

4. Observe the contents of R00 and R01 in the Register Set view.

5. Create an instruction that adds the contents of R00 and R01.

6. Execute it.

7. Write down which register the result is stored in.


Result is stored in R01 register.

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.

11. Execute it.

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.

13. Create an instruction to unconditionally jump to the first instruction.

14. Execute it.

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.

19. Execute it.

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.

23. Execute the compare instruction in step 22 above.

24. Which of the status flags Z/N is set? Why?

The zero flag is set because both values are equal.

25. Manually insert a value in register R05 greater than that in register R04.

26. Execute the compare instruction in step 22 above.

27. Which of the status flags Z/N is set? Why?

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.

29. Execute the compare instruction in step 22 above.

30. Which of the status flags Z/N is set? Why?


Flag N is set because value in R04 was greater than 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.

Please record your work in the boxes as you go along.

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.

2. Create an instruction to move decimal number 22 to register R01.

3. Create an instruction to store decimal number 51 in address location currently


stored in register R01. This is an example of indirect addressing. Note the use of
the @ prefix with R01.

4. Make a note of what you see in memory locations 20 and 22?


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
need 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

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:

Highlight the MOV instruction

Click on the INSERT BELOW… button MOV #0, R01

Type label name L1 in the box next to the ENTER LABEL button L1

Click the ENTER LABEL button ADD #1, R01

The new code should look like this: CMP #5, R01

JNE 0

HLT

Now, highlight the JNE instruction

Click on the EDIT… button MOV #0, R01

Select L1 in the drop-down list under the Source Operand section L1

Click the EDIT button ADD #1, R01

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:

Click on the RESET PROGRAM button

Highlight the MOV instruction, i.e. the first instruction of the program

Adjust the speed slider to a value 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

The value in R01 is 5 because the loop runs 5 times.

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:

Click on the SHOW PROGRAM DATA MEMORY… button.

In the displayed window highlight the line 0024 under LAdd column

Under Initialise Data click on the String radio button

Enter some text in the text box, e.g. My name is Besim

Click the UPDATE button

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):

MOV #0, R01

L1

ADD #1, R01

MSF

CAL $L0

CMP #5, R01

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:

Click on the RESET PROGRAM button

Highlight the MOV instruction, i.e. the first instruction of the program

Adjust the speed slider to a value nearest to the value 80

Click on the RUN button


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 (4). So, you’ll need to place the memory address 24 in a 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 program code below:
Exercises - 3
Enter the instructions you create in order to answer the questions. Refer to Appendix at the end of
this document to find the details on the desired instructions. You are expected to execute the
instructions you created on the simulator in order to verify your answers.

A. Loops using jump and compare instructions:


1. Write a conditional statement such that if R02 is greater than (>) R01 then R03 is
set to 8. (Use R01 as the first operand and R02 as the second operand)

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