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 BasicsJump to procedure:jal <label>–Saves return address to $raReturn from a procedure:jr $ra$a0 - $a3 to pass arguments$v0 and $v1 to return valuesSave 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 registersIf 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 25MiscellaneousMIPS 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 saveIf >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 3JVM is a stack machine–Portability–CompactnessOur simplified JVM consists of:–execution stackInstructions take parameters from the stackInstructions place results onto the stack–Pointer to top of the stack–local storageJust a big array for storing data–Java bytecode program–Program counter10/8/2003 CSE 378 - Section 27Emulating JVMInterpreter:–Get next instruction–Decode it–Execute–Store results–Repeat10/8/2003 CSE 378 - Section 28Emulating JVMProbably 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, v2Use 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 InstructionsArithmetic (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 2POP9…POP…DUP and DUP254…DUP254…5410/8/2003 CSE 378 - Section 211Loading from local storageILOAD, 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 212BranchesPop one thing off stack, compare with zero using specified condition, update PC if trueTake 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 0x05BranchesTo 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