Top Banner
CS 2630 Computer Organization Meeting 6: functions in MIPS Brandon Myers University of Iowa
27

CS 2630 Computer Organizationhomepage.cs.uiowa.edu/~bdmyers/cs2630_fa16/public... · 2016. 11. 15. · Function calls: only6 steps 1. store arguments somewhere the callee code can

Feb 03, 2021

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • CS2630ComputerOrganization

    Meeting6:functionsinMIPSBrandonMyers

    UniversityofIowa

  • http://qz.com/726338/the-code-that-took-america-to-the-moon-was-just-published-to-github-and-its-like-a-1960s-time-capsule/

    lotsmoreinfo:http://tcf.pages.tcnj.edu/files/2013/12/Apollo-Guidance-Computer-2009.pdf

  • Wherewearegoing

    Instructionsetarchitecture(e.g.,MIPS)

    Compiler

    Memorysystem I/OsystemProcessor

    Datapath &Control

    Digitallogic

    translatingsourcecode(CorJava)ProgramstoassemblylanguageAndlinkingyourcodetoLibrarycode

    HowthesoftwaretalksTothehardware

    HowaprocessorrunsMIPSPrograms!

    Howswitches(1or0)canbeusedtobuildInterestingfunctions:fromintegerarithmetictoprogrammablecomputers

  • Representationsofaprogram

    Compiler

    int x = arr[1];arr[2] = x + 10;

    lw $t0, 4($r0)addi $t0, $t0, 10sw $t0, 8($r0)

    Assembler

    100011100000100000000000000001000010000100001000000000000000101010101110000010000000000000001000

    Highlevellanguageprogram(humanreadable)

    assemblyprogramastext(CS2630studentreadable)

    assemblyprogramasbinary(machinereadable)

  • Storinganinstructionasbits• eachMIPSinstructionsis32bits,storedin“instructionmemory”

    8e 13 00 00

    8e 14 00 04

    02 74 90 20

    ae 12 00 08

    22 10 00 04

    22 31 ff ff

    1e 20 ff f9

    000000040008000C001000140018

    lw $s3, 0($s0)

    lw $s4, 4($s0)

    add $s2, $s3, $s4

    sw $s2, 8($s0)

    addi $s0, $s0, 4

    addi $s1, $s1, -1

    bgtz $s1, loop

    address contents meaning

  • PseudoinstructionsNotalltheMIPSinstructionsare“real”(i.e.,understoodbytheprocessor)TAL – thetrueassemblylanguage;MIPSprocessorsknowalloftheseinstructionsMAL – slightlymoreconvenientlanguage;eachMALinstructionisusuallytranslatedto1-3TALinstructionsbytheassembler

  • Pseudoinstructions(MAL)

    lui $t2, 0x0012ori $t3, $t3, 0x3456addu $t1, $t2, $t3

    assemblertranslatesitto

    Example:howmanybitsarerequiredfortheimmediateinthisinstruction?addiu $t1, $t2, 0x123456

    lui =“loadupperimmediate”

  • Morepseudoinstructions(MAL)• otherI-typeinstructionswithtoo-largeimmediate• move $rd, $rs• li $rd,[32-bitimmediate]• la $rd,[32-bitimmediate]• branchinstructionswith,=

    • reallyuseslt (setiflessthan),followedbybeq to0or1

    • Foryoutotry:usetheseinstructionsinaMIPSprogram,thenseewhatMARStranslatesthemto

  • Peerinstruction• Putthefollowinginto• (A)onlyinMAL• (B)TAL

  • Whataboutprocedures?• Firsttry:usethenameoftheprocedureasalabelandjustjumptoit

    • Whataresomelimitationsofthisproposal?(freeresponsewithclickers)

  • Functioncalls:only 6steps1. storeargumentssomewherethecallee codecanaccess

    them2. savecurrentlocationandthenjumptofunction3. allocatelocalstorageneeded(savingregisters)for

    runningthefunction4. performthecodeinthefunction5. storeresultsomewherethecaller codecanaccessitand

    restoresavedregisters6. jumpbacktolocationofthefunctioncall

  • MIPSregistersusedforfunctioncallsRegisterNumber ConventionalName Usage$0 $zero hard-wiredto0

    $1 $at reservedforassembler

    $2- $3 $v0,$v1 returnvaluesfromfunctions

    $4- $7 $a0- $a3 arguments

    $8- $15 $t0- $t7 temporary registers

    $16- $23 $s0- $s7 savedregisters

    $24- $25 $t8- $t9 temporary registers

    $26- $27 $k0- $k1 reservedforOS

    $28 $gp globalpointer$29 $sp stackPointer$30 $fp framePointer$31 $ra returnAddress

  • Administrivia• reminder:HW2duetonight11:59pm• Midtermscheduled

    • inclassonOctober6

  • Secondtry:allowmultiplecallsites bysavingreturnaddressin$raint mult_by_2(int x) {

    return x*2;}…int z = mult_by_2(5);

    reference:$ra returnaddress$v0 returnvalue$a0 argument

  • Convenienttip:usejal tosave$ra foryou!int mult_by_2(int x) {

    return x*2;}…int z = mult_by_2(5);

    reference:$ra returnaddress$v0 returnvalue$a0 argument

  • Problem:nomodularity!everyprocedureneedstoknowwhatregistersarebeingusedbytherestoftheprogramL

  • Conventiontotherescue!RegisterNumber ConventionalName Usage$0 $zero hard-wiredto0

    $1 $at reservedforassembler

    $2- $3 $v0,$v1 returnvaluesfromfunctions

    $4- $7 $a0- $a3 arguments

    $8- $15 $t0- $t7 temporary registers

    $16- $23 $s0- $s7 savedregisters

    $24- $25 $t8- $t9 temporary registers

    $26- $27 $k0- $k1 reservedforOS

    $28 $gp globalpointer$29 $sp stackPointer$30 $fp framePointer$31 $ra returnAddress

  • temporary registersmighthaveadifferentcontentsafteraprocedurecallastheydidbefore

    saved registershavethesamecontentsafteraprocedurecallastheydidbefore

  • Treatsavedregisterslikeyoutreatacampground

    Before LaborDayWeekend

    After LaborDayWeekend

    DuringLaborDayWeekend

  • Howtocleanupyourcampground• Beforeyouuseasavedregister,saveitsvalue.• Whenyouarefinished,restorethevalue.

    howdowesaveandrestoreregisters?

  • Callingprocedureswithinprocedures• Makesureoneoftheregistersyousave/restoreis$ra!

    • Refertothe“preservedacrossacall”columnoftheREGISTERtableontheMIPSreferencecard

    • “No”meansthecallerneedstosaveitiftheywantitlater

    • “Yes”meansthecallee needsto“cleanthecampground”beforereturning

  • Peerinstruction• Whatmust besavedbeforetheprocedurerunsandrestoredbeforeitreturns?

    multipleanswera) $t0b) $a0c) $zerod) $s0e) $s1f) $s2g) $rah) $v0

  • Memoryorganizationofprograms

    local variables, return addressesRW

    dynamically allocated memory like Java objectsRW

    globaldata(initializedwhenprocessstarts)RW

    assembledcode(initializedwhenprocessstarts)RX legend:

    R=readableW=writeableX=executable

  • Thestack• Thecall-stack(orjuststack)hasonepart,or“frame”,foreachactiveprocedure

    • anactiveprocedureisaJavamethod,Pythonfunction,etc thathasnotyetreturned

    • Theframestoresthestateneededbytheprocedurecall,includingreturnaddress,localvariablesthatdon’tfitinregisters,andsavedregisters

    • InMIPS,weuseregister$sp (stackpointer)tostoretheaddressofthecurrentframe

  • Example:usingthestack

    yoo frame

    who frame

    amI frame

    amI frame

    amI frameprocedureamI isrecursive(callsitself)

    stack

  • Example:usingthestackinMIPS

  • Templateforaproceduredefinition