ProgrammingModel1
A.Introduction
Objectives
Attheendofthislabyoushouldbeableto:
UsetheCPUsimulatortocreatebasicCPUinstructions
UsethesimulatortoexecutebasicCPUinstructions
UseCPUinstructionstomovedatatoregisters,comparevaluesinregisters,
pushdatatothestack,popdatafromthestack,jumptoaddresslocations
andaddvaluesheldinregisters.
ExplainthefunctionsofspecialCPUregisterssuchasthePC,SRandSP
registers.
B.Processor(CPU)Simulators
Thecomputerarchitecturetutorialsaresupportedbysimulators,whicharecreated
tounderpintheoreticalconceptsnormallycoveredduringthelectures.The
simulatorsprovidevisualandanimatedrepresentationofmechanismsinvolvedand
enablethestudentstoobservethehiddeninnerworkingsofsystems,whichwould
bedifficultorimpossibletodootherwise.Theaddedadvantageofusingsimulators
isthattheyallowthestudentstoexperimentandexploredifferenttechnological
aspectsofsystemswithouthavingtoinstallandconfiguretherealsystems.
C.BasicTheory
Theprogrammingmodelofcomputerarchitecturedefinesthoselowlevel
architecturalcomponents,whichincludethefollowing
CPUinstructionset
CPUregisters
Differentwaysofaddressinginstructionsanddataininstructions
Italsodefinesinteractionbetweentheabovecomponents.Itisthislowlevel
programmingmodelwhichmakesprogrammedcomputationspossible.
D.SimulatorDetails
Thissectionincludessomebasicinformationonthesimulator,whichshouldenable
thestudentstousethesimulator.Thetutor(s)willbeavailabletohelpanyone
experiencingdifficultyinusingthesimulator.Thesimulatorforthislabisan
applicationrunningonaPCrunningMSWindowsoperatingsystem.
Themainwindowiscomposedofseveralviews,whichrepresentdifferentfunctional
partsofthesimulatedprocessor.TheseareshowninImage1belowandare
composedof
CPUInstructionmemory
SpecialCPUregisters
CPU(generalpurpose)registers
Programstack
Programcreationandrunningfeatures
CPUInstruction
memoryview
Addprogram
instructionstab
Createprogramtab
CPUregistersview
SpecialCPU
registersview
Programlistview
Programstackview
Image1CPUSimulatorwindow
Thepartsofthesimulatorrelevanttothislabaredescribedbelow.Pleasereadthis
informationcarefullyandtrytoidentifythedifferentpartsontheCPUSimulator
windowBEFOREattemptingthefollowingexercises.Usethisinformationin
conjunctionwiththeexercisesthatfollow.
1.CPUinstructionmemoryview
Image2Instructionmemory
view
Thisviewcontainstheprogram
instructions. The instructions
are displayed as sequences of
lowlevel
instruction
mnemonics (assemblerlevel
format)andnotasbinarycode.
This is done for clarity and
makes code more readable by
humans.
Each instruction is associated
with two addresses: the
physicaladdress(PAdd)andthe
logical address (LAdd). This
view also displays the base
address (Base) against each
instruction. The sequence of
instructions belonging to the
same program will have the
samebaseaddress.
2.SpecialCPUregistersview ThisviewshowsthesetofCPUregisters,
Image3SpecialCPU
registersview
whichhavepredefinedspecialistfunctions:
PC:ProgramCountercontainstheaddress
ofthenextinstructiontobeexecuted.
IR:InstructionRegistercontainsthe
instructioncurrentlybeingexecuted.
SR:StatusRegistercontainsinformation
pertainingtotheresultofthelastexecuted
instruction.
SP:StackPointerregisterpointstothevalue
maintainedatthetopoftheprogramstack
(seebelow).
BR:BaseRegistercontainscurrentbase
address.
MAR:MemoryAddressRegistercontains
thememoryaddresscurrentlybeing
accessed.
Statusbits:OV:Overflow;Z:Zero;N:
Negative
3
3.CPUregistersview
Image4 CPURegistersview
The register set view shows the
contents of all the generalpurpose
registers, which are used to maintain
temporary values as the program's
instructionsareexecuted.Registersare
very fast memories that hold
temporary values while the CPU
executesinstructions.
Thisarchitecturesupportsfrom8to64
registers. These registers are often
used to hold values of a program's
variables as defined in highlevel
languages.
Not all architectures have this many
registers. Some have more (e.g. 128
register) and some others have less
(e.g. 8 registers). In all cases, these
registersservesimilarpurposes.
Thisviewdisplayseachregister'sname
(Reg),itscurrentvalue(Val)andsome
additional values, which are reserved
for program debugging. It can also be
used to reset the individual register
values manually which is often useful
for advanced debugging. To manually
changearegisterscontent,firstselect
the register then enter the new value
inthetextbox,RegValue,andclickon
the CHANGE button in the Registers
tab.
4.Programstackview
Image5Programstackview
Theprogramstackisanotherareawhich
maintains temporary values as the
instructions are executed. The stack is a
LIFO(lastinfirstout)datastructure.Itis
often used for efficient interrupt
handling and subroutine calls. Each
programhasitsownindividualstack.
TheCPUinstructionsPSH(push)andPOP
are used to store values on top of stack
and pop values from top of stack
respectively.
5.Programlistview
Image6 ProgramListView
Use the REMOVE PROGRAM button to
remove the selected program from the
list; use the REMOVE ALL PROGRAMS
buttontoremovealltheprogramsfrom
the list. Note that when a program is
removed, its instructions are also
removed from the Instruction Memory
Viewtoo.
6.Programcreation
Image7 Createprogramtab
Tocreateanewprogramenterits
nameintheProgramNamebox
anditsbaseaddressintheBase
AddressboxthenclickontheADD
button.Thenewprogramsname
willappearintheProgramListview
(seeImage6).
Image8Addprogram
instructionstab
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
BELOWbuttonstoinsertanew
instruction above or below the
selectedinstructionrespectively.
E. Lab Exercises - Investigate and Explore
Thelabexercisesareaseriesofactivities,whicharecarriedoutbythestudents
underbasicguidelines.So,howisthistutorialconducted?Thestudentsareexpected
tofollowtheinstructionsgiveninordertoidentifyandlocatetherequired
information,toactuponitandmakenotesoftheirobservations.Inordertobeable
todotheseactivitiesyoushouldconsulttheinformationinSectionDaboveandalso
frequentlyrefertotheAppendixforinformationonvariousCPUinstructionsyouwill
beaskedtocreateanduse.Remember,youneedtocarefullyreadandunderstand
theinstructionsbeforeyoucanattempteachactivity.
Now,letusstart.FirstyouneedtoplacesomeinstructionsintheInstruction
MemoryView(seeImage2),representingtheRAMintherealmachine,before
executinganyinstructions.Todothis,followthestepsbelow:
IntheProgramtab(seeImage7),firstenteraProgramName,andthenenteraBase
Address(thiscanbeanynumber,butforthisexerciseuse100).ClickontheADD
button.AnewprogramnamewillbeenteredintheProgramListview(seeImage6).
YoucanusetheSAVEbuttontosaveinstructionsinafile.Youcanalsousethe
LOADbuttontoloadinstructionsfromafile.
YouarenowreadytoenterinstructionsintotheCPUSimulator.Youdothisby
clickingontheADDNEWbuttonintheInstructionstab(seeImage8).Thiswill
displaytheInstructions:CPU0window.Youusethiswindowtoselectandenterthe
CPUinstructions.Appendixlistssomeoftheinstructionsthissimulatorusesandalso
givesexamplesoftheirusage.
Now,haveagoatthefollowingactivities(enteryouranswersinthetextboxes
provided).Awordofcaution:Regularlysaveyourcodeinafileincasethesimulator
crashesinwhichcaseyoucanrestartthesimulatorandreloadyourfile.
1. Createaninstruction,whichmovesnumber5toregisterR00.
2. Executetheaboveinstruction(todothissimplydoubleclickonitinthe
InstructionMemoryView).ObservetheresultintheCPURegistersview(Image
4).
3. Createaninstruction,whichmovesnumber8toregisterR01.
4. Executeit(Youdothisbydoubleclickingontheinstruction)
5. ObservethecontentsofR00andR01intheCPURegistersview(Image4).
6. Createaninstruction,whichaddsthecontentsofR00andR01.
7. Executeit.
8. Observewhichregistertheresultisputin.
9.
Createaninstruction,whichpushestheaboveresulttothetopof
thehardwarestack,andthenexecuteit.
10. Createaninstructiontopushnumber2ontopofthestackandexecuteit.
ObservethevalueinProgramStack(Image5).
11. ObservethevalueintheSPregister(SpecialCPURegistersviewImage3).
WheneveryoupushavalueonProgramStack,theSPregisterisupdated.
12. CreateaninstructiontocomparethevaluesinregistersR00andR01.
13. Executeit.
14. ObservethevalueintheSRregister(SpecialCPURegistersviewImage3).
15. ObservethestatusoftheOV/Z/Npartsofthestatusregister.Whichboxesare
checkedandwhicharenot?Whatdotheyindicate?
16. Createaninstructiontounconditionallyjumptothefirstinstruction.
17. Executeit.
18. ObservethevalueinthePCregister.Thisistheaddressofthenextinstructionto
beexecuted.Makeanoteofwhichinstructionitispointingto?
19. ObservethevaluesinthePAddandLAddcolumns.Whatdothesevalues
indicate?Aretheydifferent(Hint:CheckouttheBaseAddressvalue)?
20. WhatisthedifferencebetweentheLAddvalueofthefirstinstructionandthe
LAddvalueofthesecondinstruction?Whatdoesthisvalueindicate(Hint:Think
oftheinstructionlengthsinbytes)?
21. CreateaninstructiontopopthevalueontopoftheProgramStackintoregister
R02.
22. Executeit.
23. ObservethevalueintheSPregister.
24. CreateaninstructiontopopthevalueontopoftheProgramStackintoregister
R03.
25. Executeit.
26. ObservethevalueintheSPregister.
27. Executethelastinstructionagain.Whathappened?Explain.
28. Createacompareinstruction,whichcomparesvaluesinregistersR04andR05.
29. ManuallyinserttwoequalvaluesinregistersR04andR05(Image4).
30. Oneagainexecutethecompareinstructioninstep28above.
31. WhichofthestatusflagsOV/Z/Nisset(i.e.boxischecked)?Why?
32.
ManuallyinsertavalueinregisterR05greaterthanthatinregisterR04.
33.
Executethecompareinstructioninstep28above.
34.
WhichofthestatusflagsOV/Z/Nisset?Why?
35.
ManuallyinsertavalueinregisterR04greaterthanthatinregisterR05.
36.
Executethecompareinstructioninstep28above.
37.
WhichofthestatusflagsOV/Z/Nisset?Why?
38.
Createaninstruction,whichwilljumptothefirstinstructionifthevaluesin
registersR04andR05areequal.
39.
TesttheaboveinstructionbymanuallyputtingequalvaluesinregistersR04
andR05,thenfirstexecutingthecompareinstructionfollowedbyexecutingthe
jumpinstruction(Remember:Youexecuteaninstructionbydoubleclickingon
it).Diditwork?
40.
SavetheinstructionsintheInstructionMemoryViewinafilebyclickingon
theSAVEbutton(Image7).
***Endofexercises***
10
Appendix - Simulator Instruction Sub-set
Instruction
Description
Data transfer instructions
Move data to register; move register to register
MOV
e.g.
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 data to top of hardware stack (TOS); push register to TOS
PSH
e.g.
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
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 overflow.
Arithmetic instructions
Add 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 instruction address unconditionally
11
e.g.
JMP 100 unconditionally jumps to 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 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 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
CMP
Note:
If R01 = R03 then the status flag Z will be set, i.e. the Z box is
checked.
If R03 > R01 then non 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