1 / 13

Procedures 2a MIPS code examples

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:

telma
Download Presentation

Procedures 2a MIPS code examples

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Procedures 2aMIPS code examples Making Use of a Stack

  2. Outline • Assumptions for example • Implementation: sprod/prod/add2 • Example: factorial

  3. 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

  4. Strategy for example: • First examine code for simple procedures: • Prod • Add2 • Then examine code for more complex procedure: • sprod

  5. 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

  6. 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

  7. 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 …

  8. 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

  9. Example Factorial • n! = n(n-1)...21 4! = 4321 = 24 4! = 4(321) = 43! 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); }

  10. 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.

  11. 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

  12. 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)

  13. Next Time • How to handle arguments to procedures • How to write programs for the MIPS simulator SPIM • Answering your questions about the Coursework.

More Related