MIPS Function Continued Review Function A consecutive piece

  • Slides: 20
Download presentation
MIPS Function Continued

MIPS Function Continued

Review • Function – A consecutive piece of code doing a specific thing –

Review • Function – A consecutive piece of code doing a specific thing – To go to the function, use jal Function, which does two things: go to the code starting at the address associated with label Function, at the same time store the address of the instruction immediately following the jal instruction into $ra. – To return from the function, use jr $ra, which takes the code back to the instruction following the jal instruction. • Stack – A piece of memory – Last in first out – Use $sp to keep track of the first used element on the stack

Character and String Operations • Characters are encoded as 0’s and 1’s using ASCII

Character and String Operations • Characters are encoded as 0’s and 1’s using ASCII most commonly – American Standard Code for Information Interchange – Each character is represented using 8 bits (or a byte) • MIPS provides instructions to move bytes – Load byte (lb) loads a byte to the rightmost 8 bits of a register – Store byte (sb) write the rightmost 8 bits of a register to memory 11/29/2020 week 04 -3. ppt 3

SPIM syscalls li $v 0, 1 li $a 0, 100 syscall # print an

SPIM syscalls li $v 0, 1 li $a 0, 100 syscall # print an integer in $a 0 li $v 0, 5 syscall # read an integer into $v 0 li $v 0, 4 # print an ASCIIZ string at $a 0 la $a 0, msg_hello syscall li $v 0, 10 syscall #exit

String Copy Procedure 11/29/2020 week 04 -3. ppt 5

String Copy Procedure 11/29/2020 week 04 -3. ppt 5

. data msg_hello: . asciiz "Hello! This is CDA 3100!n" msg_empty: . space 400.

. data msg_hello: . asciiz "Hello! This is CDA 3100!n" msg_empty: . space 400. text. globl main: li $v 0, 4 la $a 0, msg_hello syscall li $v 0, 4 la $a 0, msg_empty syscall la $a 0, msg_empty #dst la $a 1, msg_hello #src jal strcpy li $v 0, 4 la $a 0, msg_empty syscall li $v 0, 10 #exit syscall strcpy: ori $t 0, $a 0, 0 ori $t 1, $a 1, 0 strcpyloop: lb $t 3, 0($t 1) sb $t 3, 0($t 0) beq $t 3, $0, strcpydone addi $t 0, 1 addi $t 1, 1 j strcpyloop strcpydone: jr $ra

Implementing a Recursive Function • Suppose we want to implement this in MIPS: •

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.

The Recursive Function • What happens if we call fact(4)? – First time call

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.

The Recursive Function • In MIPS, we say calling a function as going to

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

The Recursive Function • The parameter should be passed in $a 0. In the

The Recursive Function • The parameter should be passed in $a 0. 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 $a 0, -1. ” addi $a 0, -1 jal fact

The Recursive Function • After calling fact, we multiply the return result with n,

The Recursive Function • After calling fact, we multiply the return result with n, so addi $a 0, -1 jal fact mul $v 0, $a 0

The Recursive Function • After multiplying, we return, so addi $a 0, -1 jal

The Recursive Function • After multiplying, we return, so addi $a 0, -1 jal fact mul $v 0, $a 0 jr $ra

The Recursive Function • So, one if else branch is done. The other branch

The Recursive Function • So, one if else branch is done. The other branch is slti $t 0, $a 0, 1 beq $t 0, $0, L 1 ori $v 0, $0, 1 jr $ra where L 1 is where we should go to if n >= 1.

The Recursive Function fact: slti $t 0, $a 0, 1 beq $t 0, $zero,

The Recursive Function fact: slti $t 0, $a 0, 1 beq $t 0, $zero, L 1 ori $v 0, $0, 1 jr $ra L 1: addi $a 0, -1 jal fact mul $v 0, $a 0 jr $ra Any problems?

The Recursivefact: Function • The problem is that the function will call itself, as

The Recursivefact: Function • 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? addi $sp, -4 sw $ra, 0($sp) slti $t 0, $a 0, 1 beq $t 0, $zero, L 1 ori $v 0, $0, 1 lw $ra, 0($sp) addi $sp, 4 jr $ra L 1: addi $a 0, -1 jal fact mul $v 0, $a 0 lw $ra, 0($sp) addi $sp, 4 jr $ra

The Recursive Function • So now we can return to the main function, but

The Recursive Function • So now we can return to the main function, but the return result is 0, why? • Because we did not return to the correct state. • State, here, means what was the parameter we called fact with. • So, should also save $a 0!

The Recursive Function 11/29/2020 week 04 -3. ppt 17

The Recursive Function 11/29/2020 week 04 -3. ppt 17

The Stack During Recursion 11/29/2020 week 04 -3. ppt 18

The Stack During Recursion 11/29/2020 week 04 -3. ppt 18

. data. text. globl main: li $a 0, 4 jal fact done: li $v

. data. text. globl main: li $a 0, 4 jal fact done: li $v 0, 10 syscall fact: addi $sp, -8 sw $ra, 4($sp) sw $a 0, 0($sp) slti $t 0, $a 0, 1 beq $t 0, $zero, L 1 ori $v 0, $0, 1 addi $sp, 8 jr $ra L 1: addi $a 0, -1 jal fact lw $ra, 4($sp) lw $a 0, 0($sp) mul $v 0, $a 0 addi $sp, 8 jr $ra

Two other MIPS pointers • $fp: When you call a C function, the function

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.