Name (10 pts) The function countChars char·str char·target) returns the number o
ID: 3731265 • Letter: N
Question
Name (10 pts) The function countChars char·str char·target) returns the number of times characters in target appear in str. For example: int countChars(char str, char target) countChars( abc abc abc", "a") 3 countChars("abc abc abc", "ab") 6 countChars("abe abc abc", "abcd")-9 Int count 0; while ("str) ( count +a isCharinStr(target, "str) str++ The C code for countChars is given to you in the box on right. The helper function isCharinStr(char *target, char c) returns 1 if c is present in target and O if not. return count; Finish the implement of countChars in MIPS below. countchars addiu $sp, sp, store onto the stack if needed addiu $so, $ zero, o # we'll store the count in Ss0 addiu $si, $ao, o addiu $82, $al, o loop: addiu $ao, $s2, 0 beq jal isCharInstr done: # Load from the stack if needed addiu $sp, $sp dr $raExplanation / Answer
addiu $sp, $sp, -16
sw $ra, 0($sp) #store on stack
sw $s0, 4($sp)
sw $s1, 8($sp)
sw $s2, 12($sp)
addiu $s0, $zero, 0 #count = 0
addiu $s1, $a0, 0
addiu $s2, $a1, 0
loop:
addiu $a0, $s2, 0 #set the 1st parameter to make function call to isCharInStr()
lb $a1, ($s1) #set the 2nd parameter to current char at location pointed by $s1
beq $a1, '', done #end of string ? go to done
jal isCharInStr #call the function with the two arguments set above
add $s0, $s0, $v0 #add the return value from function
addiu $s1, $s1, 1 #next location
b loop
done:
lw $ra, 0($sp) #restore from stack
lw $s0, 4($sp)
lw $s1, 8($sp)
lw $s2, 12($sp)
addiu $sp, $sp, 16
jr $ra
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.