MIPS Wrapup Local Variables and Procedure Examples Outline
MIPS Wrap-up Local Variables and Procedure Examples
Outline • • Example MIPS program Example of local variables Class discussion on sort(x, y, z) Review of MIPS
Example MIPS Program. data str: . asciiz “the answer = “. text. globl main: li $v 0, 4 la $a 0, str syscall li $v 0, 1 li $a 0, 5 syscall end: li $v 0, 10 syscall #page 7 #page 8 #page 7 #page 15 #page 17 #page 9 #page 8
System Calls
Local Variables • Local variables are used in procedures to store temporary information – relevant for lifetime of that procedure only • In high level languages, allocation of memory for such local variables is carried out at declaration/initialisation time • When the procedure finishes, the used memory is restored • How? – (also see the book, page 138, also A. 6)
Local Variables in MIPS • In MIPS a temporary variable is implemented by use of a register – this is the only resource to play with! • But a register used in ‘this’ procedure might also be used by ‘that’ procedure • A register used in this procedure might have been used in the main program or calling procedure. • “Housekeeping” is required.
Why Are Stacks So Great? (Patterson) Stacking of Subroutine Calls & Returns and Environments: A: A CALL B B: A B CALL C C: A B C RET A B RET A Each procedure invocation makes use of the stack, and during its lifetime ‘owns’ its part of the stack (‘stack frame’). (Me!)
Use Stack for Local Variables • Just as the stack is used to store return addresses and parameters • It is also used to save and restore the data referenced by registers, – when these are used as temporary store by a procedure, for local variables.
Example (swap) swap(int *x, int *y) /*swaps x and y*/ { int temp; temp = *x; *x = *y; *y = temp; }
Swap in MIPS SWAP: addi $29, -4 sw $16, 0($29) add $16, $0, $5 add $5, $0, $6 add $6, $0, $16 lw $16, 0($29) addi $29, 4 jr $31 #adjust stack pointer for $16 #save $16 on stack (push) #main body follows #temp = x (assume in $5) #x = y (assume in $6) #y = temp #restore stack as follows: #$16 gets its old value back #restore stack pointer (pop) #continue from (1) above
Exercise sort(x, y, z) • Write a MIPS procedure that given 3 ints x, y, z, will sort them in ascending order.
MIPS Review software instruction set hardware (PATTERSON)
(PATTERSON) Instruction Obtain instruction from program storage Fetch Instruction Determine required actions and instruction size Decode Operand Locate and obtain operand data Fetch Execute Result Compute result value or status Deposit results in storage for later use Store Next Instruction Determine successor instruction
(PATTERSON) Register (direct) (R-type) op rs rt rd register Immediate op rs rt immed Base+index (I-type) op rs rt immed register Branch op rs PC rt Memory + immed Memory +
MIPS arithmetic instructions (PATTERSON) Instruction Example Meaning Comments add $1, $2, $3 $1 = $2 + $3 3 operands; exception possible subtract sub $1, $2, $3 $1 = $2 – $3 3 operands; exception possible add immediate addi $1, $2, 100 $1 = $2 + 100 + constant; exception possible add unsigned addu $1, $2, $3 $1 = $2 + $3 3 operands; no exceptions subtract unsigned subu $1, $2, $3 $1 = $2 – $3 3 operands; no exceptions add imm. unsign. addiu $1, $2, 100 $1 = $2 + 100 + constant; no exceptions multiply mult $2, $3 Hi, Lo = $2 x $3 64 -bit signed product multiply unsigned multu$2, $3 Hi, Lo = $2 x $3 64 -bit unsigned product divide div $2, $3 Lo = $2 ÷ $3, Lo = quotient, Hi = remainder Hi = $2 mod $3 divide unsigned divu $2, $3 Lo = $2 ÷ $3, Unsigned quotient & remainder Hi = $2 mod $3 Move from Hi mfhi $1 $1 = Hi Used to get copy of Hi Move from Lo mflo $1 $1 = Lo Used to get copy of Lo
MIPS logical instructions (PATTERSON) Instruction Example Meaning Comment and $1, $2, $3 $1 = $2 & $3 3 reg. operands; Logical AND or or $1, $2, $3 $1 = $2 | $3 3 reg. operands; Logical OR xor $1, $2, $3 $1 = $2 Å $3 3 reg. operands; Logical XOR nor $1, $2, $3 $1 = ~($2 |$3) 3 reg. operands; Logical NOR and immediate andi $1, $2, 10 $1 = $2 & 10 Logical AND reg, constant or immediate ori $1, $2, 10 Logical OR reg, constant xor immediate xori $1, $2, 10 $1 = ~$2 &~10 Logical XOR reg, constant shift left logical sll $1, $2, 10 $1 = $2 << 10 Shift left by constant shift right logical srl $1, $2, 10 $1 = $2 >> 10 Shift right by constant shift right arithm. sra $1, $2, 10 $1 = $2 >> 10 Shift right (sign extend) shift left logical $1 = $2 << $3 Shift left by variable $1 = $2 >> $3 Shift right by variable sllv $1, $2, $3 shift right logical srlv $1, $2, $3 $1 = $2 | 10 shift right arithm. srav $1, $2, $3 $1 = $2 >> $3 Shift right arith. by variable
MIPS data transfer instructions (PATTERSON) Instruction Comment SW 500(R 4), R 3 Store word SH 502(R 2), R 3 Store half SB 41(R 3), R 2 Store byte LW R 1, 30(R 2) Load word LH R 1, 40(R 3) Load halfword LHU R 1, 40(R 3) Load halfword unsigned LB R 1, 40(R 3) Load byte LBU R 1, 40(R 3) Load byte unsigned LUI R 1, 40 Load Upper Immediate (16 bits shifted left by 16)
Compare and Branch (PATTERSON) ° Compare and Branch #see pages 15, 16 of SPIM documentation ° BEQ rs, rt, offset if content(rs) == content(rt) then PC-relative branch ° BNE rs, rt, offset <> ° BLEZ rs, offset if content(rs) <= 0 then PC-relative branch ° BGTZ rs, offset >0 ° BLT < ° BGEZ >=0
MIPS jump, branch, compare instructions (PATTERSON) Instruction Example Meaning branch on equal beq $1, $2, 100 if ($1 == $2) go to PC+4+100 Equal test; PC relative branch on not eq. bne $1, $2, 100 if ($1!= $2) go to PC+4+100 Not equal test; PC relative set on less than slt $1, $2, $3 if ($2 < $3) $1=1; else $1=0 Compare less than; 2’s comp. set less than imm. slti $1, $2, 100 if ($2 < 100) $1=1; else $1=0 Compare < constant; 2’s comp. set less than uns. sltu $1, $2, $3 if ($2 < $3) $1=1; else $1=0 Compare less than; natural no. set l. t. imm. uns. sltiu $1, $2, 100 if ($2 < 100) $1=1; else $1=0 Compare < constant; natural jump j 10000 go to 10000 Jump to target address jump register jr $31 go to $31 For switch, procedure return jump and link jal 10000 $31 = PC + 4; go to 10000 For procedure call
Next Time. . . • Back to basics – binary representations – integer numbers – negative numbers – arithmetic – floating point numbers
- Slides: 20