50
MIPS and Basic Assembly Language CS 64: Computer Organization and Design Logic Lecture #4 Ziad Matni Dept. of Computer Science, UCSB

MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI based) – What is an emulator? • We’re not actually running our commands on

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI based) – What is an emulator? • We’re not actually running our commands on

MIPSandBasicAssemblyLanguage

CS64:ComputerOrganizationandDesignLogicLecture#4

ZiadMatni

Dept.ofComputerScience,UCSB

Page 2: MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI based) – What is an emulator? • We’re not actually running our commands on

4/12/18 Matni,CS64,Sp18 2

ThisWeekon

“DidjaKnowDat?!”

Page 3: MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI based) – What is an emulator? • We’re not actually running our commands on

4/12/18 Matni,CS64,Sp18 3

WhydoCPUprogrammerscelebrateChristmasandHalloween

onthesameday?

BecauseOct-31=Dec-25

Page 4: MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI based) – What is an emulator? • We’re not actually running our commands on

LectureOutline

•  MIPScoreprocessingblocks

•  Basicprogramminginassembly

•  Arithmeticprograms

•  Controllogicprograms

4/12/18 Matni,CS64,Sp18 4

Page 5: MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI based) – What is an emulator? • We’re not actually running our commands on

AnyQuestionsFromLastLecture?

4/12/18 Matni,CS64,Sp18 5

Page 6: MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI based) – What is an emulator? • We’re not actually running our commands on

4/12/18 Matni,CS64,Sp18 6

MIPS

Page 7: MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI based) – What is an emulator? • We’re not actually running our commands on

WhyMIPS?•  MIPS:

–  areducedinstructionsetcomputer(RISC)architecturedevelopedbyMIPSTechnologies(1981)

•  Relevantintheembeddedsystemsdomain–  Computersystemswithadedicatedfunctionwithinalarger

mechanicalorelectricalsystem–  Example:lowpowerandlowcostprocessorsinMP3playersor

“Smart”Appliances

•  AllmoderncommercialprocessorssharethesamecoreconceptsasMIPS,justwithextrastuff...butmoreimportantly...

4/12/18 Matni,CS64,Sp18 7

Page 8: MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI based) – What is an emulator? • We’re not actually running our commands on

MIPSisSimpler…

…thanotherinstructionsetsforCPUsSoit’sagreatlearningtool

•  Dozensofinstructions(asopposedtohundreds)•  Lackofredundantinstructionsorspecialcases•  5stagepipelineversus24stages

4/12/18 Matni,CS64,Sp18 8

Page 9: MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI based) – What is an emulator? • We’re not actually running our commands on

Note:PipelininginCPUs

4/12/18 Matni,CS64,Sp18 9

•  PipeliningisafundamentaldesigninCPUs•  Allowsmultipleinstructionstogoonatonce– a.k.ainstruction-levelparallelism

Page 10: MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI based) – What is an emulator? • We’re not actually running our commands on

CodeonMIPS

Originalx=5;y=7;z=x+y;

4/12/18 Matni,CS64,Sp18 10

MIPSli$t0,5li$t1,7add$t3,$t0,$t1

Page 11: MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI based) – What is an emulator? • We’re not actually running our commands on

CodeonMIPS

Originalx=5;y=7;z=x+y;

4/12/18 Matni,CS64,Sp18 11

MIPSli$t0,5li$t1,7add$t3,$t0,$t1loadimmediate:putthegivenvalueintoaregister$t0:temporaryregister0

Page 12: MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI based) – What is an emulator? • We’re not actually running our commands on

CodeonMIPS

Originalx=5;y=7;z=x+y;

4/12/18 Matni,CS64,Sp18 12

MIPSli$t0,5li$t1,7add$t3,$t0,$t1loadimmediate:putthegivenvalueintoaregister$t1:temporaryregister1

Page 13: MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI based) – What is an emulator? • We’re not actually running our commands on

CodeonMIPS

Originalx=5;y=7;z=x+y;

4/12/18 Matni,CS64,Sp18 13

MIPSli$t0,5li$t1,7add$t3,$t0,$t1add:addtherightmostregisters,puttingtheresultinthefirstregister$t3:temporaryregister3

Page 14: MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI based) – What is an emulator? • We’re not actually running our commands on

AvailableRegistersinMIPS

•  32registersinall– Refertoyour

MIPSReferenceCard

•  Forthemoment,let’sonlyconsiderregisters$t0thru$t9

4/12/18 Matni,CS64,Sp18 14

Page 15: MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI based) – What is an emulator? • We’re not actually running our commands on

Assembly

