1 / 18

MIPS function continued

MIPS function continued. Stack. Key things to keep in mind: Stack is a software concept – last in first out, that’s it. In MIPS, you implement the stack by yourself by keeping $sp always pointing to the top element on the stack

Télécharger la présentation

MIPS function continued

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. MIPS function continued

  2. Stack • Key things to keep in mind: • Stack is a software concept – last in first out, that’s it. • In MIPS, you implement the stack by yourself by keeping $sp always pointing to the top element on the stack • Stack can be used in functions to save register values, and is the standard approach to save register values. But • You can also use stack for other purposes • This is not the only way to save register values.

  3. .data msg: .asciiz "hello world" endl: .asciiz "\n" .text .globl main main: addi $sp,$sp,-1 sb $0,0($sp) la $t1, msg L0: lb $t0,0($t1) beq $t0,$0, L1 addi $sp,$sp,-1 sb $t0,0($sp) addi $t1,$t1,1 j L0 L1: la $t1,msg L2: lb $t0,0($sp) addi $sp,$sp,1 sb $t0,0($t1) beq $t0, $0, L3 addi $t1,$t1,1 j L2 L3: la $a0,msg li $v0,4 syscall la $a0,endl li $v0,4 syscall li $v0,10 #exit syscall

  4. Implementing a Recursive Function • Suppose we want to implement this in MIPS: • It is a recursive function – a function that calls itself. • It will keep on calling itself, with different parameters, until a terminating condition is met.

  5. The Recursive Function • What happens if we call fact(4)? • First time call fact, compare 4 with 1, no less than 1, call fact again – fact(3). • Second time call fact, compare 3 with 1, no less than 1, call fact again – fact(2). • Third time call fact, compare 2 with 1, no less than 1, call fact again – fact(1). • Fourth time call fact, compare 1 with 1, no less than 1, call fact again – fact(0). • Fifth time call fact, compare 0 with 1, less than 1, return 1. • Return to the time when fact(0) was called (when calling fact(1)). Multiply 1 with 1, return 1. • Return to the time when fact(1) was called (when calling fact(2)). Multiply 2 with 1, return 2. • Return to the time when fact(2) was called (when calling fact(3)). Multiply 3 with 2, return 6. • Return to the time when fact(3) was called (when calling fact(4)). Multiply 4 with 6, return 24.

  6. The Recursive Function • In MIPS, we say calling a function as going to the function. So we go to the function over and over again, until the terminating condition is met. • Here, the function is called “fact,” so we will have a line of code inside the fact function: jal fact

  7. The Recursive Function • The parameter should be passed in $a0. In the C function, every time we call fact, we call with n-1. So, in the MIPS function, before we do “jal fact”, we should have “addi $a0, $a0,-1.” addi $a0, $a0, -1 jal fact

  8. The Recursive Function • After calling fact, we multiply the return result with n, so addi $a0, $a0, -1 jal fact mul $v0, $v0, $a0

  9. The Recursive Function • After multiplying, we return, so addi $a0, $a0, -1 jal fact mul $v0, $v0, $a0 jr $ra

  10. The Recursive Function • So, one if else branch is done. The other branch is slti $t0, $a0, 1 beq $t0, $0, L1 ori $v0, $0, 1 jr $ra where L1 is where we should go to if n >= 1.

  11. The Recursive Function fact: slti $t0, $a0, 1 beq $t0, $zero, L1 ori $v0, $0, 1 jr $ra L1: addi $a0, $a0, -1 jal fact mul $v0, $v0, $a0 jr $ra Any problems?

  12. The Recursive Function fact: addi $sp, $sp, -4 sw $ra, 0($sp) slti $t0, $a0, 1 beq $t0, $zero, L1 ori $v0, $0, 1 lw $ra, 0($sp) addi $sp, $sp, 4 jr $ra L1: addi $a0, $a0, -1 jal fact mul $v0, $v0, $a0 lw $ra, 0($sp) addi $sp, $sp, 4 jr $ra • The problem is that the function will call itself, as we have expected, but it will not return correctly! • We need to save $ra, because we made another function call inside the function. We should always do so. • Is this enough?

  13. The Recursive Function • So now we can return to the main function, but the return result is 0, why? • A call to fact modifies $a0. But when we return from a call, we multiply it with $a0! • So, should also save $a0! • Restore it before using it again.

  14. The Recursive Function week04-3.ppt

  15. The Stack During Recursion week04-3.ppt

  16. .data .text .globl main main: li $a0, 4 jal fact done: li $v0,10 syscall fact: addi $sp, $sp, -8 sw $ra, 4($sp) sw $a0, 0($sp) slti $t0, $a0, 1 beq $t0, $zero, L1 ori $v0, $0, 1 addi $sp, $sp, 8 jr $ra L1: addi $a0, $a0, -1 jal fact lw $ra, 4($sp) lw $a0, 0($sp) mul $v0, $v0, $a0 addi $sp, $sp, 8 jr $ra

  17. Two other MIPS pointers • $fp: When you call a C function, the function may declare an array of size 100 like int A[100]. It is on the stack. You would want to access it, but the stack pointer may keep changing, so you need a fixed reference. $fp is the “frame pointer,” which should always point to the first word that is used by this function. • $gp: the “global pointer.” A reference to access the static data.

  18. Solution to the in class exercise .data A: .word 0,1,2,3,4,5,6,7,8,9 .text .globl main main: la $a0,A li $a1,6 li $a2,5 jal onUpStream done: li $v0, 10 # exit syscall onUpStream: onUpStream_LOOP: lw $t0, 0($a0) beq $t0, $a1, onUpStream_NO beq $t0, $a2, onUpStream_YES addi $a0, $a0, 4 j onUpStream_LOOP onUpStream_YES: ori $v0, $0, 1 j onUpStream_DONE onUpStream_NO: ori $v0, $0, 0 onUpStream_DONE: jr $ra

More Related