Laboratory Exercise 2 (MIDTERM)

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

IT1910

Laboratory Exercise
Basic CPU Instructions
Objectives:

At the end of the exercise, the students should be able to:

 Create instructions that will move data into the CPU registers;
 Create instructions that will compare values in CPU registers;
 Push data to the stack and pop data from the stack;
 Create instructions that will jump register content to another address location;
 Add values in CPU registers; and
 Demonstrate compare instruction’s effect on the status register flags.

Materials:

 Computer with CPU-OS Simulator Software Version 7.5.50

Basic Principles:

Table 1. CPU Simulator Instruction Set


INSTRUCTION DESCRIPTION AND EXAMPLES OF USAGE
Data Transfer Instructions
Move data to register; move register to register
ex.
MOV
MOV #2, R01 ; moves number 2 into register R01
MOV R01, R03 ; moves contents of register R01 into register R03
Load a byte from memory to register
ex.
LDB
LDB 1000, R02 ; loads one-byte value from memory location 1000
LDB @R00 R01 ; memory location is specified in register R00
Load a word (two [2] bytes) from memory to register
ex.
LDW
LDW 1000, R02 ; loads two-byte value from memory location 1000
LDW @R00, R01 ; memory location is specified in register R00
Store a byte from register to memory
ex.
STB
STB #2, 1000 ; stores value 2 into memory location 1000
STB R02, @R01 ; memory location is specified in register R01
Store a word (two [2] bytes) from register to memory
ex.
STW
STW R04, 1000 ; stores register R04 into memory location 1000
STW R02, @2000; memor y location is specified in memory 2000
Push data to top of hardware stack (TOS); push register to TOS
ex.
PSH
PSH #6 ; pushes number 6 on top of the stack
PSH R03 ; pushes the contents of register R03 on top of the stack
Pop data from top of hardware stack to register
POP ex.
POP R05 ; pops contents of top of stack into register R05

03 Laboratory Exercise 2 *Property of STI


Page 1 of 4
Arithmetic Instructions
Add number to register; add register to register
ex.
ADD #3, R02 ; adds number 3 to contents of register R02 and stores the result in
ADD
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
Jump to instruction address unconditionally
JMP ex.
JMP 100 ; unconditionally jumps to address location 100
Jump to instruction address if less than (after last comparison)
ex.
JLT JLT 120; jumps to address location 120 if the previous comparison instruction result
indicates that CMP operand 2 is less than CMP operand 1, i.e., the Nstatus flag is
set (also see CMP instruction)
Jump to instruction address if greater than (after last comparison)
ex.
JGT JGT 25 ; jumps to address location 25 if the previous comparison instruction result
indicates that CMP operand 2 is greater than CMP operand 1, i.e., flags Z and N not
set (also see CMP instruction)
Jump to instruction address if equal (after last comparison)
ex.
JEQ JEQ 1000 ; jumps to address location 1000 if the previous comparison instruction
result indicates that the two CMP operands are equal, i.e., the Z status flag is set
(also see CMP instruction)
Jump to instruction address if not equal (after last comparison)
ex.
JNE JNE 220 ; jumps to address location 220 if the previous comparison instruction result
indicates that CMP operand 2 is less than CMP operand 1, i.e., the N status flag is
set (also see CMP instruction)
Subroutine Instructions
MSF This instruction is required to set up a subroutine call (see below).
Jump to subroutine address
ex. To call a subroutine starting at address location 1000, use the following
CAL sequence of instructions:
MSF ; always needed just before the following instruction
CAL 1000 ; will cause a jump to address location 1000
Return from subroutine
RET ex. The last instruction in a subroutine must always be the following instruction:
RET ; will jump to the instruction after the last CAL instruction
System Instructions
SWI Software interrupt (used to request OS help)
HLT Halt simulation; this must be the last instruction.
ex.
HLT ; stops the simulation run (not the simulator itself)
Comparison Instruction
Compare number with register; compare register with register
ex.
CMP #5, R02 ; compare number 5 with the contents of register R02
CMP
CMP R01, R03 ; compare the contents of registers R01 and R03
Note: If R03 = R01, then the status flag Z will be set; if R03 > R01, then none of the
status flags Z and N will be set; if R03 < R01, then the status flag N will be set.
Input, Output Instructions
IN Get input data (if available) from an external IO device
Output data to an external IO device
ex. To display a string starting in memory address 120 (decimal) on console device,
OUT do the following (operand 2 = 0 for console output):
OUT 120, 0 ; the string is in address location 120 (direct addressing)
OUT @R02 0 ; register R02 has number 120 (indirect addressing)

Procedures:
Perform what is being asked.
1. Open the CPU-OS Simulator application.
2. Create a new program. Type Act2 – Surname as your program name in the Program Name text
box located in the program frame below the CPU instructions in memory (RAM). Ex: Act2 -
DelaCruz
3. Type in number zero (0) in the Base Address text box. Then, click the ADD button.
4. In the instructions tab, click ADD NEW button. This will display the Instructions: CPU0 window.
5. Create an instruction that moves number 5 to register R00. Refer to the data transfer instructions
table.
6. Execute the instruction done by simply double clicking on the item in the CPU INSTRUCTIONS
IN MEMORY (RAM) view.
7. Create an instruction that moves number 8 to register R01, then execute it. Observe the contents
of R00 and R01 in the Register Set view.
8. Create an instruction that adds the contents of R00 and R01, then execute it. Refer to the
arithmetic instructions table above.
9. In which register is the result stored? What is the value of that register? Write your answer on the
space provided.

The result stored is in R01, the value of that register is 13.

10. Create an instruction that pushes the answers in Step 9 to the top of the program stack, and then
execute it. Observe the result in PROGRAM STACK (RAM) view.
11. Create an instruction that will push number -2 on top of the stack and execute it.
12. Create an instruction that will compare the values in registers R00 and R01 then execute it.
13. 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.
14. Create an instruction that will unconditionally jump to the first instruction done in item #5, then
execute it.
15. Observe the Special CPU Registers value in the PC register. To which address is it pointing?
Explain your answer on the space provided.

It is pointing at 8100. Program Counter Register is 29, Status register is 3 and base register
is 0.
16. Create an instruction that will pop the value on top of the program stack into register R02, then
execute it.
17. Create an instruction that will pop the value on top of the program stack into register R03, then
execute it.
18. Execute the last instruction again, which is done in Step 17. What happened? Explain your
answer on the space provided.

An error has occurred stating that “Stack underflow, i.e. the stack is empty!”

19. Create an instruction that will compare the values in registers R04 and R05.
20. Manually insert two (2) equal values in registers R04 and R05. Execute the compare instruction
done in item #19.
21. Which of the status flags Z/N is set? Why? Explain your answer on the space provided.

22. Manually insert a value in register R05 greater than that in register R04. Execute the compare
instruction done in item #19.
23. Which of the status flags Z/N is set? Why? Explain your answer on the space provided.

24. Create an instruction that will conditionally jump to the first instruction done in item #5 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.
25. Test the instruction done in item #24 by manually putting values in registers R04 and R05. Then,
execute the compare instruction and execute 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).
26. Call the attention of the Lab Facilitator once finished.

You might also like