In: Computer Science
Write a MIPS assembly program that prompts the user first for a string, then for a character. The program should then search the string for the character and print out the number of times the character appears in the string. You can use as many restrictions as you want, just be sure to list them. You must allocate space in the .data segment of your program for the user string.
Answer. Here is MISP program for the gien problem:
.data
msg1: .asciiz "Enter the string "
msg2: .asciiz "Enter the character "
count: .word 10
string: .space 100
character: .space 4
msg3: .asciiz "character "
msg4: .asciiz "occourred in string "
msg5: .asciiz "times "
.text
.global main
main:
li $t0, 100 # end variable for loop
li $t1, 0 # start variable for loop
li $t2, 0 # to count number of occurrences
la $a0, msg1 # printing the msg1
li $v0, 4
syscall
la $a0, string # taking input string from
user
li $a1, 100
li $v0, 8
syscall
la $a0, msg2 # printing the
msg2
li $v0, 4
syscall
la $a0, character # taking input character
from user
li $a1, 4
li $v0, 8
syscall
la $s0, character
lb $s1, ($s0)
la $t3, string
lb $a2, ($t3)
syscall
loop:
beq $a2, $zero,end # printing the result, if reached end of the
array
beq $a2, $s1, something
addi $t3, $t3, 1 # incrementing char array
lb $a2, ($t3)
j loop
something:
addi $t2, $t1, 1 # incrementing number of occurences of
character
end:
la $a0, msg3
li $v0, 4
syscall
la $a0, character
li $v0, 11
syscall
la $a0, msg4
li $v0, 4
syscall
la $a0, string
li $v0, 4
syscall
li $v0, 1
move $a0, $t2
syscall
la $a0, msg5
li $v0, 4
syscall