2 Character and String Operations 4/11/2017Character and String OperationsCharacters are encoded as 0’s and 1’s using ASCII most commonlyAmerican Standard Code for Information InterchangeEach character is represented using 8 bits (or a byte)MIPS provides instructions to move bytesLoad byte (lb) loads a byte to the rightmost 8 bits of a registerStore byte (sb) write the rightmost 8 bits of a register to memoryweek04-3.ppt4/11/2017CDA3100 week04-3.ppt
3 SPIM syscalls li $v0,1 # print an integer in $a0 li $a0,100 syscall li $v0,5 # read an integer into $v0li $v0,4 # print an ASCIIZ string at $a0la $a0,msg_helloli $v0,10 #exit
6 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 stackStack can be used in functions to save register values, and is the standard approach to save register values. ButYou can also use stack for other purposesThis is not the only way to save register values.
8 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.
9 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 1 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.
10 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
11 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, -1jal fact
12 The Recursive Function After calling fact, we multiply the return result with n, soaddi $a0, $a0, -1jal factmul $v0, $v0, $a0
13 The Recursive Function After multiplying, we return, soaddi $a0, $a0, -1jal factmul $v0, $v0, $a0jr $ra
14 The Recursive Function So, one if else branch is done. The other branch isslti $t0, $a0, 1beq $t0, $0, L1ori $v0, $0, 1jr $rawhere L1 is where we should go to if n >= 1.
16 The Recursive Function fact:addi $sp, $sp, -4sw $ra, 0($sp)slti $t0, $a0, 1beq $t0, $zero, L1ori $v0, $0, 1lw $ra, 0($sp)addi $sp, $sp, 4jr $raL1:addi $a0, $a0, -1jal factmul $v0, $v0, $a0The problem is that the function will call itself, as we have expected, but it will not return correctly!You need to save $ra, because you made another function call inside the function. You should always do so.Is this enough?
17 The Recursive Function So now you can return to the main function, but the return result is 0, why?Because you did not return to the correct time.Time, here, means what was the parameter you called fact with.So, should also save $a0!
18 The Recursive Function 4/11/2017The Recursive Functionweek04-3.ppt4/11/2017CDA3100 week04-3.ppt
19 The Stack During Recursion 4/11/2017The Stack During Recursionweek04-3.ppt4/11/2017CDA3100 week04-3.ppt
21 Two other MIPS pointers $fp: When you call a C function, the function may declare an array of size 100 like int A. 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.
Your consent to our cookies if you continue to use this website.