DOC PREVIEW
UW CSE 378 - MIPS Procedure Calls

This preview shows page 1-2-3-4-5 out of 15 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

MIPS Procedure Calls JVM and Assignment 3Procedure Call BasicsCalling proceduresSetup at the start/end of procedureMiscellaneousJVM and Assignment 3Emulating JVMSlide 8JVM InstructionsJVM Instructions 2Loading from local storageBranchesSlide 13Example: a=a+b+cExample 2: if (b==0) a=3; else a=5;MIPS Procedure CallsJVM and Assignment 3CSE 378 – Section 310/8/2003 CSE 378 - Section 22Procedure Call BasicsJump to procedure:jal <label>–Saves return address to $raReturn from a procedure:jr $ra$a0 - $a3 to pass arguments$v0 and $v1 to return valuesSave certain registers to preserve across procedure calls.–Use the stack$t0-$t9, $a0-a3, $v0-v1 – caller-saved.–Caller’s responsibility to save if expects to use these after a call.$s0-$s7, $ra, $fp – callee-saved.–Callee’s responsibility to save if callee uses them.–Save at beginning of function, restore at end10/8/2003 CSE 378 - Section 23Calling proceduresTo call a procedure:1. Put arguments into $a0-a32. Save caller-saved registers3. jal <proc>4. Restore caller-saved registersExample: <some stuff here, uses $t2>…# set up a call to myproc(4)addi $a0, $0, 4subu $sp, $sp, 4sw $t2, 0($sp)jal myproclw $t2, 0($sp)addiu $sp, $sp, 4… <use $t2 again>10/8/2003 CSE 378 - Section 24Setup at the start/end of procedureBefore any procedure starts running, it must:1. Allocate memory for callee-saved registers 2. Save callee-saved registersIf calling another procedure inside, must save $ra! (why?)At the end of procedure:1. Place return value into $v02. Restore callee-saved regs3. jr $ra myproc: # wants to use $s0 insidesubu $sp, $sp, 8sw $ra, 4($sp)sw $s0, 0($sp)…<do some computation in $s0>…addi $v0, $s0, 42lw $s0, 0($sp)lw $ra, 4($sp)addiu $sp, $sp, 8jr $ra10/8/2003 CSE 378 - Section 25MiscellaneousMIPS stack conventions:–$sp double-word aligned–Minimum frame size is 24 bytes (fits four arguments and return address)–Don’t use it for projects–Other rules flexible too: have to use common sense for what you need to saveIf >4 arguments, use the stack to pass them–Caller, callee must agree on where they go in the stack and who pops them off.10/8/2003 CSE 378 - Section 26JVM and Assignment 3JVM is a stack machine–Portability–CompactnessOur simplified JVM consists of:–execution stackInstructions take parameters from the stackInstructions place results onto the stack–Pointer to top of the stack–local storageJust a big array for storing data–Java bytecode program–Program counter10/8/2003 CSE 378 - Section 27Emulating JVMInterpreter:–Get next instruction–Decode it–Execute–Store results–Repeat10/8/2003 CSE 378 - Section 28Emulating JVMProbably need SPIM registers for:–Pointer to top of JVM stack–Pointer to current JVM instruction (PC)–Holding a couple of values from the stack (when pushing/popping) – v1, v2Use SPIM static data section for:–The entire execution stack (1024 bytes maximum)–the local storage area–The program itself sequence of instruction opcodes and parameters10/8/2003 CSE 378 - Section 29JVM InstructionsArithmetic (IADD, ISUB, IMUL, IDIV)54…Stack:IADD9…Load constant (BIPUSH for 8-bit, SIPUSH for 16)Stack:…4…BIPUSH 410/8/2003 CSE 378 - Section 210JVM Instructions 2POP9…POP…DUP and DUP254…DUP254…5410/8/2003 CSE 378 - Section 211Loading from local storageILOAD, ISTORE – load/store 32-bit word using unsigned 8-bit index into storage…Stack:1 4 5 7 0 5 …Local storage:081624…ILOAD 37…- Represents a 32-bit word10/8/2003 CSE 378 - Section 212BranchesPop one thing off stack, compare with zero using specified condition, update PC if trueTake a signed 2-byte offset from current PC–No “labels” in bytecodes, just offsets9…JVM program:BIPISH 0x09IFGT 0x00 0x05BIPUSH 0x42IADDPC10/8/2003 CSE 378 - Section 213IFGT 0x00 0x05BranchesTo understand offset destinations, add up opcodes (1 byte), along with any arguments–E.g. IFEQ 0x00 0x05 takes 3 bytes, IADD takes 1.Part II: Perl script will resolve labels9…PCJVM program:BIPISH 0x09IFGT 0x00 0x05BIPUSH 0x42IADDCondition 9 > 0 true;Update PC10/8/2003 CSE 378 - Section 214Example: a=a+b+cAdd first 3 words in local storageStore the result into the first local storage wordILOAD 0ILOAD 1ILOAD 2IADDIADDISTORE 010/8/2003 CSE 378 - Section 215Example 2: if (b==0) a=3; else a=5;Assume a is local word 0,b is local word 1:ILOAD 1IFEQ skipBIPUSH 3ISTORE 0GOTO endifskip: BIPUSH 5ISTORE 0endif: …To bytecodes (use perl script) .align 2test2: .byte 0x15, 0x01 # iload .byte 0x99, 0x00, 0x0a # ifeq .byte 0x10, 0x03 # bipush .byte 0x36, 0x00 # istore .byte 0xa7, 0x00, 0x07 # goto .byte 0x10, 0x05 # bipush .byte 0x36, 0x00 # istore .byte 0x00 # .align


View Full Document

UW CSE 378 - MIPS Procedure Calls

Documents in this Course
Encoding

Encoding

20 pages

Load more
Download MIPS Procedure Calls
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view MIPS Procedure Calls and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view MIPS Procedure Calls 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?