Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # CHAR_COUNT FUNCTION
- #
- .globl charCount
- charCount:
- #a0 = word (word[0])
- #a1 = search (char)
- #s1 = result
- #main wants to get return value from a0 and store in s1
- #what will return value be?
- # *************** CHARCOUNT CALLEE SETUP **********************
- # int result;
- #addi sp, sp, -16 #Make stack 16 byte aligned
- #sw ra, 12(sp) #Save caller's return address
- #sw s0, 8(sp) #Save caller's frame pointer
- #addi s0, sp, 16 #Set frame pointer to first arg
- #a0 = word[0]
- #a1 = char we're searching for
- #t1 = value of word[0], first char in word
- #t0 == 0. Set to 1 if char found
- addi sp, sp, -16 #Make stack 16 byte aligned, create stack space
- sw ra, 12(sp) #store return adress in stack pointer
- sw s0, 8(sp) #store frame pointer
- sw s1, 4(sp) #store local var into stack (result)
- addi s0, sp, 12 #set frame pointer to first element
- #ot sure if these lines inc
- andi s1, s1, 0 #Set s1 to 1, (it will be set to 1 if char is found)?
- lb t1, (a0) #a0 contains word[0]. load t1 with byte value at word[0] - first char in arr
- # *************** CHAR_COUNT CODE BODY ************************
- #load mem address of str[0] (&str[0]) into register
- #load value (actual character) into register
- #branch if 0
- # if (*str == 0)
- #caller is result += Charcount
- # result = 0
- # else
- # {
- # if (*str == c)
- # result = 1;
- # else
- # result = 0;
- #base case
- beqz t1 null_char #branch is *str == 0 (null char) TODO: store result before jump
- bne t1, a1, not_null_char #else (below if *str == c)
- addi s1, s1, 1 #if this executes, *str == c. add 1 to t0 (char found)
- b not_null_char
- null_char:
- andi a0, a0, 0 #Clear a0, since it is an address.
- #We need to use a0 as the result of occurances
- sw s1, 4(sp)
- b return
- # *************** CHARCOUNT CALLER SETUP ***********************
- not_null_char: #This is the
- sw s1, 4(sp) #Store result in stack
- addi a0, a0, 1 #Increment array by 1
- jal charCount #Repeat with new char in arr
- # result += charCount(str+1, c);
- # *************** CHARCOUNT CALLER TEARDOWN ********************
- # *************** RESUME CHARCOUNT *****************************
- # }
- # return result
- #}
- # *************** CHARCOUNT CALLEE TEARDOWN *****************************
- return:
- lw ra, 12(sp) #Set return address to stack pointer
- lw s1, 4(sp) #Load t0 with result (0 or 1)
- add a0, a0, s1 #Add result (0 or 1) to a0
- #Every time we return, result goes in a0
- addi sp, sp, 16 #Pop stack (Remove space created)
- ret
- #After callee teardown, stack should look like it did before caller setup
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement