1MIPS Procedure CallsJVM and Assignment 3CSE 378 – Section 310/8/2003 CSE 378 - Section 22Procedure Call Basicsl Jump to procedure:jal <label>– Saves return address to $ral Return from a procedure:jr $ral $a0 - $a3 to pass argumentsl $v0 and $v1 to return valuesl Save certain registers to preserve across procedure calls.– Use the stackl $t0-$t9, $a0-a3, $v0-v1 –caller-saved.– Caller’s responsibility to save if expects to use these after a call.l $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 registersl 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 $ramyproc: # 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 25Miscellaneousl 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 savel 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 3l JVM is a stack machine– Portability– Compactnessl Our simplified JVM consists of:– execution stackl Instructions take parameters from the stackl Instructions place results onto the stack– Pointer to top of the stack– local storagel Just a big array for storing data– Java bytecode program– Program counter210/8/2003 CSE 378 - Section 27Emulating JVMl Interpreter:– Get next instruction– Decode it– Execute– Store results– Repeat10/8/2003 CSE 378 - Section 28Emulating JVMl 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, v2l Use SPIM static data section for:– The entire execution stack (1024 bytes maximum)– the local storage area– The program itself l sequence of instruction opcodes and parameters10/8/2003 CSE 378 - Section 29JVM Instructionsl Arithmetic (IADD, ISUB, IMUL, IDIV)54…Stack:IADD9…l Load constant (BIPUSH for 8-bit, SIPUSH for 16)Stack:…4…BIPUSH 410/8/2003 CSE 378 - Section 210JVM Instructions 2l POP9…POP…l DUP and DUP254…DUP254…5410/8/2003 CSE 378 - Section 211Loading from local storagel 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 212Branchesl Pop one thing off stack, compare with zero using specified condition, update PC if truel Take a signed 2-byte offset from current PC– No “labels” in bytecodes, just offsets9…JVM program:BIPISH 0x09IFGT 0x00 0x05BIPUSH 0x42IADDPC310/8/2003 CSE 378 - Section 213IFGT 0x00 0x05Branchesl To understand offset destinations, add up opcodes (1 byte), along with any arguments– E.g. IFEQ 0x00 0x05 takes 3 bytes, IADD takes 1.l 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;l 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