Title | Programming Model 1 Tutorial Solutions |
---|---|
Author | RAHUL KUMAR R . |
Course | Computer Science |
Institution | University of Kerala |
Pages | 12 |
File Size | 642.8 KB |
File Type | |
Total Downloads | 92 |
Total Views | 150 |
Download Programming Model 1 Tutorial Solutions PDF
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 TheprogrammingmodelofcomputerarchitecturedefinesthoselowͲlevel architecturalcomponents,whichincludethefollowing
CPUinstructionset CPUregisters Differentwaysofaddressinginstructionsanddataininstructions
Italsodefinesinteractionbetweentheabovecomponents.ItisthislowͲlevel programmingmodelwhichmakesprogrammedcomputationspossible.
D.SimulatorDetails Thissectionincludessomebasicinformationonthesimulator,whichshouldenable thestudentstousethesimulator.Thetutor(s)willbeavailabletohelpanyone experiencingdifficultyinusingthesimulator.Thesimulatorforthislabisan applicationrunningonaPCrunningMSWindowsoperatingsystem. Themainwindowiscomposedofseveralviews,whichrepresentdifferentfunctional partsofthesimulatedprocessor.TheseareshowninImage1belowandare composedof
1
CPUInstructionmemory SpecialCPUregisters CPU(generalpurpose)registers Programstack Programcreationandrunningfeatures
CPUInstruction memoryview Addprogram instructionstab Createprogramtab
CPUregistersview
SpecialCPU registersview
Programlistview
Programstackview
Image1–CPUSimulatorwindow Thepartsofthesimulatorrelevanttothislabaredescribedbelow.Pleasereadthis informationcarefullyandtrytoidentifythedifferentpartsontheCPUSimulator windowBEFOREattemptingthefollowingexercises.Usethisinformationin conjunctionwiththeexercisesthatfollow.
2
1.CPUinstructionmemoryview Image2ͲInstructionmemory view Thisviewcontainstheprogram instructions. The instructions are displayed as sequences of lowͲlevel instruction mnemonics (assemblerͲlevel 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,
Image3ͲSpecialCPU registersview
whichhavepreͲdefinedspecialistfunctions: 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 generalͲpurpose 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 highͲlevel 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 changearegister’scontent, firstselect the register then enter the new value inthetextbox,RegValue, andclickon the CHANGE button in the Registers tab.
4
4.Programstackview Image5ͲProgramstackview Theprogramstack is anotherareawhich maintains temporary values as the instructions are executed. The stack is a LIFO(lastͲinͲfirstͲout)datastructure.It is often used for efficient interrupt handling and subͲroutine 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 buttontoremoveall theprogramsfrom 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.Thenewprogram’sname willappearintheProgramListview (seeImage6).
5
Image8–Addprogram 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 BELOW…buttonstoinsert anew 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). YoucanusetheSAVE…buttontosaveinstructionsinafile.Youcanalsousethe LOAD…buttontoloadinstructionsfromafile. YouarenowreadytoenterinstructionsintotheCPUSimulator.Youdothisby clickingontheADDNEW…buttonintheInstructionstab(seeImage8).Thiswill displaytheInstructions:CPU0window.Youusethiswindowtoselectandenterthe CPUinstructions.Appendixlistssomeoftheinstructionsthissimulatorusesandalso givesexamplesoftheirusage. Now,haveagoatthefollowingactivities(enteryouranswersinthetextboxes provided).Awordofcaution:Regularlysaveyourcodeinafileincasethesimulator crashesinwhichcaseyoucanrestartthesimulatorandreͲloadyourfile.
6
1. Createaninstruction,whichmovesnumber5toregisterR00. MOV #5, R00 2. Executetheaboveinstruction(todothissimplydoubleclickonitinthe InstructionMemoryView).ObservetheresultintheCPURegistersview(Image 4). 3. Createaninstruction,whichmovesnumber8toregisterR01. MOV #8, R01 4. Executeit(YoudothisbydoubleͲclickingontheinstruction) 5. ObservethecontentsofR00andR01intheCPURegistersview(Image4). 6. Createaninstruction,whichaddsthecontentsofR00andR01. ADD R00, R01 7. Executeit. 8. Observewhichregistertheresultisputin. R01 9.
Createaninstruction,whichpushestheaboveresulttothetopof thehardwarestack,andthenexecuteit. PSH R01
10. CreateaninstructiontopushnumberͲ2ontopofthestackandexecuteit. ObservethevalueinProgramStack(Image5). PSH #-2 11. ObservethevalueintheSPregister(SpecialCPURegistersview–Image3). WheneveryoupushavalueonProgramStack,theSPregisterisupdated. It increments, keeps pointing to the top of the stack. 12. CreateaninstructiontocomparethevaluesinregistersR00andR01.
7
CMP R00, R01 13. Executeit. 14. ObservethevalueintheSRregister(SpecialCPURegistersview–Image3). The negative status flag is not checked. 15. ObservethestatusoftheOV/Z/Npartsofthestatusregister.Whichboxesare checkedandwhicharenot?Whatdotheyindicate? None of the boxes is checked. OV - overflow, Z - Zero, N - Negative 16. Createaninstructiontounconditionallyjumptothefirstinstruction. JMP 0 17. Executeit. 18. ObservethevalueinthePCregister.Thisistheaddressofthenextinstructionto beexecuted.Makeanoteofwhichinstructionitispointingto? 0 19. ObservethevaluesinthePAddandLAddcolumns.Whatdothesevalues indicate?Aretheydifferent(Hint:CheckouttheBaseAddressvalue)? Physical address and the logical adress. Logical is from the perspective of the program. Physical from the perspective of the hardware. 20. WhatisthedifferencebetweentheLAddvalueofthefirstinstructionandthe LAddvalueofthesecondinstruction?Whatdoesthisvalueindicate(Hint:Think oftheinstructionlengthsinbytes)? It is the instruction length in bytes. 21. CreateaninstructiontopopthevalueontopoftheProgramStackintoregister R02. POP R02
8
22. Executeit. 23. ObservethevalueintheSPregister. It decrements, since the stack is smaller. 24. CreateaninstructiontopopthevalueontopoftheProgramStackintoregister R03. POP R03 25. Executeit. 26. ObservethevalueintheSPregister. It's back where we started. 27. Executethelastinstructionagain.Whathappened?Explain. Stack underflow error. The stack is empty, so it cannot pop any values from the stack. 28. Createacompareinstruction,whichcomparesvaluesinregistersR04andR05. CMP R04, R05 29. ManuallyinserttwoequalvaluesinregistersR04andR05(Image4). 30. Oneagainexecutethecompareinstructioninstep28above. 31. WhichofthestatusflagsOV/Z/Nisset(i.e.boxischecked)?Why? The Z status flag is checked. When subtracted the result is zero. 32.
ManuallyinsertavalueinregisterR05greaterthanthatinregisterR04.
33.
Executethecompareinstructioninstep28above.
34.
WhichofthestatusflagsOV/Z/Nisset?Why? Not one is checked. Because the result is not zero nor negative.
35.
ManuallyinsertavalueinregisterR04greaterthanthatinregisterR05.
36.
Executethecompareinstructioninstep28above.
37.
WhichofthestatusflagsOV/Z/Nisset?Why?
9
Now the N flag is set, because when R04 is subtracted from R05, the the result is negative. 38.
Createaninstruction,whichwilljumptothefirstinstructionifthevaluesin registersR04andR05areequal. JEQ 0
39.
TesttheaboveinstructionbymanuallyputtingequalvaluesinregistersR04 andR05,thenfirstexecutingthecompareinstructionfollowedbyexecutingthe jumpinstruction(Remember:YouexecuteaninstructionbydoubleͲclickingon it).Diditwork?
40.
SavetheinstructionsintheInstructionMemoryViewinafilebyclickingon theSAVE…button(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...