MIPS program to count how many times a character occurs in a string The string s
ID: 3544168 • Letter: M
Question
MIPS program to count how many times a character occurs in a string
The string should be less than 60 characters.
Use a function to determine how many times a character occurs in a string
The function should get the string and character from the user
Call your output function
Output the result
For example :
If string = " More MIPS Madness is fun, fun, fun!
Character count is 'M'
Output= Occurences of 'M' : 3
Also return indices of the first and last occurences of the character in a string.
Assuming that the index of the first character is 0.
string = " More MIPS Madness is fun, fun, fun!
Character count is 'M'
output
Occurrences of
Explanation / Answer
# Get the string from user li $v0, 4 # Load 4=print_string into $v0 la $a0, prompt # Load address of prompt into $a0 syscall # Output the prompt la $a0, prompt # Load address of prompt into $a0 li $v0, 8 # Load 8=read_string into $v0 la $a0, str # $a0=address of str li $a1, 1024 # $a1= max str length syscall # read string # Compute string length la $s0, str # $s0 contains base address of str add $s2, $0, $0 # $s2 = 0 addi $s3, $0, ' ' # $s2 = ' ' loop: lb $s1, 0($s0) # load character into $s0 beq $s1, $s3, end # Break if byte is newline addi $s2, $s2, 1 # increment counter addi $s0, $s0, 1 # increment str address j loop end: sb $0, 0($s0) #replace newline with 0 # Output li $v0, 4 # Load 4=print_string into $v0 la $a0, r1 # Load address of newline into $a0 syscall # Output the newline li $v0, 4 # Load 4=print_string into $v0 la $a0, str # Load address of newline into $a0 syscall # Output the newline li $v0, 4 # Load 4=print_string into $v0 la $a0, r2 # Load address of newline into $a0 syscall # Output the newline li $v0, 1 # Load 1=print_int into $v0 add $a0, $s2, $0 # Load first number into $a0 syscall # Output the prompt via syscall li $v0, 4 # Load 4=print_string into $v0 la $a0, r3 # Load address of newline into $a0 syscall # Output the newline # Exit Gracefully li $v0, 10 syscall .data prompt: .asciiz "Enter a string of characters: " str: .space 1024 # Allocate 1024 bytes for the input string r1: .asciiz "The string "" r2: .asciiz "" has " r3: .asciiz " characters." newline: .asciiz " "
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.