•  ThecodethatyouseeisMIPSassembly

•  Assemblyis*almost*whatthemachinesees.Forthemostpart,itisadirecttranslationtobinaryfromhere(knownasmachinecode)

•  Anassemblertakesassemblycodeandchangesitintotheactual1’sand0’sformachinecode– AnalogoustoacompilerforHLcode

4/12/18 Matni,CS64,Sp18 15

li$t0,5li$t1,7add$t3,$t0,$t1

Page 16: MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI based) – What is an emulator? • We’re not actually running our commands on

MachineCode/Language

•  WhataCPUactuallyacceptsasinput•  Whatactuallygetsexecuted

•  Eachinstructionisrepresentedwith32bits

•  Therearethreedifferentinstructionformats:R,I,andJ–  Theseallowforinstructionstotakeondifferentroles–  R-Formatisusedwhenit’sallaboutregisters–  I-Formatisusedwhenyouinvolvenumbers–  J-Formatisusedwhenyoudocode“jumping”(i.e.branching)

4/12/18 Matni,CS64,Sp18 16

Page 17: MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI based) – What is an emulator? • We’re not actually running our commands on

4/12/18 Matni,CS64,Sp18 17

Sinceallinstructionsare32-bits,thentheyeachoccupy4Bytesofmemory.

MemoryisaddressedinBytes(moreonthislater).

Page 18: MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI based) – What is an emulator? • We’re not actually running our commands on

4/12/18 Matni,CS64,Sp18 18

Sinceallinstructionsare32-bits,thentheyeachoccupy4Bytesofmemory.

MemoryisaddressedinBytes(moreonthislater).

Page 19: MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI based) – What is an emulator? • We’re not actually running our commands on

4/12/18 Matni,CS64,Sp18 19

Sinceallinstructionsare32-bits,thentheyeachoccupy4Bytesofmemory.

MemoryisaddressedinBytes(moreonthislater).

Page 20: MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI based) – What is an emulator? • We’re not actually running our commands on

4/12/18 Matni,CS64,Sp18 20

Sinceallinstructionsare32-bits,thentheyeachoccupy4Bytesofmemory.

MemoryisaddressedinBytes(moreonthislater).

Page 21: MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI based) – What is an emulator? • We’re not actually running our commands on

4/12/18 Matni,CS64,Sp18 21

Sinceallinstructionsare32-bits,thentheyeachoccupy4Bytesofmemory.

MemoryisaddressedinBytes(moreonthislater).

Page 22: MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI based) – What is an emulator? • We’re not actually running our commands on

4/12/18 Matni,CS64,Sp18 22

Sinceallinstructionsare32-bits,thentheyeachoccupy4Bytesofmemory.

MemoryisaddressedinBytes(moreonthislater).

Page 23: MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI based) – What is an emulator? • We’re not actually running our commands on

4/12/18 Matni,CS64,Sp18 23

Sinceallinstructionsare32-bits,thentheyeachoccupy4Bytesofmemory.

MemoryisaddressedinBytes(moreonthislater).

Page 24: MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI based) – What is an emulator? • We’re not actually running our commands on

4/12/18 Matni,CS64,Sp18 24

Sinceallinstructionsare32-bits,thentheyeachoccupy4Bytesofmemory.

MemoryisaddressedinBytes(moreonthislater).

Page 25: MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI based) – What is an emulator? • We’re not actually running our commands on

4/12/18 Matni,CS64,Sp18 25

Sinceallinstructionsare32-bits,thentheyeachoccupy4Bytesofmemory.

MemoryisaddressedinBytes(moreonthislater).

Page 26: MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI based) – What is an emulator? • We’re not actually running our commands on

4/12/18 Matni,CS64,Sp18 26

Sinceallinstructionsare32-bits,thentheyeachoccupy4Bytesofmemory.

MemoryisaddressedinBytes(moreonthislater).

Page 27: MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI based) – What is an emulator? • We’re not actually running our commands on

4/12/18 Matni,CS64,Sp18 27

Sinceallinstructionsare32-bits,thentheyeachoccupy4Bytesofmemory.

MemoryisaddressedinBytes(moreonthislater).

Page 28: MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI based) – What is an emulator? • We’re not actually running our commands on

AddingMoreFunctionality

•  Whatabout:displayresults????Yes,that’skindaimportant…•  Whatwouldthisentail?–  EngagingwithInput/Outputpartofthecomputer–  i.e.talkingtodevices

•  Q:Whatusuallyhandlesthis?

•  Soweneedawaytotell theoperatingsystemtokickin

4/12/18 Matni,CS64,Sp18 28

