In MIPS code and should be able to run on MARS 4.5, create a program that finds
ID: 3911636 • Letter: I
Question
In MIPS code and should be able to run on MARS 4.5, create a program that finds the number of times the letters K, N, I, G, H, T, and S are used in a sentence that the user inputs. YOU MUST SHOW A SCREENSHOT OF THE OUTPUT AND MUST RUN ON MARS 4.5
Sample Input by user:
Hello, the cat jumped over the kitten and walked down the road to grab some food. The dog ran across the feild and fetched the ball. The Gardner mowed the lawn.
Sample Ouput should be in the following format:
K: # of times said
N:# of times said
I:# of times said
G: # of times
H: # of times
T: # of times
S: # of times
Explanation / Answer
Screenshot
----------------------------------------------------------------------------------------
Program
#Variable declaration section
.data
#space for reading string
input: .space 200
#prompt
prompt: .ascii "Please enter a sentence(character limit is 200):"
#display each character seperately
K: .asciiz "K: "
N: .asciiz "N: "
I: .asciiz "I: "
G: .asciiz "G: "
H: .asciiz "H: "
T: .asciiz "T: "
S: .asciiz "S: "
#times display
timesSaid: .asciiz " of times said "
times: .ascii " of times "
#main program
.text
.globl main
main:
#User prompt
la $a0,prompt
li $v0,4
syscall
#reas user enter sentence
li $v0,8
la $a0,input
li $a1,200
syscall
#counter variables
li $t1,0 #for k count
li $t2,0 #for N count
li $t3,0 #for I count
li $t4,0 #for G count
li $t5,0 #for H count
li $t6,0 #for T count
li $t7,0 #for S count
#loop for each given variable count
loop:
#until space finish
beq $a1,0,print
#each character
lb $t0,0($a0)
#check character is 'K'
beq $t0,75,KCount
#check character is 'N'
beq $t0,78,NCount
#check character is 'I'
beq $t0,73,ICount
#check character is 'G'
beq $t0,71,GCount
#check character is 'H'
beq $t0,72,HCount
#check character is 'T'
beq $t0,84,TCount
#check character is 'S'
beq $t0,83,SCount
#increment to get next character
addi $a0,$a0,1
#decrement counter
addi $a1,$a1,-1
#jump
j loop
#Increment K count
KCount:
addi $t1,$t1,1
addi $a0,$a0,1
addi $a1,$a1,-1
j loop
#Increment N count
NCount:
addi $t2,$t2,1
addi $a0,$a0,1
addi $a1,$a1,-1
j loop
#Increment I count
ICount:
addi $t3,$t3,1
addi $a0,$a0,1
addi $a1,$a1,-1
j loop
#Increment G count
GCount:
addi $t4,$t4,1
addi $a0,$a0,1
addi $a1,$a1,-1
j loop
#Increment H count
HCount:
addi $t5,$t5,1
addi $a0,$a0,1
addi $a1,$a1,-1
j loop
#Increment T count
TCount:
addi $t6,$t6,1
addi $a0,$a0,1
addi $a1,$a1,-1
j loop
#Increment S count
SCount:
addi $t7,$t7,1
addi $a0,$a0,1
addi $a1,$a1,-1
j loop
#print each count
print:
#K prompt
la $a0,K
li $v0,4
syscall
#count display
move $a0,$t1
li $v0,1
syscall
#times display
la $a0,timesSaid
li $v0,4
syscall
#N prompt
la $a0,N
li $v0,4
syscall
#count display
move $a0,$t2
li $v0,1
syscall
#times display
la $a0,timesSaid
li $v0,4
syscall
#I prompt
la $a0,I
li $v0,4
syscall
#count display
move $a0,$t3
li $v0,1
syscall
#times display
la $a0,timesSaid
li $v0,4
syscall
#G prompt
la $a0,G
li $v0,4
syscall
#count display
move $a0,$t4
li $v0,1
syscall
#times display
la $a0,times
li $v0,4
syscall
#H prompt
la $a0,H
li $v0,4
syscall
#count display
move $a0,$t5
li $v0,1
syscall
#times display
la $a0,times
li $v0,4
syscall
#T prompt
la $a0,T
li $v0,4
syscall
#count display
move $a0,$t6
li $v0,1
syscall
#times display
la $a0,times
li $v0,4
syscall
#S prompt
la $a0,S
li $v0,4
syscall
#count display
move $a0,$t7
li $v0,1
syscall
#times display
la $a0,times
li $v0,4
syscall
#end of the program
exit:
li $v0,10
syscall
---------------------------------------------------------
Output
Please enter a sentence(character limit is 200): Hello, the cat jumped over the kitten and walked down the road to grab some food. The dog ran across the feild and fetched the ball. The Gardner mowed the lawn.
K: 0 of times said
N: 0 of times said
I: 0 of times said
G: 1 of times
H: 1 of times
T: 2 of times
S: 0 of times
-- program is finished running --
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.