CSE315 Computer Organization Lecture 4 MIPS ISA
Loading, Storing bytes In addition to word data transfers (lw, sw), MIPS has byte data transfers: load byte: lb store byte: sb Same format as lw, sw What to do with the remaining 24 bits? lb: sign extends to fill upper 24 bits MIPS instruction that doesn't sign extend when loading bytes: load byte unsigned: lbu Spring 2009 CSE315 - Lec 4 2
True Assembly Language Pseudo-instruction: A MIPS instruction that doesn t turn directly into a machine language instruction. What happens with pseudoinstructions? They re broken up by the assembler into several real MIPS instructions. But what is a real MIPS instruction? Spring 2009 CSE315 - Lec 4 3
Example: Pseudoinstructions Register Move move reg2,reg1 Expands to: add reg2,$zero,reg1 Load Immediate li reg,value If value fits in 16 bits: ori reg,$zero,value else: lui ori reg,upper 16 bits of value reg,$zero,lower 16 bits Spring 2009 CSE315 - Lec 4 4
True Assembly Language MAL (MIPS Assembly Language): the set of instructions that a programmer may use to code in MIPS; this includes pseudoinstructions TAL (True Assembly Language): the set of instructions that can actually get translated into a single machine language instruction (32-bit binary string) A program must be converted from MAL into TAL before it can be translated into machine code (1s and 0s). Spring 2009 CSE315 - Lec 4 5
Function or Procedure A separate piece of code Possibly separately compiled Located at some address in the memory used for code, away from main and other functions (main is itself a function called by the system) May return a value to caller Function may use parameters provided by caller Spring 2009 CSE315 - Lec 4 6
Function call Store parameters where function can find them Store address where control should return after call Go to function code Store information in registers that need to be used by function, so they can be restored Get parameters Execute function Put result where it can be found by caller Spring 2009 CSE315 - Lec 4 7
Function call continued Restore information in registers Return control to caller at appropriate address Caller get results Spring 2009 CSE315 - Lec 4 8
Function call -- issues How to save return address? How to provide parameters for function? How to save information stored in registers? How to store return value of function so it is available to caller after function terminates Spring 2009 CSE315 - Lec 4 9
MIPS solution Mixed: keep as much as possible in registers Spill register contents to memory stack when needed Single function calls often use only registers Deeper function calls always use stack how much depends on register usage by functions Spring 2009 CSE315 - Lec 4 10
MIPS Register Usage ra (31) used for function return address sp (29) used as stack pointer v0, v1 function return value(s) a0, a1, a2, a3 function parameters s registers guaranteed to hold same values across function calls (e.g. value after call is the same as it was before the call) t registers values may be changed by function call without being restored after Spring 2009 CSE315 - Lec 4 11
Function Call Caller saves the registers that are not preserved across call Of course, needed only if caller wants some value to be preserved $a0 to $a3, $v0, $v1, $t0 to $t9 Callee saves the registers that are required to be preserved across call Needed only if callee uses these registers $s0 to $s7, $gp, $sp, $fp, $ra Spring 2009 CSE315 - Lec 4 12
Instructions Supporting Procedure Calls Jump and link jal procedure address note: return address is stored in R 31 Return jr R 31 Spring 2009 CSE315 - Lec 4 13
Example swap Procedure (1/2) swap (int v[], int k) { int temp; temp = v[k]; v[k] = v[k+1]; v[k+1] = temp; } Spring 2009 CSE315 - Lec 4 14
Example swap Procedure (2/2) Register assignment: $a0 base address v of the integer array $a1 index k $t0 local variable temp No stack operation. void swap (int v[], int k) { int temp; temp = v[k]; } v[k] = v[k+1]; v[k+1] = temp; swap: add $t0, $a1, $a1 add $t0, $t0, $t0 add $t0, $t0, $a0 lw lw sw sw jr $t1, 0($t0) $t2, 4($t0) $t2, 0($t0) $t1, 4($t0) $ra Spring 2009 CSE315 - Lec 4 15
c code: Leaf Procedure Example int leaf_example (int g, h, i, j) { int f; f = (g + h) - (i + j); return f; } Arguments g,, j in $a0,, $a3 f in $s0 (hence, need to save $s0 on stack) Result in $v0 Spring 2009 CSE315 - Lec 4 16
Leaf Procedure Example MIPS code: leaf_example: addi $sp, $sp, -4 sw $s0, 0($sp) add $t0, $a0, $a1 add $t1, $a2, $a3 sub $s0, $t0, $t1 add $v0, $s0, $zero lw $s0, 0($sp) addi $sp, $sp, 4 jr $ra Save $s0 on stack Procedure body Result Restore $s0 Return Spring 2009 CSE315 - Lec 4 17
Non-Leaf Procedures Procedures that call other procedures are known as non-leaf procedures. For nested call, caller needs to save on the stack: Its return address Any arguments and temporaries needed after the call Restore from the stack after the call Spring 2009 CSE315 - Lec 4 18
C code: Non-Leaf Procedure Example int fact (int n) { if (n < 1) return f; else return n * fact(n-1); } Argument n in $a0 Result in $v0 Spring 2009 CSE315 - Lec 4 19
Non-Leaf Procedure Example MIPS code: fact: addi $sp, $sp, -8 # adjust stack for 2 items sw $ra, 4($sp) # save return address sw $a0, 0($sp) # save argument slti $t0, $a0, 1 # test for n < 1 beq $t0, $zero, L1 addi $v0, $zero, 1 # if so, result is 1 addi $sp, $sp, 8 # pop 2 items from stack jr $ra # and return L1: addi $a0, $a0, -1 # else decrement n jal fact # recursive call lw $a0, 0($sp) # restore original n lw $ra, 4($sp) # and return address addi $sp, $sp, 8 # pop 2 items from stack mul $v0, $a0, $v0 # multiply to get result jr $ra # and return Spring 2009 CSE315 - Lec 4 20
MIPS operands Name Example Comments $s0-$s7, $t0-$t9, Fast locations for data. In MIPS, data must be in registers to perform 32 registers $zero, $a0-$a3, $v0- arithmetic. MIPS register $zero always equals 0. $gp (28) is the global $v1, $gp, $fp, $sp, $ra pointer, $sp(29) is the stack pointer, $fp (30) is the frame pointer, and $ra (31) is the return address. Memory [0], Accessed only by data transfer instructions. MIPS uses byte addresses, so 2 30 memory words Memory [4],, sequential words differ by 4. Memory holds data structures, such as arrays, Memory[42949672920 and spilled register, such as those saved on procedure calls. MIPS assembly language Category Instruction Example Meaning Comments Arithmetic add add $s1, $s2, $s3 $s1 = $s2 + $s3 Three operands; data in registers subtract sub $s1, $s2, $s3 $s1 = $s2 - $s3 Three operands; data in registers Data transfer load word lw $s1,100 ($s2) &s1 = Memory [$s2 + 100] Data from memory to register store word sw $s1,100 ($s2) Memory [$s2 + 100] = $s1 Data from register to memory branch on equal beq $s1, $s2, L if ($s1 == $s2) go to L Equal test and branch branch on not bne $s1, $s2, L if ($s1!= $s2) go to L Not equal test and branch Conditional branch equal set on less than slt $s1, $s2, $s3 if ($s2 < $s3) $s1 = 1; else Compare less than: for beq, bne $s1 = 0 jump j 2500 go to 10000 jump to target address Unconditional jump jump register jr $ra go to $ra For switch, procedure return jump and link jal 2500 $ra = PC + 4; go to 1000 For procedure call Spring 2009 CSE315 - Lec 4 21