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

IN MIPS Write an assembly program that prompts the user first for a string, then

ID: 3599928 • Letter: I

Question

IN MIPS Write an 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 must allocate space in the .data segment of your program for the user string. Your program should prompt the user for an input string and a search character. Use SYSCALLs #8 and #12 to do this. Your output should be a message like “# of characters found = 5”.

Appendix B - SYSCALL Functions In MARS Service Code in v0 uments Result print integer print float print double $a0 = integer to print $f12 float to print $f12 = double to print $a0 = address of null-terminated string to print print string 4 read integer read float read double SvO contains integer read $f0 contains float read fo contains double read 6 a0 address of input buffer 8 Sa1 maximum number of characters to read read string See note below table $v0 contains address of allocated sbrk (allocate heap memory) exit terminate execution print character read character 9 a0 number of bytes to allocate mem 10 |$a0 = character to print 11 12 See note below table Sv0 contains character read a0 address of null-terminated string containing filename Sa1 flags $a2 = mode Sa0 file descriptor a1- address of input buffer Sa2 maximum number of characters to read a0 file descriptor $al- address of output buffer Sa2 number of characters to write | $a0 = file descriptor SvO contains file descriptor (negative if error). See note below table open file 13 SvO contains number of characters read (0 if end-of-file, negative if error). See note below table read from file 14 vO contains number of characters written (negative if error). See note below table rite to file 15 close file 16

Explanation / Answer

MIPS assembly program:

----------------------------------

.data
promtMsg1: .asciiz "Enter a string "
promtMsg2: .asciiz "Enter a character "
count: .word 10
string: .space 50
character: .space 4
output: .asciiz "# of character found = "

main:
li $t0, 50 #end var for loop
li $t1, 0 #start var for loop
li $t2, 0 #number of occurences

la $a0, promtMsg1 #print 'enter string'
li $v0, 4
syscall


la $a0, string #input string
li $a1, 50
li $v0, 8
syscall

la $a0, promtMsg2 #print 'Enter char'
li $v0, 4
syscall

la $a0, character #input character
li $a1, 4
li $v0, 8
syscall

la $s0, character
lb $s1, ($s0)
la $t3, string
lb $a2, ($t3) #gets first char of string

loop:
beq $a2, $zero, end #once reach end of char array, prints result
beq $a2, $s1, something #if the char within string == comparing char
addi $t3, $t3, 1 #increment char array
lb $a2,($t3)
j loop

something:   
addi $t2, $t2, 1 #increments number of occurences of char
end:
la $a0, output
li $v0, 4
syscall

la $a0, character
li $v0, 11
syscall