A:theoperatingsystem

Page 29: MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI based) – What is an emulator? • We’re not actually running our commands on

TalkingtotheOS

•  WearegoingtoberunningonMIPSemulatorcalledSPIM–  Optionally,throughaprogramcalledQtSPIM(GUIbased)– Whatisanemulator?

•  We’renotactuallyrunningourcommandsonaMIPSprocessor!!!

…so,inotherwords…we’re“fakingit”

•  Ok,sohowmightweprintsomethingontostd.out?

4/12/18 Matni,CS64,Sp18 29

Page 30: MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI based) – What is an emulator? • We’re not actually running our commands on

SPIMRoutines

•  MIPSfeaturesasyscallinstruction,whichtriggersasoftwareinterrupt,orexception

•  Outsideofanemulator(i.e.intherealworld),theseinstructionspausetheprogramandtelltheOStogodosomethingwithI/O

•  Insidetheemulator,ittellstheemulatortogodosomethingwithI/O

4/12/18 Matni,CS64,Sp18 30

Page 31: MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI based) – What is an emulator? • We’re not actually running our commands on

syscall

•  SowehavetheOS/emulator’sattention,buthowdoesitknowwhatwewant?

•  TheOS/emulatorhasaccesstotheregisters

•  Weputspecialvalues(codes)intheregisterstoindicatewhatwewant–  Thesearecodesthatcan’tbeusedforanythingelse,so

they’reunderstoodtobejustforsyscall

4/12/18 Matni,CS64,Sp18 31

Page 32: MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI based) – What is an emulator? • We’re not actually running our commands on

ProgramFilesforMIPSAssembly

•  Thefileshavetobetext

•  Typicalfileextensiontypeis.asm

•  Toleavecomments, use#atthestartoftheline

4/12/18 Matni,CS64,Sp18 32

Page 33: MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI based) – What is an emulator? • We’re not actually running our commands on

(Finally)PrintinganInteger•  ForSPIM,ifregister$v0contains1andthenweissueasyscall,

thenSPIMwillprintwhateverintegerisstoredinregister$a0ßthisisaspecificruleusingaspecificcode–  Note:$v0isusedforotherstuffaswell–moreonthatlater…–  When$v0=1,syscallisexpectinganinteger!

•  Othervaluesin$v0indicateothertypestosyscallExamples:–  $v0=3meansdouble(orthememaddressofone)in$a0–  $v0=4meansstring(orthememaddressofone)in$a0–  We’llexploresomeoftheselater,butcheckMIPSrefcardforallofthem

4/12/18 Matni,CS64,Sp18 33

Page 34: MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI based) – What is an emulator? • We’re not actually running our commands on

(Finally)PrintinganInteger•  Remember,theusualsyntaxtoloadimmediateavalueintoa

registeris: li<register>,<value>–  Example: li$v0,1 #PUTSTHENUMBER1INTOREG.$v0

•  Tomakesurethattheregister$a0hasthevalueofwhatyouwanttoprintout(let’ssayit’sinanotherregister),usethemovecommand:

move<toregister>,<fromregister>–  Example: move$a0,$t0#PUTSTHEVALUEINREG.$t0INTOREG.$a0

4/12/18 Matni,CS64,Sp18 34

Page 35: MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI based) – What is an emulator? • We’re not actually running our commands on

4/12/18 Matni,CS64,Sp18 35

Ok…SoAboutThoseRegistersMIPShas32registers,each32bits

Page 36: MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI based) – What is an emulator? • We’re not actually running our commands on

AugmentingwithPrinting#Mainprogramli$t0,5li$t1,7add$t3,$t0,$t1#Printanintegertostd.outputli$v0,1move$a0,$t3syscall

4/12/18 Matni,CS64,Sp18 36

Page 37: MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI based) – What is an emulator? • We’re not actually running our commands on

We’reNotQuiteDoneYet!ExitinganAssemblyPrograminSPIM•  IfyouareusingSPIM,thenyouneedtosaywhen

youaredoneaswell–  MostHLLprogramsdothisforyouautomatically

•  Howisthisdone?–  Issueasyscallwithaspecialvaluein$v0=10(decimal)

4/12/18 Matni,CS64,Sp18 37

Page 38: MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI based) – What is an emulator? • We’re not actually running our commands on

AugmentingwithExiting#Mainprogramli$t0,5li$t1,7add$t3,$t0,$t1#Printtostd.outputli$v0,1move$a0,$t3syscall#Endprogramli$v0,10syscall

4/12/18 Matni,CS64,Sp18 38

Page 39: MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI based) – What is an emulator? • We’re not actually running our commands on

