Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MIPS write a program that prompts user for a string, then for a character. The p

ID: 3587873 • Letter: M

Question

MIPS

write a program that prompts user for a string, then for a character. The program will search the string for the character and print out the number of times the character appears in the string. There might be restrictions on this but just list them, you dont have to make any test cases. For example you can set a max size for the first input string but if something is entered thats longer just make a comment that it may not work if longer than x size. allocate space for the .data segment, use the .space directive in the .data segment to allocate space for example: ustring: .space 30

use syscalls 1,4,8, and 12 on this.

should print out ; number of characters found = x

Explanation / Answer

Given below is the code with output. Please don't forget to rate the answer if it helped. Thank you.


.data
prompt1: .asciiz "Enter a string (max 50 chars): "
prompt2: .asciiz "Enter a character: "
msg : .asciiz " No. of times character appears = "
ustring: .space 50
.text
#prompt and get string input
li $v0, 4
la $a0, prompt1
syscall
li $v0, 8
la $a0, ustring
li $a1, 50
syscall
#prompt and get the character
li $v0, 4
la $a0, prompt2
syscall
li $v0, 12
syscall
move $t0, $v0 #store the character in t0
#iniitialize counter $t1
li $t1, 0 #counter
la $t2, ustring #address of string in t2
loop:
lb $t3, ($t2) #get current character
beq $t3, '', finish #if end of string , go to finish
bne $t3, $t0 , next #compare current char t3 with needed char in t0, if not equal go to next
#they are equal , incrment count
addi $t1, $t1, 1
next:
add $t2, $t2, 1 #next address
b loop
finish:
#display the count  
li $v0, 4
la $a0, msg
syscall
li $v0, 1
move $a0, $t1
syscall
#exit
li $v0, 10
syscall

output

Enter a string (max 50 chars): hello every body! where are you
Enter a character: e
No. of times character appears = 6
-- program is finished running --

Enter a string (max 50 chars): hello
Enter a character: t
No. of times character appears = 0
-- program is finished running --

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote