introduction to risc assembly language programming-john waldron--191.pdf

Upload: kapetanyiangoulas

Post on 07-Jul-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    1/191

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    2/191

    ndi RSCAby Ln

    Pin

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    3/191

    ntroductin t RISCAssembly Lagage

    PgmmgJOHN WALDRO

    School of Computer Applications

    Dubi ity University

    Harlow, England • London • New Yok • Boston • San Fancsco • Toonto • Syney• Singapoe • Hong Kong

     Too • Sou•Tae • ew Deh• Cae Town • Mad • Mexco •Amsedam • Mnch• Pas• Man

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    4/191

    Peaon Eucaon Ltedinburgh Gaealowsse CM20 2Jnland

    an Associae Comanies hroughou he word

    V W W W h://www.earsoneduc.com

    © Addison Wesley Longman Limied 999

    The righ of John Waldron o be idenied a auhor of his Work has been sseredby him in accordance wih he Coyrigh , Designs and Paens Ac 988

    A ighs reserve. o ar of his ubcai on may be rerouced, sored in a re rievalsysem, or ransmied in any rm or by any means, eecronic, mechanical,

    oocon recorin or oherwse whou eher he rior wren erission o heublisher or licence ermiing resriced coying in he Unied Kingdom issed byhe Coyrgh Licensing Agency L, 0 oenham Cor oad, London W LP

    The rogrms in his book have been ncuded r heir insruciona vaue. They haveeen ese wi care are no guaranee r any acur does no oer any warranies or reresenaions nor does i acce any iabiiie wihresec o he rorams.

    Many of he designaions used by manucurers and sellers o singish er

    roduc are claime as raemak. Aon Wesle Longman Lme s maeeer em o sl rdemar nrmaion abou manucurers and herroduc menioned in hi s book A i s of he raemark designa ions and herowners ers o e .

    Cove deigned by OdB esign Communicaion, Readng, UK 0/ 12 es y 2Prne n o re Br y enry ng ieda he Dorse Press, orcheser, DT D

    Firs rined 998

    ISB 0-20-9828-

    ii ia aaiiiai aaA clogue recor r his book is avaiabe om he Briish Library

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    5/191

    This boo is based o a oe-semeste itodto omte ahiteteorse r rstear omtig studets i the hool o ComteAppiations, Dubli Cit Uivesit, usig PIM, a virtual mahie tats gams the M R000/R000 omutes The ahitetue ofthe MIP is a ieal examle of a sime, lea RIC (Reue Istutoet Comte) mahie, whih maes it eas t lea a udesta heproessr ontais 2 geeral-rpose egisters a a well-esige

    isttio set The exisene of a simlato r he oesso geatlsimlies the develomet ad ebuggig assembl laguage ogamsFo these reasons, MPS is the reed hoie teahig omuteaitetue i te 0s, jst as te Motooa 680 was ug e 80s

    The mateial assmes that te eade has eve stdied omteprgaming ee, n s usua given at te same tme as a pgramigouse in a high-level laguage ie Java o C The mai daa strutesovee are stigs, as a staks The ias of ogram loops, semes, oeu n a ptd. Th philph

    e e oo s t up t ang rav arhitetue books b eablig the eade to stat writig simple assembllaguag ogms eal, without gttig ivolve i labious itioo tad-o ioed h di of th oo mot ulaa to omte ahitete i to bgi by witig umeos smallassmbl languag pgam bee ging n stu the underlingnpt s tis txt es t are tpi sh as lg esgn boolea algeba but es ntain exaple pgrams sing the MIPS logialistios While oessos lke te MIPS wee esige g-v

    laguage ompilation ad as suh are targete at mpiles ae hanhma omm, th ol wa to ai a aeiatio of theitionlit s to wite ma pgrams te pess n assemangag

    Te boo is asoiate wit an autoati gam tstig sstem (MisAssemb Language xam Sstem) wi aows a etue to set asseml mr te assinmetsaaia a t tst a MIP asmb lagage ogam agast

    sevea dee ase ad detemie hte it wo, deibed inppenx A e exam sstem is witte as a etion o nix C shellsipts the instrutor o sent does t wish to adot his leanngpp th ttbk an e se n a tatna manne sen w

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    6/191

    RC

    n write n ssembly lnguge rogrm whih onverts number to nASCII string in hexdeiml rmt nder exm onditions hs demonstrted thorough understnding o ll the riniles o introdtory omuterrhiteture There is little oint in desribing onets suh s ielning,

    delyed brnhes o dvned omiler tois to students who re not yetmilir with simle ogrm loos.

    Assembl lnguge rogrmming is sully onsidered n rne ndmpe dpe. e a ong tho who t xin ossembly lnguge rogrmming ws the instrutions nd registers orhtetures like the Intel 808 mily Progrmming in RISC rhitetureis very dierent due to the elegnt, omt nd simle instruton settudents o this text who hve never rogrmmed bere nd begin to study

    it simultneousl with ourse on C rogrmmng reor it is esier ndmore logil to rogrm in ssembly! In ddition, beuse of therogrmming exm system, thee is higher ss rte nd level o roienyhieved by students on the sembly ourse thn on the more trditionl Course

    The SPIM simultor is vilble in the ubli domin om theUnversity o Wisonsin Mdison t t : t . s . wis . edu/u/sm/ vehed rojetor slides o leture notes, ll exmle rogrms n em stons r vibl o htt / ww . o du e/wdo e rogms tht orret the uetions togte w ttses nd solutions, re vilbe to eturers dong the ourse

    e P smuor sowre ws en n w L (u@wu) ook ws t nse on Conrsourse t the Unversy o regon wh e s me ve on enteret oud ie to thn im r rmission to use some o his exmeogm n mtel Tnks to Dr Dvid Snir r reng n erlyda d prd may potnt ggton. Alo hnks o rnutern n Ket nse t on Wese Logn

    ohn Wldon, Dublinuly 8

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    7/191

    u

    I . Bs pu go I1 . 2 Mcn lngug 31 .3 Assembly langage 31 .4 Wy pog in assby language 41 .5 Otline of chates 51 . Sum 7

    xecises 7

    2 Essnial bacound inmaion 9

    2 . 1 ntodtion 2.2 eim n bina nmbes . eadecimal nmbes 1 0 in iin 1 p u 2. Bs byes n nbbles 4. s 42.8 Suy 1

    eises

    S comput oganzaton 1 7

    oucon 1 7 . T I iss 1 3.5 T SIM simuo 2 1

    3. 0 ognion 3.7 Sumy 3

    Exciss

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    8/191

    COT

    4 An exampe MPS program 2

     4. ntrodction 24. 2 Sorce code rmat 2 4. 3 escrition of 29 4.4 Pting theory nto ractice  4. oad and stoe insttons 3 7 4.6 Arithmetic instrctions 384.7 Mtpication and division 404.8 Programming examle 424.9 Smmary 48

    Exerises 48

    5 Contro fow structres 9

    5. 1 ntrodction 495.2 Contol strutues 495.3  Conditonal banches 05. Exampe prgrams sing loos 5 15.5 Smmary 58

    xercises

    6 Addressing modes 59 

    . Introduction 6. MPS instruction rmats 596.3 MIPS register addressng 606 .4 base addressing 265 ssg 636.6 MPS PCrelative addressing 6467 Ep pg usg s ssg 646.8 Exame roram sin indexed addrssin s s ssg s ssg

    Ess 72

    7 gical, si ad tate instuctions 73 

    . ntoton 73  h nd otat nsttions

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    9/191

    CTT

    7. An xaml oga 76 ummry 2

    xriss 3

    k p

    . 1 nroucon . stac . rcdur clls 9 1.4 assing parms 92.5 mporry d sv rgisrs 93

    .6 ac rams 93 8.7 Assmbly cod fo a comi 100. Exml rcursiv roms .9 ummr 1 1

    Appendix A MIPS oamn xas 1 2 1

    A I Ioduco 1 2 1 sofar 1 2 2A3 xam qusos s oa oos xam ston sn t anatn 1 3 1 m qtin uing th tack 1 3 2A6 Exm usios using ncions

    ppn x MIPS/SPIM u ik

    Appdix C IP/SPI iscon nc 1

    In

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    10/191

    Java is a ademak o egisered rademark of Su icrosysems IcJurassic Park is a tademar o egieed ademak of Ambln Enrann. acin osh is a egis eed ademak of le omue Inc.IPS is a rademk o r egisered rademark of IPS echol ois Ic rol 000 dm o Lma o Mooo, InN    e  o  64    r  em r  or re gis ered  tradeark  o f N n ndo of Amc,  In.SI is  a regstered trademark of Slicon Graph i s,  I nc. SP is rademark of SPAR C Inernai oal Inc a raema or  resered rademar o ua Fo y Soy is a radema isne s a radema i nse  hrouh X/ON Company

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    11/191

    Introduction

    At ribing bai omputr organiation thi hapt introuambl languag xplain what it i an what it i u r h the reader hould tudy aembly lgg r iscuss. Fin o he emann chaes n he book s ven

    B

    he aes an os ha coecvey consue he compue are but soha he can ony assume one o wo vaues o saes cae on an o aceement of he compue an thrr rprnt onl th valu ro o onEach one o zo s ce a bnary , o b he nerae cc n aypca compuer can be oranze no hree caeoes he poo thmmor an thos onnting to arou nput output (/) c sc ik o boar a hown in Fgur 1 1 . Th bu onnt th ntgratdiuit togthr

    Te oesso s n need h h bi ntional builinboc o he compte t ows he echeecue cyce repeatey rainsmpe nsrctons, sc as to a two numes or mov a nubr om thmmory an xutin thm a hown in Figu 1 . 2 . h roor onito

    perfrm arhme operaon

    coro wc es he memoy /0 evces an aa ath wa o oaor wh o nrco o rorm

    mo (srs) o e em ltan ceran conro nrmaton

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    12/191

    C

    Bu connecd een _ineged

    r o c e o r

    eory

    interface

    F tratd crcits i a cotr

    ead an intructon

    fro memory

    R e o e ( s )

    u eceece cycle

    rite reult

    l

    etere next

    nruction

    Ectric connct to th procssor chip is th mmor Mmor c o rous ss, us mure mulple of meabe or miion oyts, whr yt s group o gt ts Ao coct to t poco 0 vc tt ll t c t cunct t t t

    or thro gh scrns ors n othr inrmton storg ics s ch

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    13/191

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    14/191

    4 T

    An ssembler cn lso hide  mny mey  machine deta fom

    pogmms. Fo mp, t ssmr n giv t son tt tr armny more insrucions in he  processor  tan  tee  eally  ae,  by povdng

    pseudo-instructions  whch consst of seveal machine insrucions, removng

    the  one-to-one  correspondence between assembly language and  macinengg instrtions.

    4 HY PRRAM N ASSE M BLY

    LANUAE?

    T most ommon rson to progrm in ssm ngg is tt it is tst w to gin n nrstnding o ow th prosso n omptr worst t owr vs Aprt om tis it is gnr ttr not to progrm inssm ngg i o n do t sm jo in ig ngg i Cn s a ompir to trn ig ngg sttnt into sqns ofmhin insttions, s sow in Fig 1 3 Assm ngg s towing isdntgs:

    m n i not tdio mhin n t it i ti

    opon n sow t sor o pogrms is t o motims s ong s orrsponding progrms n high ngg s s, n pin shows h pop n wri progrms onsnnmr of ins pr d no mttr wht t n, so it wi tk thrm on o t ab na on o t probabto introing g is proportion to th ngh of t progrm.

    Assm n is minpnnt, so tt prorm wrttn P wostton n w to ompt wtn E, SG o BM woton. Am ngg pogrm r notpota.

    omle e LL

    anguage progra

    f e nd poduce -embl

    emble un

    ue rorm

    embly lnguge mhi lgug

    anguage rogra

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    15/191

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    16/191

    6 C

    hate 3 does ot desibe eey detai of the MIPS oesso, buties eouh imatio about memoy ad itea MIPS eistes toaow sime assemby auae oams to be witte h SIMsimulator ntroduced. eepe esa o e conce nouci this hate wi be deeoed as ate hates ead o them It iseessay to hae a idea of the ahitetue of th MIPS osso if one s

    e egi y ouig he y ued i MIP embyauae oam It the osides a sime eam oam. heistutios used i tis oam ae itodued he XSPIM oammioo s en dsced ad suos ecu e amoam us XSPM ae ie Additioa sime oad, stoe ad

    aithmeti istutios ae itodued, toethe with some eame oamsiustati thei use.hae 5 oos at a a tat uses a oam oo to

    wo out the eth of a haate sti Famiiaity with a w assembyauae istutios suh as basi oad, stoe ad sime aithmetioeatios, is eeded, togete wit te oet of ogm oos. oam oo aows a oeato to be eeated a umbe of tmes, wthouthai to·e the assemb auae stutos eiit Fo ee, tosum u 50 umes, oe woud ot hae 50 add stutos the oam

    ut nsta wou ha the a nstucton onc an o oun a oop 50times

    Fo an en opeaton, uch a oa, a o anch th onma dfn was o sef the ddss of the ond(s) he dieewas of detemii the addess ae aed addessi modes. hate 6oos a e diee addessig modes o e MI oesso ad sowsow a isutios a t ito a sige u-yte wod ome sameogms ndd o so ddon ddssng mods n on

    hat 7 st oos at shift ad otate istutios t the osidesoga istutios, sowi a eame oam how tese stutosa b usd to ot a dima umbe to a AII sti i adeimamat oia shift ad otate istutos ae a used to mauate theiddua bits of a wod

    ate 8 st itodues te sta data stute, ad te iustates itsusae wit a oa to eese a sti usi a sta. e teiques tosuot poede as i MIPS assemby auae ae te studied.Poceues aow oams to b oe ito sae, moe manaeae

    uis he dmet to the deeomet of oms one thn w z saemes ma mes b ei o tem b me ate ta eeatithe ode. I additio, oedues mae ae oams easie to ead adudestad ta ames eeded to impemet oedue as aescusse wo ecuse oams ae en a ccuate iboas sisad soe the owes of Haoi obem ad eame ode om a ea

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    17/191

    RCI

    endix desibes he IS ommin exm sysem endix B a S S non eene soed by nsuion yeAenx C s moe omlee nsuion eence in ahabe ode

    U

    eement o the omute an eesent onl the vlue eo o one eoceo llow te etcecte cyce eeately eing imleintuction uch to two nume o move nume om tememo nd eecuting tem. All intuction tht the oceo execte aeencoded tig o t clle maine lnguage nd stoed in he

    memoy An asemle i a oam tt ae symol witen y theogmme n semle the nl machine lngge ogm to eexecuted y he oesso. The soue oe o semly lnuage oams ishee o moe ime s lon as oesondin hih-level lane oamse eml lnue ntuctio et dene the intece between thewe nd te otwae nd undelie l the ntioning o comute oh hooh eiion o his oi is essenia ny sdent ocompute cience o eectonic engieeing

    EXEIE

    11 h is eise1 ht does oesso do?1 3 ht o ineed is onsis o ?1 esbe he a neae s n a ome1 5 Desibe he elionshi between mhine lnuge nd aemly

    lnguge16 t e te dvntge o ogmming in eml lngue ove

    mhine lnue hen shold ssemly lne e e

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    18/191

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    19/191

    Esseal backgroud

    nformon

    . 1 NTRUTN

    Thi hp i m il imi dd tu m u pomm Hxcm cm

    um p Th w i which itio d utcto cid ut th ub tth wt th ntt atum i icu Alo cod th ASCII chct co u to toc comput mmo utg o t cocp t pogmmg comput gug cu gtcompt ultmt cot o g um o oo wtc

    . EM BNY UMBES

    dnt tm h ud to nt um thuhuthito h Bloi hd mthod o couti d o th um t ct o t c tll mumt o tm glu pt tm o cou o th um o g o thum h c th cm um tm o

    th c um tm ch digt' poitio pt ditow o Fo xml th um 9 quilt to

    1 10 + 10 + 9 10° igit compu u 2, kow th i tm

    umicl qutiti th th b 10 Bny u bd

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    20/191

    1 0 EE BCRO ORO

    3 91 00 00 3 9

    u 2

    2 ) 3 92 ) 9

    2 ) 9 C on e decml2 )  numbe o

    2 2 0 bny

    0

    C on e

    bnynu mbe o

    dec m 0

    Coveso etwee y ecm mes

    2 21 2

    2

    42

    2 O

    3 9

    pow o 2 t t o pow o 1 T um 11 t numb sysm is quivaln o

    x + 1 x + x 2 + 1 2°T mto o covtg um to cm tgtw is sown in Figu . 1 . I involvs adding up pows of wo vywt copog poto cot o

    onving a dimal numb o binay is no qui as simpl T wayo m u w

    mind If maind is on, a binay on is gnad If i is zo, abiy zo s poduc is viio two pt ut zo uott ot utt Fgu 1 T poc t t o tw n v o

    Cog tw cm cu um tout numbs dmal, bu numbs w b sod as a squn of bis int comput

    . HEXDEIL UE

    Hadimal numb, o so, us ba 1 o psn nmialutt c gt c ta on 6 valus w m tt x xtmo o top o t to 9 u m A ow Figu ls A oug F usd to snt additional valus to ow-c toug o somims usd wit sam

    maning. T ook ow th covto o putng a numb oii i i i hi h h and dimal numb systms a also sown in Figu tcu cog t m tho uttd Fgu 2 1 t a tm

    T vtg o um tt oc t um gt gt boms vy dious o wi ou a long sing of ons and zos

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    21/191

    HCI BR 1 1

    10B - 11

    12

    D -E

    F -

    13

    14

    15

    484  O1E46 484 x me hex16 4

    O1E4 484 16 E 4

    0 1

    IC onve

    dec mnumbe o ex

    u 22 The hexecm me system

    4 1

    16° 4

     

    16 56

    484

    IC onve

    hexnumbe odem

    a occupi t um w to i t comput a wic it at o cla ( o o o) Ti woul ot ovou i t um wai a 1. v lag um a ot to wt out t t i till

    poil to ipcto ow ma it a occupi t um awic it a t o cla ic popt o acmal um tathy n b nv bn b npn hn n ne

    2 t i a impl latioip tw um a a i a 16 uia git goup togt psnt on aciml digi

    0 0000 10001 0001 9 10012 0010 A - 10103 0011 B 10114 0100 100 0101 D - 1101 0 01 F - 11

    0001 110 000 C ove o

    _b E 4 hex

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    22/191

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    23/191

    u 2

    0 0

    1 0

    1 1

    1 1

    46

    3

    O' COP BR 1 3

    0

    0 1 1

    0  y 11 1  y 1

    1 0 1 1 1 0 Add   1 1 1

    1 1 0 1 0 1mbe

    Addiion of nmbes in bse 2 .

    91

    0

    0010011111011001

    11

    10111 110110+1

    C e 1 _ m mbe O b y1

    I11011001

    C hnge heg y gg e b

    u 2 ighbi wo ompleme epeenaio of nmbe.

    110 4

    rN eg e

    m e y beg n

    h

    1000

    4 0100

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    24/191

    1 4 I CRO IORIO

    - ??? hnge he

    _ ign o  

    subtrahan by ogg e add

    + diny ule

    l_ _

    o addiopp y o' comp emen

    u 2 ubra f w's pl ubrs

    26 TS, TES ND NILES

    A mtod o, th gt d pop tht collctl cottut thcompur r buil o ht th c ol um o o o lu or Bt compur r groupd togthr o tht th trl rproo umr r rricd o cr iz rl ll compur r orgizdroud group o ght t, clld t or omtim octt Four b

    groupd togthr r cld ord. Cogl o om oldr computr,to t cd word. A u t h t d c ecbe one he hee ae n een ombnaons o t Fompl thr r 28 2 comto tht c d o t tp gdd po or gd umb h h umb rom O to 28 - 1 2

    th t hodg gd umr two complmt rmt thumr c rg om -2n to +2nl

    For mpl, i Figur 2,

    4 d th 1 umr rg om 8 to +7 .2 7 S RN HRRS

    ordr o rpr chrctr rmto i h computr mmor, hcrctr mut cortd to umrc lu To tdrd cod rd h

    ASC mrc Stdrd Cod r rmto trchg EBD Eene Bnaoe Decm nechane oe

    All microcompur u ASCI cod (Figur d EBCIC tpic ud BM mim.

    ppr d lor-c lphic chrcr, h digi hrough 9 dth commo pctutio mr r uci r m purpo ou h d d h t i ll Th S d h l t 27

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    25/191

    OR CHRCR 1

    L e m

    Righm bi

    bi \\

    L LE SP @ O DCl A Q q T D2 2 B R b O DC4 $ 4 D d t EQ NAK % 5 E AK SY 6 F v f v EL ETB G w B E 9 I  y LF B * j z VT E + K [ k FF F \ R GS m } SI ? EL '' 1 ASII O3E

    IFnd ASCII cdeof chce

    u 2 AC he odes

    to 3 1 e eseve se ot oes ese le CRc tu) LF l ) ootl t) t SC cos te e to 3 1 e se vos oses s s tommuto otocols

    e Uoe St s e tetol stn se to eoett comput pocss ts s s s o t smplcty osstny o SC t gos yo SC's lmt ilty toco oly t Lt lpt T Unco St povs t

    ty to no ll o t cts us t mjo ttlnu o t l To ommot t m toun ochrcers se nernonl e he Unoe Snr ses -tco st tt povs cos mo t 6 000 ccts To pcct cog sml ect t Uco St ssgs ccct uu 6t vu Mtmtcs tccs o

    l l t t l l d t t l t

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    26/191

    1 6 CRO ORO

    28 SUMMR

    I te eml me sstem e 's os eeses ee

    oe of l 0, ees mes e se o oes of ese of mes s tt oe te me gets lge teomes e teos to e ot lo t of oes eosHexecml or hex r shor ses bse o reresen nmerc qnesese t s es to st etee e. To mes se f te sm of to gts eqls o eees te me se y sgeete. e le of te s 1 ll moe omptes se te tosomplemet epesetto egte numes I oe to epesette mto te omptes memoy te tes eoete to me lues s te SCII oe.

    XRSES

    1 o 300 soe y? Is s s n 00 s so n n3 o my ts e eqe to stoe 300 y?

    Wt s 300 e? Wte o te egtt to's omplemet ny epesetto of

    Wt on th t tos omplmnt epesntton o 3

    eeml otton7 So te steps ole e ompte stts 3 om h i h SII h i

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    27/191

    CHA

    MIPS  compu t er  

    ornzon

    3. 1 I NTRDUTIN

    s ct os not sc tl o the MPS poesso, ut gsnouh inmton aout mmo an intnal MIPS eses owsmle ssml lngug ogms to wttn XSPIM smulto sntouc A p unstning o te cocets itoe i tispt ll evelope as late haptes epan on hem I s neessay to n o t ctctu o he MP pocsso one s to ogm tn ssml lngug

    3 TE M I PS DES I N

    te mi-l 90s me o stes soe tt le toetill eolcn wt gl comle gevel ngge pogms most o co tte tll te consists o smle ssmets sttemnts npocu cs wt lmte nme o metes togt 5 p cnt)is is so n Figue 3

    te el 18s new ten n t esgn o ocssos gn i t c t mut mi T nl sen u te commonst smpl nstcons o coul ao to p alt i t usl cse m l net gin in mnc contst omp nstucton t ompt) cps cn cut mcomlcte nstuctions t t ns o slowng ow te smlest oes

    In 9 gou t l le Dv Pttson n Clo Squngn sgnng RISC cs T con t tm ISC n nme tei

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    28/191

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    29/191

    Aeeof o

    OxOOOOOOOO

    OOOOOOOO

    O x 0 0 0 0 0 0 0

    H RR

    OOO

    OO

    OxO

    v

    E ch ce d e

    ye

    OC O90

    OFFFFFFFD O6

    OFFFFFFE Ol

    OxFFFFFFFF OOO

    u 32 Mery rgaza

    Ox7FFFFF F

    Sck see

    D h

    c opee o "

    OxlOOOOO0

    Ox00400000

    "

    u33 M ey l

    D see

    ex see

    eseve

    v

    Itruto

    t th botom o h us adde ae x00000 s h x swhch os nsu a h x m is h m t OOOOOOOO Th stak is a las i s o quwhich is n to mplmnt pocus, llowng pogmms to stctsotw to it as to unstn n u ( Chpt ) Thm tc ss at th to o th ss spc (Ox7. t gwown, tows th at sgmnt

    Th pocsso oy oni a i, h of hih hs m ont gst s t ogm count (PC) which

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    30/191

    20 M COMR ORIO

    T MIPS (an SPM) pocsso contains 32 gna pupos giststhat are nbered g ga aways contains t awi au 0. MIPS as stais a st oconnons as o ow gss sou us s susons

    gudns wc a not c y t awa. How a ogam tatoas m w o wo opy w ot softwa. Ta 3. ists tcommony us gists an scis ti intn us. T MIPSg PM ga a Figu 4.

    T conntion t us o t gists wi com ca wn stuy assmy anguag suppot pocu cas in Capt Rgists $ $ (2) an $ (27) a y tassm an opating systm. Rgists $$3 (47) a us to passt st agumnts to pocus (maining agumnts a pass ont stac) Rgists $v an $v (2 3) a s to tun aus ompocs Rgsts $ t $ t 9 (1 24 2) a ca sa gts ana us tmpoay quantitis tat o not n o ps wn apoc cas anot ta may aso s ts gists. In contastgists $ s $ s (123) a ca sa gists an o ong-iaus tat wi n to ps acoss cas. Rgist $s (29) is tstac point, wic points to t ast ocation in s on stac. Rgst

    $ f (30) is t am point. poc ca am is an aa o mmoyus to o aios ination assocatd wit a poc suc asagumnts sa gists an oca aias as iscuss n apt 8Rgist (1) is wittn wit t tun ass wn a nw pocu ca. g 8) a goa o a o a 4K oc o mmoy tat os constants an goa aia Tojc in t pa o mmoy can qcy acc w a n ao sto instution

    al 3 P regsers he coeo gover her use,

    Rst nam Numb Usa

    eo $ os $ 1 Reseve ssemev0v $$ xpesso evo ess o co$a-$a3 $$ ge 107

    $ 8 15Tempoy (no peseve coss c

    -7 $$ em eseve ss c$k0$k $$ O enelp 8 Poie o o e$p $29  c poefp $ 3 0  Fme poe

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    31/191

    1/0 ORO

    MIPS prr ig pi rgir hd umr in ig pi rm, u

    .59

    TE SPM SMULTR

    i imulr h ru prgrm r h 0/00 mpur . SPIM n rd d immdiy xu ining mlygug SPIM -ntin ym r rug t prgr ndnan a debe and nee a ean sysm ri.

    r r y dng uig mhi imur lik SPIMMP wrn r t gnry , nd t m w npri r my yr u f rpid prgr dig w d rmur rul h rn mk mpur r y xutigr intruti urrty mk hir rhiur mr diul udrn n prgrm i my gug Smutr pr ttrnirm r w- prgrmming hn ul mhi u hyan de e s and d mr fur h u mpur.

    One metho t t uy my rgrmming i

    pily digd irui rd ih prr, mmry nd riu /di dimld dpm yl i muh r wh imur tn wndg my prgrm imp mrprrte n ton uh ytem r r t rwr rm wmn h h ame nee e ee a b a d r u rdwr prm. Wit mutr t pty o ppning i rmd, ug r i wy t piility f ug in imur fwar Suh ug r ir d d x hn inrmihrdwr iur.

    SPIM h n X-Wid ir h i r m duggr rt u m. T y didg imd mhi i prgrm wi ru wr n rl mh, lhugh hi i prm r ig imp wrkd.

    h Uix ri f prid impl rm d Xiinr. pr ut ty, ntr r r u nd mr inrmi. h imur i i ur hr r ih d PC ri f h imur il t pu dmn ( Pr

    / Z

    hi i xpli w ipu nd upu (/) i rgid y SPIMr A ompute oul e uele thout lo level ote ru

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    32/191

    I COR ORZO

    ogm mkeyem cll

    k

    ingk

    enelo do 1/0

    conol 1/0devicediecly

    -

    3 perag ye all

    Appprgrs

    K

    1

    Hrwre

    kl g h hg kw h pacula ca ha ach /vc ua applca pga ak h kl / ba a cal a hw u 34 h l l h call b alkg cl h hawa call a ag a hav hu cal

    SPM v a all 10 ag--lk vc huh

    h call sysall uc c h ula a ls ag qu a vc a pga l h call c ( abl 32 g v agus g o oat pot vlus Sst clls ha u valu s s £ g u u h call wll b xpla a a xapl pga h llwg cha. apl tt a a ap o e cool p sl a ubadt a a l u u a clu h w a

    u a Caac g h ub a g pa pa ug

    Tabl 3 M' ye a

    Svc Ca co Amts Rt

    i_ a

    ger

    pn_loa = oatnoube

    1 2 =

    insn = snea v0)ea_loa a $ )e_oul 7 uble ( ean $ buer, $ leghsb oun ddess

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    33/191

    RC 23

    U

    as a basd da a b sdg u th oost

    l ruco o could aord to a a alt h uuual cas oor col orato ad ak a larg t ga rra cos o a ubr o cll, ac o ch l hold o ghbt ubr orb A gas addrss sa s sd a g whch hol h ruco r a ogra th daa gt adh tack sgt PS rocor cota 32 gral-uro rgsad covto hav b ablhd a hw gt hould b ud Aalcao rogra ak t krl to do /O b akg calls chth krl lts b talkg drcl o th harar SP aulator ha ru rogra r h MIS cu

    EEIE

    3 1 a s bas da bd SC sor?3 2 ha go a t sg?3 3 ha he a unte .4 cu h avaa ula ach 3 5 o /O orgazd P?3 a s br h rur addr rgstr

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    34/191

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    35/191

    HA

    A mp pogam

    4 IODUCIO

    hs chapter ens y ounn th sta s i a P assly laaror I en conidr imp xamp proram The nsucos s iths proram are nroduced e SIM oai too is h descred.eaed nsruos cti th xl oa i SIM arei dditioa sil oad to nd rmec nsructons ae itodcdtot wt so a roas i sai s

    assl porm i all hl i a l with . a at th of th

    n r emle he o . Ts s processe ne by ne by theP oa ach li i th soc co l ca ithr traat o aachi istctio (o ral achi istctio i th cas o aassey pseudonsruo) ca at ata lt() to tor imemor or ma prove nmaon o he ssemer rorm

    The he o a eow onans th sorc co of a rora o s A l r dt ur n o tem o n mmoy as shown n ure arcern conuous senc of SI ys Fre 28) with a ewos a s ro s to iicat t of t t h loiasl proram sets up a sr n e daa seme. txt sas scos ss llowdb a sstem ca to et the poam

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    36/191

    2

    3

    # ## # e l l o a t s out " e l l o wo l d "# # a o t s t o te s t

    # #

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # text semet #

    0   # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    37/191

    7

    9

    2

    2 2

    2 3

    .

    s  r

    $ v 0 , 4sys a

    v , sys al

    ORC COD OR 2

    # exeutin stats ee ing ddess ino sysem a rn# ut a strin

    # au revo i . . .

    2 4 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

    2 # #2 6 # ata sement #2 7 # #2 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #2 9

    3 0 . ata

    3

    3

    s t

    # # 

    . a s i i " e l l o \ n "

    3 4 # # en f f i l e e l . a

    Lie umbers are only icluded n e proram r t sae o carya are ot etere we tpi i roams Irmatio i the dataseme es t cnai instructions tht are executed but rather is aaused dur oa eco

    Reardess o the use o a particuar i e o the surce ce te rmat sreative taar, ivie it ur es serae b b

    l ae : ] opera t i on pean ] , p eand ] , op ean ] # oent

    It is extreme mortat in a anuaes bu especially asselyauae to iet the ce roery si e tab or space es to mae theproram as reaabe s sse, h by he authr a hers racs ] )dicae a opioal eld, so o all elds appear o each lie Comments areoptona i the enitio o he anuae, ut must be sprikled iberall i aassemy roram t reaabty eenin on the articlaroeratio ad the eeds o the roram, a ae ad oerad(s) ma e

    required o a lie

     42 COMMNS

    Commets i assemer es i wh a shar sin (# ) verti om tea i d f l i i d b a bl Si a b

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    38/191

    PO

    a ae rora a he orce coe w ohewe ea a uea a a a poa wn n a nli C

    4.. A

    etier are a eqce o alphaec charace ea a o( ) hat o o ei wih a er exaple st i lie 3 1 o he o a Ocoe r inrction uch a a o i are reerve wor tat are otvali ietir Labl are eclar b pttin intier at e bei oa ln lowe by a coon a can be een i bot t text an ata ento h e o a he choo a ieer it a oo iea o pic oe whichha a eai hat icreae e reaabilit o t prora r exaple str he ar o a rin rather han a L the t lael proraer who e eaile label at time o coi wll eve etro o alei he a wil e uale o re o the pora a ewwee ie Oe lael are ep o wer eih character o cilitath ratti o te orce ab

    f a abl i rnt it to aociat t ybo wt t mory

    are o a varial locae he aa ee o he are o aintruction in t txt ent

    4..3 O RAI O FI D

    The oeraton el cotan thr a recve ach che tco ha a peca o o eoc

    aociate with it e ll et o SPIM eonic i lie peix C a paricla uco eee y the poaer he correoonc plac he opraton e or exapl th o n he o a o caue he loa are rctio o e place a thpoint in te prora

    The oprao e ca alo ho a aele rectve whch oe otraate to a ache rco I he o . a the rectve dat to tel he aele o place what low the ata eet o theprora

    4..4 OERA D FI LD

    Ma ache iucto eqie oe or ore opea o example o a ete ae lael a uerical qae are e a

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    39/191

    EIION OF hel lo . 9

     42 CONSANS

    constant is a vaue tat does ot cane durin proram assemb or

    execution Proram e lo . a ses bot inteer ad character strn constantsIf an iteer constant is specied withot indicatin its base it is assumed tobe a decima number o indicate a number i hexadecma, prex he numberwith Ox and use eiter ower- or uercase etters a f or AF

    strin costant is eimited b dobe quotes ( ) r exampe

    l o o d \

    Special charactes in strins ow te C convention

    nelineta

    quote

    \ n\ t

    \ "

    SPM supports two assember irectives r caracter strins

    s  d

    stores te SCI btes in memor but does ot n -termnate them

    . a s i i "a"

    stores te strn memor a uermae

    DSR F

    he exampe proram e o . a prints out te characters heo word I is aver simpe proram which sets up te strin in te data sement and maes

    sstem c to prit ot tis strn i te tet semet, owe b ssemca to exit the proram Ater the iitia commens, the

    t et

    directive (ine 1) tes te assember o ace wha ow he esemet Te owin two ines (nes 11)

    o

    stt

    s t a rt : b o t rt intruco o t t ambr nidenti where execution soud ben prorams in this boo wi havetese ree ines unced

    s mentioned aread SPIM provides a sma set o opeatin-sstemie services throu te ssem ca sysa instruction o request a

    i d t t d ( b ) i t i t $

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    40/191

    30 AN EXAMPLE MIPS PROGRAM

    integer, etc) and the arguments into registers $ a 0 $a3 o set up the

    system call it is necessary to load values into the registers The la, or load

    address instruction (Figure 42) puts an address into a register (line 16). It

    takes two operands, the rst being the register and the second being the

    address Note the register names all begin with $ .The dagger (t in the instruction set reence means that a i s a

    pseudo-instruction In order to make it easier to write, read and unerstand

    ou od mbl oid om inuin wi d ncorespond to a single machine instructon but instead consist of a seuence achine instructins As we shall see shotly whe we execute the

    program instruction by instruction, called single stepping la reuires twomachine instuctions since te address is a 32-bit quantit and is teree

    a pseudo-instruction.

    Processor Memory

    $ t 0 O x O O O O O O

    O x 1 0 0 1 0 0 0

    x

    O x 0 0 1 0 0 0 3$tl

    O x 0 0 1 0 0 0 4

    IO x 0 0 1 0 0 0 5

    $t2 O x 1 0 0 0 0 0 6

    O x 1 0 0 1

    O x 1 0 0 1 0 0 0 8

    x 1 9

    OxOOOOOA

    O x O O O O O B a , od ddss,puts an address

    O x O O O O O C

    into register

    O x O O O O O D

    O x O O O O O E

    O x l O O O O O F

    O x 0 0 0 0 1 0

    §

    O x 1 0 0 1 0 0 1 1

    O x 0 0 1 0 0 1 2

    O x 1 0 0 1 0 0 1 3

    O x 1 0 0 0 0 1 4

    �O x 1 0 0 1 0 0 1 5

    O x 1 0 0 1 0 0 1 6

    O x 0 0 1 0 0 7

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    41/191

    PT ORY O RC 3 1

    i l ,

    l d mmede

    p e mn nn n ege

    Prcessr

    $ l

    $ 2

    u 3 l : la ete strts

    Mer

    O x O O O O O O

    x O x 2

    x 3

    x

    O x 5

    O x 6

    O x 7

    O x 8

    O x

    O x O O O O O

    O x O O O B

    x C

    O x O O O O D

    O x O O O O E

    O x

    O x

    O x

    O x 4

    O x 5

    O x 6

    O x 7

    (u 4) s oa mmat (ln mm mns hrocessor exacs a ale om e insrcion isel, no o emory, andsores in e eister e size o e consant is lited to bits tispsdo-nsrucn c n sinle cine instrction. od immedieas wo opns, s is an scon n nber to be loaded

    TT TY T

    Ts setion dscsses e Unix ersion o SPM Versions r P C or Mcinosre ost idetc XSM is n X-Window licion t meens a environn Th nv t

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    42/191

    3 A A A

    Tpe cden eed

    A Fnd n e.

     

    s

    __

     

    _

     

    - () pm d bn Spm tp

    Fnd lgc e

    .

      ___ () pm ep bn

    u

    t sseme test cyce

    Spm tp

    Te basc meod o f prorammn w XSPM s o ave e XSPMwndow open, ad also a dor wdow op w e cod aeworn on To es yor code, yo load and r n XSPM. Usally yowll nc os cd, s a cs cd edor, save yor code om yor edor clear e XSPM envronmenload cod XSPM, ad al cd XSPM 

    cycle connes nl yo've removed all e bs om yor code. Fre 44sws sps d a ssl pa w.

    .. 1 SARI N XSI

    XSM pe xsm ot & and pess ene Te ot opons needed so a conrol wll ben om e

    _ _stt label n yor proram.

    en e PM wndow comes p, noe e dvson o e wndow a eop e crren saes of all o e resers n e smlaed macne aredsplayed re e vales o ese resers w cane as yo rno proram. Below e reses are e conol bons (Fe 46), wcae sed ell XSM wa o do.

    ex s e ex semen Fe 4 wc s bon no ese ecolmns lsed le o r)

    • e address of e nscon

    e cd sc (c )• do o o, o s d xpc sss

    • e lne nmber of e code om yor roram c o poam a prodced s nsrcon. oe

    ta te nscton n yo code may not matc eacly te nstcton e td colm bca om instructons are atually

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    43/191

    P ORY O PCTC 33

    , - • 0 0 4 0 0 0 0 0 0 0 0 0 0 0 0 0 s

    0 0 0 0 0 0 0 0 B A r 0 0 0 0 0 0 0 0

    S u s 0 0 0 0 0 0 0 0 H 0 0 0 0 0 0 0 0 L 0 0 0 0 0 0 0 0

    Gr srs \ 4 ( 0 0 0 0 0 0 R ( 0 0 0 0 0 0 0 0 ( s 0 0 0 0 0 0 0 0 2

    2 ( v 0 0 0 0 0 0 0 0 ( 2 0 0 0 0 0 0 0 0 ( s 2 0 0 0 0 0 0 0 0 2

    R 0 0 0 0 0 0 0 0 l l 0 0 0 0 0 0 0 0 R4 ( 0 0 0 0 0 0 2 ( 4 0 0 0 0 0 0 0 0 2 0 s 4 0 0 0 0 0 0 0 0 2

    R f f f 4 R 3 0 0 0 0 0 0 0 0 R 2 ( s 0 0 0 0 0 0 0 0

    R 2 f f f c R l 4 0 0 0 0 0 0 0 0 R 2 2 s 0 0 0 0 0 0 0 0 R30

    ( 3 0 0 0 0 0 0 0 0 ( 0 0 0 0 0 0 0 0 3 s 0 0 0 0 0 0 0 0 R 3

    D F srs

    4

    F

    P

    4 0 l

    P

    4

    Rege $O

    e vlue000000

    0

    0

    0

    P P 0 0 F 0 2 0 F30 0

    S F srs

    0 4 0

    F 0 F 0

    P F2 0F 2 2 0 P 0 0

    0 0 0 0 0 0 0 0( 0 0 0 0 0 0 0 0

    ( 0 0 0 0 0 0 0 0

    ( ( p 0 0 0 0 0 0

    s p f f f 0

    s 0 0 0 0 0 0 0 0

    ( r 0 0 0 0 0 0 0 0

    u 4 SPM ew o rrent state o a o e ege n ecne

    qu ) ( load ) ( reload st p (c l

    e vaue rin) (breakpoins hep (ermnal) ( mode

    u 4 Control buttons to tell SPIM wha o do

    Below e e semn is e daa seen Fire 8) were yo can

    see e daa orion of yor code e XSPM ool rins o e daasem d word f d in reresens caracers e ASC codes r fr caracers will aea in word e order o y n od dd on a eariclar comr on ic XSPM s rnni has een ult A the ve te wnow s e messas area PM is

    f db if i d l i

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    44/191

    3 P P PROR

    g [ O x 0 0 4 0 0 0 0 0 J O x 3 c 0 1 1 0 0 1 u i $ 1 , 4 0 9 7 [ s t r ] 1 6 : l a $ a 0 , # str put strng addre

    [ 0 x 0 0 4 0 0 0 0 4 O x 3 4 2 0 0 0 0 0 r $ 4 ,

    [ Ox 0 0 4 0 0 0 0 8 ) O x3 4 0 2 0 0 0 4 r $ 2 ,

    [ O x 0 0 4 0 0 0 0 c O x O O O O O O O c s y s c a

    [ O x 0 0 4 0 0 0 1 0 ] O x 3 4 0 2 0 0 0 a or $ 2 ,

    [ O x 0 0 4 0 0 0 1 4 ] O xO O O O O O O c s ys c a

    $ 1 , 0 s t r

    $ 0 , 4

    $ 0 ,

    /

    Highl ghed ne beeee

    ; 1 7 l $v0 ,

    1 8 sysca

    2 0 : $v 0 ,

    ; 2 1 sysca

    Text sement broen into tese ve columns

    da

    AA[ OOOOOOOD ] - [ OOOOf f f c ] OOOOOOOOO

    [ OlOOO c OOOOOOOOO

    # system ca

    1 0

    O10010000 O6c 6 c 6 5 6 8 O6 7720 6 O0a6 6 c72 OOOOOOOOO

    [Ox100000� 0x0020000] OOOOOOOOO

    SK[ O7 a c OOOOOOOOO

    wd od 0 00000

    iu 4 aa segmen of MIS program

    4.4.2 LD EMBLY

    o p i

    Oe yo e te SPM wdow ope yo eed to lod yo poPess te lod btto d, wow wll oe p (e 9) sk yo eme. ype te me of yo pom d pess ssee e' XSPM wll poeed to lod d sseble yo pom ook temess e e of te SPM wdow eos tt SPM y e d

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    45/191

    PUING THEORY INO PRACICE

    input filename

    / h e l l o . a

      ssemble e (  bort coad

    \

    u 4 Wnow se to oa a e n SPM

    is also possile o se e coad lie inerce o e SPMprora y ypi sp -notrap e llowed by e leae, wc willsw y syt s i til wiw

    ..3 UI MBLY M

    Oce yor prora as sccesslly loaded eai XSPM th le

    ad did o ecoer ay errors wile asseli i) yo ca r i Frs,ss th eral bo ad plldow o op cosole', wic will ww t will il proraprodces To r yor prora press e o e 4 1 0 wll e asked r e address a wic yo wis XSPM t tiT dul addess sold e coec so ess e o' o ad yorprora wll

    di' see o or oerl loo a e essaes I

    r rgram

    stai addess Ox4

    �bo

    coa )

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    46/191

    .. ROADI NG AN D R-XCUI NG AN ASSB Y

    ROGRA

    Ual o poa wo' wr obr can o yor cod a yo o o . o modid yor cod i yor dior ic i i amd yo a op alonwi XSPM), pr clar' bu XSPM widw ad ull idow o mmory & rir'. Ti will clar a o imulaedmacin o a yo an r w rio of yor proram. Nowpr lad' and XSPIM rmmbr lam o your proram, o pr ambly l' ad XSPM will rlad i. Fially, pr rn' ad

    o' o rxc yor proram.

    4..5 DBUGGING AN ASSBY ROGRA

    Syax rrr ar ay o la ad x ba amblr willauoaiall ll ou li ur w aud rol Silig xl oa a it wa to d a loial rror

    i yor rogra by ioiig ri lin wic ad rorDbi wi SPIM aly amo o il ppi ro anrn n r rr n rn h hn n h hrir. T arilar li a rgra cour a rad iigligd i x g widow a roor o durig xioT mod exoe mo b icly To e ro your proram yom ir lar a brapoi or ar ppin om start labl linby li. braoi i a o i our rogra wr XSPM wi l orarild xio o a o may iw dd a of imlad

    mn A ovn ou m IM o n unyor rra i i iry or uil i ur aor brai), r uay rou your rra by li. Braoi ar ul o paor a loop cly ad aod l ppin arod may m.

    o o o of yor prorm om rpo or ry ro your proram, r p o. T r p p wow r

    4 1 1 ) wic allow yo o r p ro yr prra pr p'o or o o o yor prorm pr o o.

    a braoi yu u br a uwr yo wi d xui rbr addr i r

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    47/191

    D ND E INON 37

    - d

    . : · � . .

    rorm

    mbr o

    J h l l o .

    \

    p

    )

    coninu

    bort cond

    gu 4 Window sed o sngle sep n SPM

    pou

    r

    O x 0 0 4 0 0 0 0 8

    (

    a

    :

    )

    (b rt c mand

    u Wow se to set a reaont

    B rkn ' n (Fgr 1 2) en yp n ddre f

    rkpn nd pr d' n y rn yr prrm, SPM wllnd xecn rn y cd pl mll rkpn e ekpoin wndw (pr rekpn' un m SPM winw) llow y mnn l o bkpns

    4.LD ND SRE INSTRUINS

    l n o o o o owr m nd sore rd n mmy mr. n wrk n bye lb n b(r . 1) rrmen r lmn; mmry ddr

    l b f

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    48/191

    3 XP P PROR

    r o c e s s o r

    $ t $ t l -2 [

    I

    w d dcpe d fmmem in egie

    i 4 o wor nstruon

    eo

    O x O O O O O O

    O x

    O x 2O 3

    O x

    O

    O x

    O x 7

    O x 8

    O x 9

    O x O O O O O

    O x O O O O O B

    O O O O O C

    O O O O D

    O x O O O O E

    O x O O O

    O

    O

    O 3

    O x

    O

    O x

    6 RTHMT NSTRUTNS

    m o uo o o

    mn

    No orr o orn e rmen re e conen of het o h o t dd mod om hon on n o onn n s:

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    49/191

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    50/191

    e assembler wll aomacall enerae an nsrcon r o Also consan o a reser. s

    $ t 0 , 7me

    $ t 0 , $ t 0 , 7

    4. ULIPII D DVISI

    acal P nsrcon s

    ,

    wc mn mlpl conn

    s by otet o t Se the eut o mut wo -bi nmbers can be o 2 bs IS ses wo secial reisers called l o and to hod theeut o mutto Se move tuto o dt om eeete . 3 toom lo and ou o't ave o worry oo mcabo e deails o th The eudotuto ul dest , s , S2 assmble ino ral nsrcos o do e mlly

    u $ t 4 , $ t 4 , $ t

    i s e same u l t $ t 4 , $ t m o move om e o este

    Sm thee div est , s , S2 sdoinsrcion o d oneer dvison Proram on 88 wll rovd an xaml of sue Tbe 1 umme the ue o the IS oeo thmetuto

    Tabl 4 mpls MPS rihmi isruis

    Instucton

    dd $ t $ t 2 , $ t 3Add mmedate dd t 2 5 0Subr s u b , 2 ,

    Multply m , 3

    Mve H mfh

    Mve f

    Exampl

    # $ t 1 = $ t 2 + $ t 3

    1 = 2 5 0# 1 2

    # , L = $ t x $ t 3

    = # H2 mo

    # H# ge copy of H

    # Lo

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    51/191

    PCO VO 4 1

    rogram t emp . a ass a usr r a mratur i sius ortsit to Farit a rits t rslt

    user for eerare in Ce ls is , cvr Fahrhi , ri he rs l t .4

    5 # # O - reads n e s i�s

    6 # ld s ar e e i r e s l

    7 a - o in s s r in gs

    8 # #

    9

    1 0 # # # # # # # # # # # # # # # # # # # #1 1 #

    1 2

    3

    #

    ex segen #

    1 4   # # # # # # # # # # # # # # # # # # # # # # # # # # #

    1 5  

    6

    7

    1 8 s a r :1 9  2 0

    2 1

    2 2

    2 3  

    2 4

    2 5  

    2 6  

    2 7

    2 8  

    2 9  

    3 0  

    3 1  

    3 2  

    3 3  

    3 4

    3 5  

    3 63 7  

    3 8  

    3 9  

    4 0  

    1  

    4 2

    . ex

    ob s a r

    l a $ a 0 , r

    i $ 0 ,

    sys a

    $ 0 , 5

    sys

    l $ 0 , $ 0 , 9

    di $ 0 , $ 0 , 5

    add $ 0 , $ 0 , 2

    l a $ a 0 , ans

    i 4

    s ys al l

    e a 0 0

    $ 0 ,

    sys al

    l a a 0 , endl

    li $0 , 4

    sys a l

    $ 0 1 0

    rin r rinal

    # ssal 5 rads a igr

    nr , i 9 ,

    d i de 5 , en

    add 2

    rin sring eore resl

    # rin res

    # syse all o rin

    # o a newine

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    52/191

    42 N EME M RGM

    4 4

    4 5  

    # # # # # # # # # # # # # # ## # # # # # # # # # # # # # # ## # # # # # # # # # # # # # # # ##4 7   # #

    # aa sg # 9 # #

    5 # # # # # # # # # # # # # # # ## # # # # # # # # # # # # # # # ## # # # # # # # # # # # # # # #5 1

    5 a a5 5 5 5

    5 5 5

    a s z " E ra r ( s s )

    as s " T a n r s

    as " \ "

    # #

    # # n a

    m a teet beue t ue ve to ed tee(e 2) tht houd be eu temetue It ue the m eudotuto (e 26) d the div eudo-tuto (e 27) to or out theemee n n n n o uom

    oetn tem (Aedx A), t mott ot to ue ve to edn put The dt uh om be toed the dteme

    8 I XL

    mat a belo omm ueto tht tet oede o thethmet tuto toethe th the od ord tuto The ueto n npu m umc n ym c ll nAppedx A

    e: nv yo s to' l h th booo houd ttet to te te o ed the oe e o thel n pc pv n ln

    2 # #

    # # # #

    # #

    6 # #

    S e a t .

    Q s n

    t A * X 2 * X C

    tt rmt mst

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    53/191

    OGMMING EXME

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 0 # # # tet segent # 2 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #1 4  

    1

    t t

    g b l_ s t t : stt cton stts e2 0 # ny cnges bve ts ne be scre by # s t o ns tn dsd n s 2 2 # - - - - - - - stt ct - - - - - - - - - - - - - - - - 2 3

    2 4

    2

    2 # - - - - - - en ct - - - - - - - - - - - - - - - - - - - -2 # ny cnges bel ts lne l be dsce y2 # s t yo nse eteen dse l ne s 3 0 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #3 1

    3

    ## t segent

    #

    3 3

    3 4   # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #3 5  

    3

    4 0

    4

    4 2

    4 3

    4 4

    X

    C ns :

    n :

    # # nd

    d t

    od

    .o 4

    s c z nse

    s c z \ n

    o l e t

    H n ttt outon to th uton wh onn orror i cion ow ow o orcll dbn o inl ro od and loa rror in o lion

    # ny cnges bve ts ne be scre by2 #

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    54/191

    N M MI OGM

    4

    7

    9

    # solto t o m th NTE IT C NANS A UG ! ! ! !

    1 0  

    1 1  

    12  

    1 3

    1 4  

    1 5

    1  

    1 7

    1  

    1 9  

    2 0  

    2 1  

    2 2  

    2 3  

    2 4  

    2  

    2  

    2 7

    2 8  

    2 9

    3 0  

    3 1  

    3 2

    3 3  

    4  

    l $ t 0 , Xl $ t , A

    l $ t 2 ,

    l t 3 , C

    ml $ t4 , $ t 0 , $ t 0

    ul $ t4 , $ t4 , $ t

    u $ t , $ 2 , $ t 0

    $ t4 , $ t4 , $ t $ t4 , $ t4 , $ t 3

    l $ 0 , s

    l $ v 0 , 4

    sys cl l

    move $ 0 , $ t 4

    $ v 0 ,

    syscll

    l $ 0 , el

    $v0 , 4

    c

    l $v0 , 0

    c

    # #        -  -  -  -  - -    - e u

    # t 4 X 2

    # t 4 A * X 2

    # t * X

    # t 4 A * X 2 + * X# t 4 A * X 2 + * X + C

    # system cll to t# ot stg

    # t elt o teml

    # syst em cl l to t

    # out ele

    u ev

    # Ay chge elo t le ll e scre

    ir u o ee e l i

    is aemped solion sed e l insrcon o ge e vale o AB, and X ino regisers om memory ines 81 1 ) so a e armeic

    uo b u o u h u u ( 11) L Te solion also ses e move psedo-insrcion line 23) o move a

    vae om oe register to anothe so that e be $ 0 hesysem ca s s a goo exampe o ow an assember can e a messymh om omm

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    55/191

    MMI N EME

    means copy e conens of $ t o $ t 0 MPS does no ave a movensron n he ahne angage B oes ae a a aeie a away as e ae zero a very common consan Macinene prorammers wo wan o copy t o to wod we e

    quivaln

    b e assemby lanae prorammer can wrie move and ave eassembler ranslae i ino

    f e above aemped solion i s rn e answer prined o is 1 52 noe expeced 1 8 e bes way o nd e b is o oad e proram inoe XSPM ool and sine sep ro i wacin e vales in e reisers

    as eac insrcion is eeced Fire .15 sows XSPM afer e sep'bon as been presse r mes coesponn o e rs r macneinsrcions o e rs wo assemby lanae psedo-insrcions ecorec vales can be see in e reisers

    f e sep bon is repeaedy pressed everyin oes accorin opan ni e proram coner reaces x (Fire 1 6) we ibecomes clear a e proramme as inadverenly yped $2 isead of$ t 2 I is easy o aler is in a ex edio and clea ad reload XSPM

    afer wic e poram will ncio coecly

    Pgm h gne h gneun no no

    PC 0 0 0 0 0 0 EPC C

    0 0 0 0 0 0 0 0

    S 0 0 0 0 0 0 0 0 H I L 0 0 0 0 0 0 0 0

    ( 0 0 0 0 0 0 0 0 ( ( 0 0 0 0 0 0 0 0 R R t 1 0 0 1 0 0 0 0 R t ( ) 0 0 0 0 0 0 0 0 R 5 ( t 9 )R V O 0 0 0 0 0 0 0 0 R O t 0 0 0 0 0 0 0 0 R 8 s 0 0 0 0 0 0 0 0 R 6 k O

    R3 V 0 0 0 0 0 0 0 0 R t 3 0 0 0 0 0 0 0 0 R 9 s 3 0 0 0 0 0 0 0 0 R k

    R O 0 0 0 0 0 0 0 R t 4 0 0 0 0 0 0 0 0 R 0 ) 0 0 0 0 0 0 0 0 R 8 g R a = f f f e a f R 3 t 0 0 0 0 0 0 0 R 0 0 0 0 0 0 0 0 R 9 s p

    R 7 c R 4 6 ) 0 0 0 0 0 0 0 0 R ( 6 ) 0 0 0 0 0 0 0 0 R30 ( 8 )

    R7 a 3

    0 0 0 0 0 0 0 0 R 1 t 0 0 0 0 0 0 0 R 3 ( s 0 0 0 0 0 0 0 0 R3 1 r a

    0 x 0 0 4 0 0 0 0 0 x 3 c 0 0 0 i 4 0 9 X 2 w 0 X

    O x 0 0 4 0 0 0 0 4 ] O x 8 c 8 0 0 0 0 8 0 1 ] 0 0 0 0 0 0 0 7 [ ] � $ [ O x 0 0 0 0 0 0 c ] O x 8 c 9 0 0 0 9 4 A J

    0 x 00 4 0 0 0 1 0 ] O x3 c 0 1 1 0 0 1 i 1 4 0 9 [ B ] 6 w t B

    x 0 0 0 0 0 x 8 0 0 0 8 0 8 ( 1 B J

    0 x0 0 0 0 0 1 8 ] O x3 c 0 1 0 0 i 1 4 0 9 J 2 t 3

    x 0 0 0 0 0 c x8 2 b0 0 0 c 2 ( C

    0 0 0 0 0 0 0 0

    0 0 0 0 0 0 0 0

    0 0 0 0 0 0 0 0

    0 0 0 0 0 0 0 0

    0 0 0 8 0 0 0

    f f e f f c

    0 0 0 0 0 0 0 0

    0 0 0 0 0 0 0 0

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    56/191

    N EXME MI RGRM

    Pgmcne

    t hdO9

    PC 0 0 4 0 0 0 3 00 0 0 0 0 0 0 0

    0

    0 0 0 0 0 0 ause

    tatus

    R O O

    2 O

    R3

    R 4 a O

    R a

    R 6 a 2

    R 7 a 3

    0 0 0 0 0 0 0 0 R t O

    0 9

    0 0 0 0 0 0 0 0 O 2 0 0 0 0 0 00 0 R t 3

    0 0 0 0 0 0 0 1 R 1 2 t 4

    7 f f f e af 4 R 1 3 t

    7 f f f e af c R 1 4 t 6

    0 0 0 0 0 0 0 0 R t 7

    R 6 s O

    R B 2 R 9 s 3

    0 0 0 0 0 0 9 3 R2 0 s 4

    0 0 0 0 0 0 0 0 R2 1 s

    0 0 0 0 0 0 0 0 R 22 s 6

    0 0 0 0 0 0 0 0 R 23 s 7

    0 0

    0 0 0 0 0 0 0 0 R 2 4 t

    0 0 0 0 0 0 0 0 R2 O 0 0 0 0 0 0 0 0 R 27

    0 0 0 0 0 0 0 0 R 2 8 0 0 0 0 0 0 R2 9 s p

    0 0 0 0 0 0 0 0 R3 0 s

    0 0 0 0 0 0 0 0 R3 a

    [ O x 0 0 4 0 0 0 2 0 ] Ox 0 1 0 8 0 0 8 u t 8 8

    [ O x 0 4 0 0 2 4 O x0 0 0 6 1 2 f $ 1 2

    [ O x0 0 4 0 0 2 8 O x 0 1 8 9 0 0 1 8 u t $ 1 2 $ 9

    [ O x 0 0 4 0 0 0 2 c ] O x0 0 0 0 6 0 2 f $ 2

    2 9 u t 4 t 0 t 0

    O x 0 0 4 0 0 0 0 O x0 0 4 8 0 8 ru l t $ 2 8

    O x 0 0 4 0 0 0 3 4 ] O x0 0 0 0 6 8 1 2 $ 1 3

    O x0 0 4 0 0 0 3 8 ] O x 1 d 6 0 2 0 add $ 1 2 $ 1 2 $ 1 3

    O x0 0 4 0 0 0 3 c ] Ox 1 8 6 0 2 0 add $ 1 2 $ 1 2 $ 1 1

    Fgu 4SPIM when h e g ehe

    3 0 u $ t4 $ t 4 $ t

    ru t 5 2 t 0

    3 2 add $ t 4 $ t 4 $ t

    3 3 add $ t 4 $ t 4 $ t 3

    0 0 0 0 0 0 0 0

    0 0 0 0 0 0 0 0

    1 0 0 0 8 0 0 0

    7 f f f e f f c

    0 0 0 0 0 0 0 0

    0 0 0 0 0 0 0 0

    wo more exale rorams a es or ndersandin of earme nsrons are a2 a an a3 a

    S tt l e t2 2 est:4 # # c a c u a t e 2 3 # # tt t s t e 7 # # " anser = 24 2 "

    9 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # ext seent #

    # # # # # # # # # # # # # # # # #  

    5 ex

    6 . g lb l _ _stt7

    s t t : # ee t stts ee

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    57/191

    RGMM NG EME 7

    0 y ges ove ts le ll e sre y

    psrk . Put your ser etee s l es .

    - - - - - - - - - - strt ut - - - - - - - - - - - - - - - - -

    5

    6 - - - - - - - - - - - e ut

    # y g lo t l ll y psrk . Put yur sr t se l es .

    9

    0

    t sgt

     

    5 6 . t

    3 7  

    3 8  

    3 9  

    4 0  

    4  

    45

    69

    0

    5

    9

    X :

    s

    el

    . r

    . s z s er

    s z

    # # d o l e

    trt l t .

    u es t o:

    u t ( 3 ) * ( +4 )

    utput rt us t

    ## er 9

    text seget#

    # #

    _ _ . t ext

    . g l o l str

    0 y ges ove ts le ll e sre y

    srk . ut our ser etee se l e s .

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    58/191

    4 EXME M OM

    2

    2 5

    2 6 ut - - - - - - - - - - - -

    2 y gs lo ts l ll sr y

    2 psrk ut your sr t s l s .2 9

    0

    1

    3

    3 3

    t sgt

    3  

    3 5  

    3 6 t

    3

    9

    0

    s :

    l

    o r 1 0

    s z s r

    s z

    o f f l t h3

    49 SUMMAR

    d o u o tu n o h ssmb ou od hmt tv tndd, dvdd nto u d td b tb It x on o ndn h od o un h b d o ddommns ibr o mk h prorm rdb s po n od tom i s to t, d nd undtnd ou od mb ovdsom r psdo-instrtions i do no orspond o nmn nuto, u td ont o un o mhn

    nson A m d o uon th ond

    4 1 Wht t SI u mn idnti?2 W s h din bn sn o nd o or?4 Wh s th pos o n sppn Wht bpont?4 . 5 d d ?4 W r ommnt nd indntt ion so mpotn in smb nu th dn tn l l l nd l48 In ht od houd th ond o thm nd o intuton

    b d?

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    59/191

    HA

    C w strucue

    IRDUI

    Ts oos om l enh a t ue om toor o n o a caracr srin Famiiari it a ssmbn nstrtions, s s bsi od, sto nd me meoeons s neee, toet e one o om oos A omoo os an oraion o b ra a nmbr o ms, o onr t assmb na nsrton xt For exme, to sm 50nmbes one od no av 0 ad nsrcons in roram, b insado av a nsrcion onc and o ron a oop ms

    5.2 NRL SRUURES

    n an abstrc vi, orons n orams can b dividd nto o ros

    a maniaon rsson aaion an assnmns coo rmin ic insrion o n

    om s on o v, on h nmen n ono nstutons jus tee to e e oue e te tamnpuon nucon n h oe son ued o tete o o ono e non bne nd m A bn es nmb o nstrutions bov o beo e een nson o mov orm con ras mp spcs aca addrss o mov o

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    60/191

    CONTROL FLOW STRUTURES

    smpst cono nstcon m

    mans jump  to addrss   which cue he oceo o fc x

    instuction fom the  word at address  . Implementation is  acuallyey me - jut the vaue o the adress t t pga cu Aal ternative m of the jump instuction s

    R s c

    mea jm to te ae e ete sc

    5.3 N TNAL BRANH ES

    jmp instction is aso non as an nconitiona m meanin tatte poceo aas oe to te jm taet ae Iteeti oamnee a a to mae conitiona banes e MIS ontiona bancinstrctions av nas tat bin i b b is r bancs an rmps) amps:

    bq $ t 0 , $ t , < >bq z $ t 0 , < b $ t 0 , $ t , < >

    bt $ t 0 , $ t , <

    bncbc

    bc

    bc

    tt

    t

    to

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    61/191

    XM M N 1

    a e o P , nc n core nrcon

    Instucton

    n o eqa

    ea

    Exampl

    beq $ t l , $ t 2 , 8 0# i f ( $ t l == $ t 2 )

    go o PC+4 + 8 0

    ne $ l $ t2 - 3 6 f ( $ t l ! = $ t 2 )

    e e n s t $ tl $ 2 $ t 3 # i f ( $ t2 < $ t 3 ) $ t l = l

    # e s e $ t l = O

    Se e hn eae s t $ tl $ t 7 # f ( $ 2 < 7 ) $ t l = l# e s e $ t l = O

    J

    J reger

    J n n

    O x 0 0 4 0 0 0 6 8

    r $ r a

    a O x 0 0 4 0 0 0 1 4

    cn k consn s on opn, mpl,

    bl t 0 , , l

    # g o o O x 0 0 40 0 0 6 8

    # g o t o $ r a

    # $ra = C + 4

    # go to Ox00400014

    mean an t lp f 0 a nme e an equal ale5 1 liss sa o som o MIS poo' mp bn andompa ino

    EXM PLE PO M U I N OOP

    pom l gt pins o ln o sin s t

    2 l g t pts ut t lgt cct

    s t g " s t .

    # #

    # #

    t l s c byt st g tu

    t cts cut ccts

    t 2 pts t t stg

    0 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 

    x

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    62/191

    2 C LW C

    x

    g l b l

    sa

    s a :

    la $ 2 , s l i , O

    nxC : lb $ 0 , ( $ 2

    # xcuin sas

    # 2 pins sing# ls cun

    g a y r r

    7

    8

    2 0

    2

    2 3

    2 4

    2

    2

    z , s d z o ms d o s

    a , , # incmn cun

    a $ 2 , v pin n caac

    nxC g u lp agai

    2 s n : l a a 0 , ans2 8 l i v 0 , 4

    2 syscall

    03 1 mv a 0 ,

    3 2 l i v0 ,

    3 3 sys cll

    3 4  

    3 5 l 0 , nl

    3 6 l i v0 , 43 syscall3 8

    3 l i v0 , 0

    4 0

    4 14

    sys cal l

    sysm call pin# u a ssag

    # sysm cll pin

    # u lng wk u

    # sysm cll o

    # u a nwlin

    # au vi 4 3   # # # 4 4 # #

    4 smn 4

    4 7 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

    4 8

    4

    0 1

    5 2

    3

    s :ns :nl

    a a

    as c i i z " l l

    a s c i i z " L ng s c i i \ "

    5 5   # # n i l l ng a

    wl "

    i s

    e cce n n ue n memo o e

    ga st th lb t t (l 2 1 ) t b

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    63/191

    XAMP PROGRAMS USING OOPS

    e ete ued to old temmoy ass of s n sn ne ss an addessnmode known as ndct addssn ecase we ae no loadin te ale n , sn ale n e eiste as te addess o tebte to load Te backets ndcate ndect addessn Reste $ 2 issoetes e a pone o addess of te uantt of nteest e a instction is needed to moete onte $ alo te ti (tt i, dd oe to it), lookin r teeo bte tht e te e (e 4). dd lso ued to incemet tecount of caactes in the stn eld i este (ne ) ngh . auses te bqz banc on eual zeo psedo-instcton, line o banccoditionll to te istuctio te lbel s n (lne ) f e

    contents of $ 0 euals zeo, sinifn te end of stn, s sown nFue 4 1 An ucoditoal jump (le 5) s used o etun to te lbelnC (lne 1) epeatedl poessn to te net caacte in te stnTs se lue o could be a wh l sttement i a ileel luae

    Anote nteestn eample pommn ueso tt w oou undestandn of te conditional bncn and poam loops s a

    S a . a2 3 Q u s i n :4 R a c a c c u nc s w

    A in h s ing " caaay " an

    in su ing sing

    uu m m b e :

    9 " AbbbAAbbbAbAbAb "

    0

    2

    3

    4

    sgn

    6

    9

    2 0

    2

    s a : . star cuin sas h2 2 Any cangs abv s in i b isc by

    i k b i

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    64/191

    4 NL LW E

    2 6

    2 la $ t2 , caaay t pnts s n

    2 l i $ ,

    2 nxtC : b $t 0 , ( $ t2 # gt a byt m st ng3 0 bqz $ t 0 , st n z mans n st ng

    3

    bn $ t 0 , a , n t a

    ( ) o a 3 3

    3 4  

    nta : add

    nxtC

    # mv o

    g un t p agan

    3 5  

    3 6 stn :

    3 a $ a 0 , a a ay3 l $ v 0 , 4

    3 9 

    4

    4 1

    4 2  

    4 3  

    44  

    syscall

    $v0 , 0

    syscall

    - - - - - n cut

    pnt

    au v i

    4 # b w s w l b ds ad by

    4 6 ipsmak Put yu answ btwn as ns .4 4 8 #

    4 #

    0

    5

    #

    daa sgmn #

    # 2   # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 3

    4   a a

    aaay

    6 as c z " abbbaabbbababab\ n "

    7  

    5 # #

    5 # n l p a

    I hs pa he bn stuct (le 3 1 ) s use t esue tha au A ly t t y whe th chct th pwas a lwe case a hs wul be a i stateet a hhlevel lauae.e ha ' ' le 28 s use ea the SCII ce f a lette Le32 uses the sb ste byte stuct (Fue 5) t wte a vlue este he st e xaples f he use f he he

    t l t ll b th b t

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    65/191

    EXAMPLE PROGRAMS USING LOOPS 55

    Processor

    $t0

    $tl

    $t2[

    sb, store bytecopies a byte froma register intomemory

    Figure 5.1 sb: store byte instruction.

    Memory

    O x l O O l O O O O

    §

    O x 0 0 1 0 0 0

    O x l 0 0 1 0 0 0 2

    O x 0 0 1 0 0 0 3

    O x l 0 0 0 0 0 4

    §O x 0 0 0 0 0 5

    O x 0 0 0 0 0 6

    O x 0 0 0 0 0 7

    O x 0 0 1 0 0 0 8

    0 0 0 0 0 9

    O x

    l O O O

    O x l O O D O B

    O x l O O l O O O C

    O x l O O l O O O

    O x O O O O O E

    O x 0 0 0 0 0 F

    O x 0 0 0 0 0

    O 1 0 0 1 0 0

    O x 0 0 0 0 2

    O x 0 0 1 0 0 1 3

    O x 0 0 0 0 4

    O x 0 0 0 0 5

    O x 1 0 0 0 0 6

    O x l 0 0 0 0 7

    Two mo pogams that test your uestaig o f these istructiosa oop4 a nd oop a

    1 Stat o f f l e l oop a

    2 # #  3 Ques on :

    4 # #   Swap each pair of eements n

    5 he s ring " chararray " and

    6 # print the esu ting st ring .

    7 # #   Thee w aways be an even number

    8 f h t " h "

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    66/191

    6 ONO OW E

    0 # # upu a us b # # b a c 2

    1 3   # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 4

    6

    ###

    x sgn###

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # x

    2 0 g b l

    s

    2

    2 22 3

    s a : # xcun sas

    2 4 # Any cangs abv i s in wi b s ca by2 # psak . Pu yu nsw bwn as n s .2 # - - - - - - s cu - - - - - - - - - - - - - - - -2 7  

    2 8  

    2 9  

    3 0

    3 3 2 3

    # - - - - - - - - n cu - - - - - - - - - -

    # Any cangs blw is n wl b isc by# psk Pu yu nsw wn as l ins

    4  

    3 6  

    3 7

    3 8  

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # ###

    a sgn ##

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

    4 0 . data  

    4 caay

    2   s c " b cde f "4 end : s c z " \ n "4 4

    4 # # # # End l op

    a

    # # a l l p . a2 # #3 # # Qusn:4 # # Rplac vy scn caac # # T in i

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    67/191

    EXME RGRM NG

    7 # # ee ll ala e an een ne8 caac e n ca • 9 # #

    # # en n e e n n # # # # u a e # # XX 5 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 6 7 8

    ###

    e een

    ###

    1 9  

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 1 5

    _ _ a e

    l a # eecn a ee

    6 # An cane ae lne ll e cae 7 # a P ane eeen ae l ne # - - - - - - sa c - - - - - - - - - - - - - 9 # - - - - - - - - - en c - - - - - - - - - - - - - - - - - - - # An cane el lne ll e cae # a P ane eeen ae l ne 6 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #3 7   # #3 8  

    3 9  

    ##

    aa egen ##

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #4 1  

    a a ca

    5 enl

    a c z " a ce

    a c z \ n

    7 # # n l e l a

    t t t t t f l

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    68/191

    8 N W E

    55 SMMARY

    ll the acn n an assembl lanuae pam taes place n the

    assnments and the cntl nstuctns ae ust thee t help the cmputee the data manpulatn nstuctns n he h de pam lpallws an peatn t be epeaed a numbe mes whu havn n bl lnu nun pll bnh p hnumbe nstuctns abve belw the pesent nstuctn t mve thepam cunte wheeas a ump speces the actual addess t mve thepam cunte t n uncndtnal ump means that the pcess alwases t the jump taet addess but nteestn pams need a wa t maecndtnal banches as well.

    EXERSES

    5. 1 What d cntl nstuctns d?5. 2 What s a cndtnal banch?5 . Hw des a lp detemne the end f a chaacte stn?5 4 escbe the ndect addessn mde.

    55 What s a pnte?5 . 6 What assembl lanuae nstuctns des a cmple use t mplement a

    whe p?5 7 Ho re AS rter oes oe nto resters n MS ssemy?

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    69/191

    CHA

    Addrin d

    1 INOUION

    For n ve oerto such s lo or brnch there re oten nerent ws to specf the ress of the oper(s) he erent ws of

    eternn the ess re clle ressn oes hs chpter loos tthe eret ressn oes o the MIS processor n shows ow nstrucons cn t nto snle urbte wor. Soe sple pos renue to show ton essng oes n ton

    M PS N STR T N FRMATS

    ver S nstructon ossts of snle bt wo lne o worouna Thee ae hee iee insuion mas: I pe pe Jpe s show n Fure 6. 1 he prts of ech rt hve the llownen

    t oeton coe• s 5 bt source rester specer rd

    secer

    5 bt tret (sourceestnton) ester or brnch conto

    eae 16 bt ete brnch splceent or ressspceet are 26 bt u tret dress• sham 5 bt shft ount ncton el.

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    70/191

    ADDRESSING MODES

    6

    o Operation ource

    registercodespecifier

    o

    ource

    egser e t

    Target Destiatioregister register

    s pecifier s pecifier

    Iee ypet

    6

    mt t h i f t Functio

    aout f i e l d

    mt

    Operation arget registerregister specifier or ediate brach displaceent orcode

    6o

    Operationcode

    s pecifier branch condition

    Jp ype 6

    ttTarget address

    u P co

    address displaceent

    W hav ala al u MIS a whl cnsn th pas e l l o a an length . a hs a:

    • st assn • ba as • at asn • Clatv assn .

    o sos o s os os o s o s o assn c s plnt by t assbl s a suntuto

    6 S RGSR DDRSS G

    st assn (Fu 2 s t splst assn nstuctns

    usn sts xcut ucl bcaus th av th las asscat wth ccs ul o ss s l afw bts a v t slct a st st assn s a fct assn bcaus w a ntst n t nub t stath tha u that ub a as at ustan Fu 6. s f th cs us b MS a shwn nF 6 3 h l f t l b t t l t b

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    71/191

    MIP GIT ADDING

    6

    o s

    0 0 0 0 0 0 0 0\ 'T 0 0 0 0 $ t 0 t $ t l t $ t 2

    u 6 P reger ddreng

    op

    0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 j0 0 0 0 jal0 0 0 0 0 baq0 0 0 0 be0 0 0 0 blez

    0 0 0 b0 0 1 0 0 0 0 0 0 0 au0 0 1 0 1 0 0 0 0 s l u 1 1 0 0 1 0 1 0 0 0 1 1 1 1 u1 0 0 0 0 0 l1 1 0 0 0 0 w 0 0 0 w 0 0 0 0 bu 0 0 0 lu 0 0 0 lw1 1 1 1 0 0 0 0 sb1 0 1 0 0 1 0 0 0 swl 0 0

    he o i o he opeioi deemied b ohe ield

    rt

    0 0 0 0 0 b z0 0 0 0 1

    0 0 0 0 0 0 0 1 geal

    6

    sha u

    0 0 0 0 0 0 0 0 0 0

    io oa i 2

    0 0 0 0 0 0 s l l0 0 0 0 0 0 0 0 0 0 sl0 0 0 0 sa0 0 0 0 0 slv0 0 0 1 0 10 0 0 0 slv0 0 0 sav0 0 0 0 0 j 0 0 0 0 a 0 0 0 00 0 0 0 0 0 0 0 0 0 beak0 0 00 0 0 0 0 0 0 mfh0 0 0 0 mh0 0 0 0 m l o0 0 0 l 0 0 0 0 mu0 0 0 mulu0 0 0 0 0

    0 0 0 0 0 a 0 0 0 0 au 0 0 0 0 sb 0 0 0 subu 0 0 0 0 0 0 0 0 0 0 o 0 0 o 0 0 0 0 0 0 0 0 0 0 s l 0 0 slu

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    72/191

    DD D

    .4 MIPS ASE AESSI

    ata strcture that s ver prtat cputer prra s the

    recr calle a structure the C prra lanuae It s a cllect varables treate as a unt Fr eaple a persel recr ht nclueae aress ae a epartet rat It s cveet t eep thsrat as a ut r each eplee sn a recrd r srucur l

    stuct esonnel

    ca name [ 8 0 ]

    ca add e ss [ O O ]

    nt age

    ca deatment O ]

    I a a w a a all a a hl areser he rester a pt t a strucure r se ther cllect ata a we ee t la a value at a cstat set the ben the structure. Because each MIS struct ts e wr the se f thea l 1 h ax

    lw d , ( )

    xal f h a f a uu w ul at the elds s

    w $ t 4 ( $ t 0 )

    lw $ t 8 ( $ t 0 )lw $ t ( $ t )

    et c

    We hae use a r base aressn er se (Fur eg a (l 1) ( ) , h f a w

    medate

    $ 0 ( $ )

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    73/191

    MPS MMEATE ARESSNG

    s n sn n pan a atn w n

    . M I PS I M M DIA ADDRSSI

    It ssn ( 6) ans at n pan s a nsnwn h nn lf n f eg s an xapl f assn It assn as t vta f nt nn xta ass t t t pan bt t pan s l t 1 6bts n s.

    jp nsn an al b n n xapl ft ssn sn t stn s l t nsttnF 66 llstats ts sn ln 5 f l a

    6 6

    p ee

    0 0 1 0 0 0 0 1 0 0 1 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0

    !Oppcde dd s

    \ $ t , $ t ,

    u 6 MIPS mt r dd

    6 6p ar

    0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 1 1 1

    ! j ! s 2

    DestnatinO40003. A zers sincew-lg

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    74/191

    64 R O

    M PS P-ELATE AESS N

    -elve ddessn (Fue , whee he ddess s he su he

    p cune nd cnsn n he nsucn, s used cndnlnches le lne 22 n . a Bnch nsucns cn nl ve 32 ve elw he p cune ecuse he se s - wsclemen nume see Secns 2 nd

    7 EXAMLE RGM SNG BASE

    AESSNG

    ax a s nhe neesn p usn he se essnmde Fue 4 wh e se seches n wds hees nd slles eleens he

    r

    etve s se t set an aa 1 5 -te ws n the ata setn. aa ata stte s a nae lst tes ste n e, as shwnn Fu 68

    6 6

    p rs rt arss

    +

    0 0 0 0 0

    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

    bqz , s rE pcod obeq i 4

    Bnc inucion

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    75/191

    XAMPL POGM U S G BAS ASS

    Memoy

    O O O O O O O

    O x O 0 0 0 0 0

    O 0 0 1 0 0 0 3

    O 1 0 0 0 0 0

    IO 1 0 0 1 0 0 0

    O 1 0 0 1 0 0 0 6

    O 1 0 0 0 0 0

    O O O O O O

    O 1 0 0 1 0 0 0 9

    O 1 0 0 1 0 0 0 A

    O x l O O l O O O

    O O O O O O C

    O O O O O O D

    O x l O O l O O O O O O O O O

    O 0 0 1 0 0 1 0

    O 1 0 0 1 0 0 1 1O 1 0 0 1 0 0 1 2

    O 1 0 0 1 0 0 3

    O 1 0 0 1 0 0 1 4

    �O x O 0 0 0 0 6

    O 0 0 0 0 1

    u

    6.8 o wo to mmoy.

    Lin then ses the w nstuton to loa eah wo nto a esteAn porn rn n a ponr o a wo an a ponte to arr s or ponr s nrn r ron oop n 40), sne eah wo opes eo loatons

    1 # # i p , x s # #   # # Asss h hs s w s ( ]

    4 #   [ ) t i i t i i s b t i a ax t a [

    t th p is 6 Tis p wi s p s

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    76/191

    N M

    9

    0

    4

    # #

    # #  

    # # 

    #

    2

    3

    conns coun o ns

    conins min

    conns mx

      # # # # # # # # # # # # # # # # # # #  

    # 7

    8

    #

    #

    x sgmn #

    #

    9   # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

    2 0

    2 . x . g o _ _s a

    $ 0 , y

    $ , coun

    $ 2 , ( $ 0 )

    3 ( )

    a $ 0 , $ 0 , 4

    $ , $ ,

    # 0 i o o mns

    # xi oop n $ s 0

    # i ni i z o in ( $ 2 )

    # ad max ( 3 ) o a ]

    # p o n o s [

    # n go oun coun is

    3 4

    2

    7

    2 8

    2 9

    3 3

    4

    oop o nx o om y

    g $ 4 , $ 2 , n n

    # sk p i [ i > = n

    3 moe 4 # o a ] o m

    3 n oMn l $ 4 , $ , no Mx

    3 7 s k p [ < = x

    8

    4 0

    4

    4 2

    4 3  

    4 4

    4

    4

    4 74

    4

    0

    2

    3

    oMax

    moe 3 4

    add

    $ 0 $ 0 4

    n $ p

    l $ 0 ns

    $v0 , 4

    ss c

    moe a sc

    $ 0 , ns 2

    0 , 4

    s s c

    # o a ] o max

    deeme oe

    # incn pn y o

    # n cnnu cun>

    # pin n

    # pin n

    # pin \n

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    77/191

    6

    7

    6

    6 2

    6 3

    4

    6

    EXAMPLE PGAM UNG BA ADD

    move $ 0 , $ 3

    $ v 0 ,

    sys

    $ 0 , en $v0 , 4

    sys c

    i $ v0 , 1 0

    sys c

    # pin mx

    sysem c # o nene

    # evo .

    6 6 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

    6 7 8

    6

    ##

    segmen ##

    7 0   # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #  

    7 1 7 4

    7

    7 7 77 8

    7 9

    y

    con :

    e n s s

    .

    . o 3 , 4 , 2 , 6 , 2 , 7 , , 2 6 , 2 1 4 , 1 , 7 , , 1 2 , 1 3

    . o 1

    s c i i z \ n . s ci z mi n. s c i i z \ nm

    8 1 # en o f e m nmx .

    o mor mp porms s o of s of

    ors oo . n o op .

    1 # #   o i e oop . # #  

    3 # #   Ques on

    4 # #   c c e he sm o he eemens in y

    # # coun os e nme eemens in y

    7 # #   om ms b sm

    0 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #1 1 # # ex segmen 1 3 # #

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    78/191

    8 RING M

    7

    8

    92 0

    __ a ex

    g a r xc ar

    2 y chage ave his e i e dicaded

    a o a a l

    2 3 - - - - - - - - - - - a cu -

    2 4

    2

    2

    2 7 - - - - - - - - - - - - ed cu - - - - - - - - - - - -2 8 y chage el hi ie i e di c arded y2 ar P yr ar eee a 3 03 # # # # # # # # # # # # # # # # # # # # # #

    3 2 4

    d s #

    3

    3 6  

    3 7  

    3 8

    3 9  

    4 0  

    4

    42  

    4 3

    23

    4

    6

    7

    8

    array

    cu

    a s

    edl

    da a rd 3 , , 2 ,

    rd 4 a c "

    a c z " \ "

    d i e a

    S ar l e l 6 a

    u s o

    " uers " i s a array ve rd

    acua e e u a e ee i ue

    e va ue e ha 0 0 0

     

    uu a us e 0

    3

    #

    x seme

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    79/191

    EXMLE GM NG N

    6

    1 7

    1

    02 1

    e x

    g o l

    sar

    exec sars here

    2 2 y chages aove hi s ie i l e di sc arded y

    an n es 2 4 - - - - - - sar cu - - - - - - - - - - - - - -

    2

    2 6

    2 7

    2 - - - - - ed cu - - - - - - - - - - - - - - - - -2 y chages eo his lie i e dscarded y 0 ipsar your aser eee dashed ies

    3 2

     

    3 4 daa sege

    3 6  

    8 da a3 numbr

    0 o 0 0 0 0 0 0 4 3

    4

    a n

    ed

    . c i z 1 u

    s c " n

    d o f f e l o op 6 a

    the exame as that test u unestann f paps sn aas f ws ae n Appen A 3

    .8 EXAMLE RGRAM SNG NDXD

    RESSNG

    A use e accessn eeents f an aa s cae neearessng'. The assemer gves he prmm e pn f usn ts ett nce s m cann n he

    F

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    80/191

    M

    u co o g ess spece n e nscon. e sn s

    ar r

    A oo o vslze nee essn s o nk of e secon opena a arra acc lk

    ar [ rx ]

    s moe lso pples o oe o n soe nscons ke lb f eqantt t e ae t te egste es nt t n 16 s he

    eneres s essn moe s pseonsrucon Prorm out ases nee essn o o lon sn conn he occences o prclr crcer.

    c a c ccc f a pcfc

    ha a t e t " t "

    exe are i ue to a e array e emet

    6

    Ot

    ho idex c

    eah yte from trig i turto aayo ccrc

    caracr c

    1 1

    1 2 tt mt

    1 6

    1 8 o at t r t

    i t t

    l b $ t 3 , char

    t e th aay ex t2 e th out

    a n d $ t 3 w i l l h o l d t h e c h a r

    2 2

    2

    2

    6

    7

    2 8

    oop lb $ t t r ( $ t ) f et h ext ha r

    b q z $ O , r f a l l , x lp

    e t $ t o ot u ame a ha r?

    a t 2 t ye emet outdd t t d

  • 8/19/2019 Introduction to RISC Assembly Language Programming-John Waldron--191.pdf

    81/191

    XP PO X 7

    3 sd:

    2 a $ a0 an

    i $ v 0 4

    5

    3 6  

    7

    3 8  

    3 9

    4 0

    4

    4  

    4 3  

    4 4

    4 5  

    4 6  

    y ca

    ove $ a 0 $ 2

    $0

    y ca

    d

    i $ v0 4

    s s c l l

    l v

    y ca

    ye ca o prin u a eage

    ye call o prin ou ou wo ou

    ssm ll o ou neine

    au revo ir

    4 7

    4 8

    4 9  

    5 0

    5 1  

    5

    daa egen

    a a5 3

    5 4

    5 5 6

    5 7

    5 8

    r a c i i aceece eeeacac "

    char :a n

    dl

    5 9  

    a c i i e

    a " C o un t

    a c i " \ n "

    d o l ou

    "

    Lin 2 u rir n ndx ino n rry Sn bdde , t wl ot t ny of te rmt Fure 1 . Te emleru to ud te ddre d te ue te e ddre mode,gure .4 to cce te operd.

    t )

    ecoe s te vlue OxOOOOOO)

    l 4 7

    dd $ $ $

  • 8/19/2019 Introduction to RISC Assembly La