4/12/18 Matni,CS64,Sp18 39

MIPSSystemServicesExamplesWe’veSeenSoFar…

stdout

stdin

FileI/O

Page 40: MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI based) – What is an emulator? • We’re not actually running our commands on

NowLet’sMakeitaFullProgram(almost)

•  Weneedtotelltheassembler(anditssimulator)whichbitsshouldbeplacedwhereinmemory–  Bits?

(remember:everythingendsupbeingabunchof1’sand0’s!)

4/12/18 Matni,CS64,Sp18 40

AllocatedasprogramRUN

AllocatedatprogramLOAD

Constantstobeusedintheprogram(likestrings)

mutableglobalvariables

thetextoftheprogram

Page 41: MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI based) – What is an emulator? • We’re not actually running our commands on

MarkingtheCode

•  Forthesimulator,you’llneeda.textdirectivetospecifycode

4/12/18 Matni,CS64,Sp18 41

AllocatedasprogramRUN

AllocatedatprogramLOAD

Constantstobeusedintheprogram(likestrings)

mutableglobalvariables

thetextoftheprogram

.text#Mainprogramli$t0,5li$t1,7add$t3,$t0,$t1#Printtostandardoutputli$v0,1move$a0,$t3syscall#Endprogramli$v0,10syscall

Page 42: MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI based) – What is an emulator? • We’re not actually running our commands on

Themoveinstruction

•  ThemoveinstructiondoesnotactuallyshowupinSPIM

•  Itisapseudo-instructionforustouse,butit’sactuallytranslatedintoanactualinstruction

ORIGINAL: move$a0,$t3ACTUAL: addu$a0,$zero,$t3 #what’saddu?what’s$zero?

4/12/18 Matni,CS64,Sp18 42

Page 43: MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI based) – What is an emulator? • We’re not actually running our commands on

WhyPseudocodes?Andwhat’sthis$zero??

•  $zero–  Specifiedlikeanormalregister,

butdoesnotbehavelikeanormalregister–  Writesto$zeroarenotsaved–  Readsfrom$zeroalwaysreturn0value

•  Whyhavemoveasapseudo-instructioninsteadofasanactualinstruction?–  It’sonelessinstructiontoworryabout–  OnedesigngoalofRISCistocutoutredundancy–  moveisn’ttheonlyone!liisanotheronetoo!

4/12/18 Matni,CS64,Sp18 43

Page 44: MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI based) – What is an emulator? • We’re not actually running our commands on

4/12/18 Matni,CS64,Sp18 44

ListofallCoreInstructionsinMIPS

Page 45: MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI based) – What is an emulator? • We’re not actually running our commands on

4/12/18 Matni,CS64,Sp18 45

ListoftheArithmeticCoreInstructionsinMIPS

Page 46: MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI based) – What is an emulator? • We’re not actually running our commands on

4/12/18 Matni,CS64,Sp18 46

ListofallPsuedoInstructionsinMIPSThatYouAreAllowedtoUseinCS64!!!

ALLOFTHISANDMOREISONYOURHANDY“MIPSREFERENCECARD”NOWFOUNDONTHECLASSWEBSITE

Page 47: MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI based) – What is an emulator? • We’re not actually running our commands on

MIPSPeculiarity:NORusedaNOT

•  HowtomakeaNOTfunctionusingNORinstead•  Recall:NOR=NOTOR•  Truth-Table:

•  So,intheabsenceofaNOTfunction, useaNORwitha0asoneoftheinputs!

4/12/18 Matni,CS64,Sp18 47

A B ANORB0 00 11 01 1

1

000

Notethat:0NORxx=NOTxx

Page 48: MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI based) – What is an emulator? • We’re not actually running our commands on

Let’sRunThisProgramAlready!UsingSPIM

•  We’llcallitsimpleadd.asm•  RunitonCSILas:$spim–fsimpleadd.asm

•  We’llalsorunotherarithmeticprogramsandexplainthemaswegoalong– TAKENOTES!

4/12/18 Matni,CS64,Sp18 48

Page 49: MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI based) – What is an emulator? • We’re not actually running our commands on

YOURTO-DOs

•  ReviewALLthedemocode– Availableviatheclasswebsite

•  Assignment#1– DueFriday

•  Assignment/Lab#2–  LabonMONDAY–  Submititviaturnin

4/12/18 Matni,CS64,Sp18 49

Page 50: MIPS and Basic Assembly Language - GitHub Pages– Optionally, through a program called QtSPIM (GUI based) – What is an emulator? • We’re not actually running our commands on

4/12/18 Matni,CS64,Sp18 50