MIPS Instructions J-type Instruction Encoding • We have jump and jump-and-link instructions – Note that we have 26 bits for the target fields, which represents the number of instructions (words) instead of bytes • In terms of bytes, it represents 28 bits in terms of bytes – But PC requires 32 bits • Where do we get the other 4 bits? 3/22/2016 week04-3.ppt 2 J-type Instruction Encoding • Pseudo-direct addressing – In J-type instructions, the jump address is formed by upper 4 bits of the current PC, 26 bits of the target address field in the instruction, and two bits of 0’s • What is the largest program a J-type instruction works properly? 3/22/2016 week04-3.ppt 3 Peek into the Future 3/22/2016 week04-3.ppt 4 Jump Register Instruction • Jump register (jr) – Unconditionally jump to the address given by register rs 3/22/2016 week04-3.ppt 5 Big Immediates • We know that we can use 16-bit immediate numbers in MIPS instructions such as addi – What about large numbers? • We also need to load 32 bit addresses in order to use jr – Load address (la) – Load the address of a label into the register (note: not the content of the address) 3/22/2016 week04-3.ppt 6 MIPS Addressing for 32-bit Immediates • In MIPS, the immediate field has 16 bits – In order to handle 32-bit immediate operands, the MIPS includes load upper immediate (lui) • Which sets the upper 16 bits of a constant in a register and fills the lower 16 bits with 0’s • Then one can set the lower 16 bits using ori 3/22/2016 week04-3.ppt 7 Example • To load the following 32-bit constant in $s0, 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 1 1 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 – We first need to do lui $s0, 61 • Why? – Then we need to do ori $s0, $s0, 35072 • Why? 3/22/2016 week04-3.ppt 8 Branching Far Away • Note that for conditional branch instructions the offset is 16 bits – What can we do if the branch is further away than the 16bit offset can represent? 3/22/2016 week04-3.ppt 9 Branching Far Away • Note that for conditional branch instructions the offset is 16 bits – What can we do if the branch is further away than the 16bit offset can represent? – We can replace it using a pair of instructions bne $s0, $s1, L2 j L1 L2: 3/22/2016 week04-3.ppt 10 MIPS Assembly Instructions add MIPS assembly language Example Meaning 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 $s1 = $s2 + 100 $s1 = Memory[$s2 + 100] Memory[$s2 + 100] = $s1 $s1 = Memory[$s2 + 100] Memory[$s2 + 100] = $s1 Used to add constants Category Arithmetic Instruction addi $s1, $s2, 100 load word lw $s1, 100($s2) store word sw $s1, 100($s2) load byte lb $s1, 100($s2) store byte sb $s1, 100($s2) load upper immediate lui $s1, 100 add immediate Data transfer Conditional branch Unconditional jump 3/22/2016 16 $s1 = 100 * 2 Comments Word from memory to register Word from register to memory Byte from memory to register Byte from register to memory Loads constant in upper 16 bits branch on equal beq $s1, $s2, 25 if ($s1 == $s2) go to PC + 4 + 100 Equal test; PC-relative branch branch on not equal bne $s1, $s2, 25 if ($s1 != $s2) go to PC + 4 + 100 Not equal test; PC-relative set on less than slt $s1, $s2, $s3 if ($s2 < $s3) $s1 = 1; else $s1 = 0 Compare less than; for beq, bne set less than immediate slti jump j jr jal jump register jump and link $s1, $s2, 100 if ($s2 < 100) $s1 = 1; Compare less than constant else $s1 = 0 2500 $ra 2500 go to 10000 Jump to target address go to $ra For switch, procedure return $ra = PC + 4; go to 10000 For procedure call week04-3.ppt 11 Procedures and Functions • We programmers use procedures and functions to structure and organize programs – To make them easier to understand – To allow code to be reused 3/22/2016 week04-3.ppt 12 A Simple Example Note that there are steps involved in the calling function as well as in the one being called 3/22/2016 week04-3.ppt 13 MIPS Calling Conventions • MIPS assembly follows the following convention in using registers – $a0 - $a3: four argument registers in which to pass parameters – $v0 - $v1: two value registers in which to return values – $ra: one return address register to return to the point of origin 3/22/2016 week04-3.ppt 14 Registers • Remember that the same registers are used by both the caller and callee – What do we need to do in order to guarantee the correctness of the program? • For the following code for example, what do we need to do? 3/22/2016 week04-3.ppt 15 Register Spilling • The callee has to save all the registers it uses and restore the values before it returns – By storing them on the stack • At the beginning – Then restoring them • At the end 3/22/2016 week04-3.ppt 16 Simple Example 3/22/2016 week04-3.ppt 17 The Stack Pointer 3/22/2016 week04-3.ppt 18 MIPS Calling Conventions - more • MIPS software divides 18 of the registers into two groups – $t0 - $t9: 10 temporary registers that are not preserved by the callee on a procedure call • These are caller-saved registers since the caller must save the ones it is using – $s0 - $s7: 8 saved registers that must be preserved on a procedure call • These are callee-saved registers since the callee must save the ones it uses 3/22/2016 week04-3.ppt 19 Caller Must Do • Before it calls a procedure/function, it must – Pass parameters • Up to four parameters are passed by $a0 - $a3 – Save caller-saved registers on the stack • It includes $a0 - $a3 (since the callee may change these values), $s0 - $s9, and $ra • Why $ra? – Execute a jal instruction, which jumps to the callee’s first instruction and save the next instruction in $ra 3/22/2016 week04-3.ppt 20 Caller Must Do – cont. • After the procedure/function call, it needs to – Read the returned values from $v0 and $v1 – Restore caller-saved registers 3/22/2016 week04-3.ppt 21 Callee Must Do • Before it does its calculations, the callee must do the following – Allocate memory for its frame by subtracting its frame size from the stack pointer – Save callee-saved registers in the frame • It must save $s0 - $s7, $fp, $ra before changing them • $ra needs to be saved if the callee itself makes a call – When needed, establish the frame pointer $fp by loading $sp to it • In this case, $fp must be saved 3/22/2016 week04-3.ppt 22 Allocating Space for Local Data on Stack • In MIPS, local variables are also stored on the stack – The segment of the stack containing a procedure’s saved registers and local variables is called a procedure frame (or activation record) 3/22/2016 week04-3.ppt 23 Callee Must Do • After its calculations and before it returns to the caller, the callee must – Place the return values in $v0 and $v1 (if needed) – Restore all the callee-saved registers that were saved at the procedure entry – Pop the stack frame by adding the frame size to $sp – Return by jumping to the address in register $ra using jr 3/22/2016 week04-3.ppt 24 Earlier Example 3/22/2016 week04-3.ppt 25 Revised Version 3/22/2016 week04-3.ppt 26