130 likes | 315 Views
Procedures 2a MIPS code examples. Making Use of a Stack. Outline. Assumptions for example Implementation: sprod/prod/add2 Example: factorial. Assumptions for Example. We suppose the following: x1,y1,x2,y2 stored in memory starting at location X Procedures:
E N D
Procedures 2aMIPS code examples Making Use of a Stack
Outline • Assumptions for example • Implementation: sprod/prod/add2 • Example: factorial
Assumptions for Example • We suppose the following: • x1,y1,x2,y2 stored in memory starting at location X • Procedures: • add2 - adds together $4, $5 puts result in $6 • prod - multiplies $4, $5 puts result in $6 • sprod – (x1 * y1) + (x2 * y2), puts result in $8 • $29 is the stack pointer (address of ‘top’ of stack) • The stack in MIPS grows downwards • so that subtracting 4 from $29 makes room for a push • and adding 4 to $29 adjusts for a pop
Strategy for example: • First examine code for simple procedures: • Prod • Add2 • Then examine code for more complex procedure: • sprod
Prod prod : addi $29,$29,-4 #step 1 of push sw $31,0($29) #step 2 of push $31 mult $6,$4,$5 lw $31,0($29) #step 1 of pop: # restore return address addi $29,$29,4 #step 2 of pop: # adjust top of stack jr $31 #return
add2 add2 : addi $29,$29,-4 #step 1 of push sw $31,0($29) #step 2 of push $31 add $6,$4,$5 lw $31,0($29) #step 1 of pop: # restore return address addi $29,$29,4 #adjust top of stack jr $31 #return
Implementation of sprod sprod : lw $4,X($0) #x1 into $4 li $5, 4 lw $5,X($5) #y1 into $5 addi $29,$29,-4 #step 1 for push sw $31,0($29) #step 2 for push $31 #return address now saved, #to be popped at end sprod jal prod #call prod, result in $6 add $20,$0,$6 #copy result into $20 li $4, 8 lw $4,X($4) #x2 into $4 li $5, 12 lw $5,X($5) #y2 into $5 jal prod #call prod, result in $6 # continued …
sprod continued add $4,$0,$20 #first result into $4 add $5,$0,$6 #second result into $5 jal add2 #puts result in $6 lw $31,0($29) #step 1 of pop: # restores return address addi $29,$29,4 #step 2 of pop: # adjust ‘top’ of stack jr $31 #return to caller of sprod
Example Factorial • n! = n(n-1)...21 4! = 4321 = 24 4! = 4(321) = 43! n! = n(n-1)! (unless n=1, in which case n!=1) • Function to implement factorial: fact(n) { if n==1 then return 1 else return n*fact(n-1); }
Factorial in MIPS (Assumptions) • The argument (n) is in $4 • $8 is already initialised to 1 • $8 will hold the result • We will use the pretend instructions • push • pop • print • for sake of simplicity.
Nearly a MIPS representation MAIN : li $4,4 #the argument n = 4 li $11,1 # set $11 to have the value 1 li $8,1 # initialize $8 to have the value 1 jal fact #call “fact” procedure print $8 #no such instruction! exit #no such instruction - halt altogether fact: beq $4,$11,RETURN #if n==1 goto RETURN push($31) #save on stack push($4) #save argument addi $4,$4,-1 #n becomes n-1 jal fact #recursive call to fact RETURN: $4 = pop() #no such instruction! $31 = pop() mult $8,$8,$4 #$8 = $8*$4 jr $31 #return
Summary • jal • jump and link • saves return address in $31 • jr $31 • jumps to return address in register $31 • Stack pointer used to store nested return addresses ($29)
Next Time • How to handle arguments to procedures • How to write programs for the MIPS simulator SPIM • Answering your questions about the Coursework.