Programming Model 1tutorial
Programming Model 1tutorial
A. Introduction
Objectives
At the end of this lab you should be able to:
▪ Use the CPU simulator to create basic CPU instructions
▪ Use the simulator to execute basic CPU instructions
▪ Use CPU instructions to move data to registers, compare values in registers,
push data to the stack, pop data from the stack, jump to address locations
and add values held in registers.
▪ Explain the function of the special CPU registers PC (Program Counter) and SR
(Status Register) and status flags OV, N and Z.
▪ Produce code for simple conditional statements and loops.
C. Basic Theory
The programming model of computer architecture defines those low-level
architectural components, which include the following
▪ CPU instruction set
▪ CPU registers
▪ Different ways of addressing data in instructions (i.e. addressing modes)
1
▪ Program stack
▪ Program creation and program running features
CPU Instruction
memory view Special CPU CPU registers view
registers view
“Add program
Program stack view
instructions” tab Program list view
“Create program“ tab
Image 1 – CPU Simulator window
The parts of the simulator relevant to this lab are described below. Please read this
information carefully and try to identify the different parts on the real CPU Simulator
window BEFORE attempting the following exercises. Use this information in
conjunction with the exercises that follow.
2
1. CPU instruction memory view
Image2 - Instruction memory
view
2. Special CPU registers view This view shows the set of CPU registers,
which have pre-defined specialist functions:
PC: Program Counter contains the address
of the next instruction to be executed.
IR: Instruction Register contains the
instruction currently being executed.
SR: Status Register contains information
pertaining to the result of the last executed
instruction.
SP: Stack Pointer register points to the value
maintained at the top of the program stack
(see below).
BR: Base Register contains current base
address.
MAR: Memory Address Register contains
the memory address currently being
Image 3 - Special CPU accessed.
registers view Status bits: OV: Overflow; Z: Zero; N:
Negative
3
3. CPU registers view
4
4. Program stack view
6. Program creation
Image 7 – Create program tab
5
Image 8 – Add program
instructions tab
Use ADD NEW… button to add a
new instruction; use EDIT…
button to edit the selected
instruction; use MOVE DOWN/
MOVE UP buttons to move the
selected instruction down or up;
use INSERT ABOVE…/INSERT
BELOW… buttons to insert a new
instruction above or below the
selected instruction respectively.
6
Part A: (Refer to the appendix for help with CPU instructions)
1. Create an instruction, which moves number 5 to register R00.
7. Execute it.
8. Note down which register the result is put in.
9. Create an instruction, which pushes the value in the above register to the top of
the program stack, and then execute it. Observe the value in Program Stack
(Image 5).
10. Create an instruction to push number 2 on top of the program stack and execute
it. Observe the value in Program Stack (Image 5).
7
13. Observe the value in the PC register. This is the address of the next instruction to
be executed. Make a note of the instruction it is pointing to?
14. Create an instruction to pop the value on top of the Program Stack into register
R02.
19. Create a compare instruction, which compares values in registers R04 and R05.
20. Manually insert two equal values in registers R04 and R05 (Image 4).
21. Execute the above compare instruction.
22. Which of the status flags OV/Z/N is set (i.e. box is checked)?
23. Manually insert a value in first register greater than that in second register.
24. Execute the compare instruction again.
25. Which of the status flags OV/Z/N is set?
26. Manually insert a value in first register smaller than that in second register.
8
27. Execute the compare instruction once again.
28. Which of the status flags OV/Z/N is set?
29. Create an instruction, which will jump to the first instruction if the values in
registers R04 and R05 are equal.
30. Test the above instruction by manually putting equal values in registers R04 and
R05, then first executing the compare instruction followed by executing the
jump instruction (Remember: You execute an instruction by double-clicking on
it). If it worked the first instruction should be highlighted.
31. Now that you have some understanding of basic CPU instructions and are able to
program the simulator here is a bit of challenge for you: preparing a little
program loop. Program loops are extremely useful and are very frequently used
by computer programs. Here’s what you have to do:
1. Create an instruction that moves number 0 into register R01
2. Create an instruction that adds number 1 to register R01
3. Create an instruction that compares number 10 and register R01
4. Create an instruction that jumps back to instruction 2 above if R01 is not
equal to number 10.
5. Create a HLT instruction.
Make a note of the instructions 1 to 5 you created above in the box below:
7. Now first click on the RESET PROGRAM button (see Program Control tab
in Image 1) and then highlight instruction 1 above. Next click on the RUN
button. Now observe the loop in action.
9
Part B: (Refer to the appendix for help with CPU instructions)
1. Produce the code for a conditional statement such that if the value in register
R02 is greater than (>) the value in register R01 then register R03 is set to 8. Test
it on the simulator.
2. Produce the code for a loop that repeats 5 times where the value of register R02
is incremented by 2 every time the loop repeats. Test it on the simulator.
3. The numbers 4, -3, 5 and -6 are manually pushed on top of stack in that order.
Produce the code for a routine that pops two numbers from top of stack
multiplies them and pushes the result back to top of stack. The routine repeats
this until there is only one number left on top of stack. Test the code on the
simulator.
10
Appendix – CPU Simulator Instruction Sub-set
Instruction Description
Data transfer instructions
Move a number to register; move register to register
e.g.
MOV
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
LDW Load a word (2 bytes) from memory to register
STB Store a byte from register to memory
STW Store a word (2 bytes) from register to memory
Push a number to top of program stack (TOS); push register to TOS
e.g.
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 program stack to register
e.g.
POP 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 a number to register; add register to register
e.g.
ADD 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 given instruction address unconditionally
11
e.g.
JMP 100 unconditionally jumps to instruction address location 100
JLT Jump to instruction address if less than (after last comparison)
JGT Jump to instruction address if greater than (after last comparison)
Jump to instruction address if equal (after last comparison instruction)
e.g.
JEQ JEQ 200 jumps to instruction 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)
CAL Jump to subroutine address
RET Return from subroutine
SWI Software interrupt (used to request OS help)
HLT Halt simulation
Comparison instruction
Compare a 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:
CMP
If R01 = R03 then the status flag Z will be set, i.e. the Z box is
checked.
If R01 < R03 then none 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
12