I would like this program to take string and character from the keyboard not lik
ID: 3544552 • Letter: I
Question
I would like this program to take string and character from the keyboard not like I declared it.
# Computer Organization
# A program that counts the occurrences of a specific character in string.
# data segment / input and output
.data
string: .asciiz "I am alive"
character: .asciiz "I"
occurence: .asciiz "Character Occurence is "
endl: .asciiz " "
#text segment
.text
start:
li $t1,0 # $t1 will be the array index
li $t2,0 # $t2 will be the counter
lb $t3,character # and $t3 will hold the charecter
loop: lb $t0,string($t1) # fetch next chararacter
beqz $t0,stringEnd # if it's a null, exit loop
bne $t0,$t3,continue # not null; same as char?
add $t2,$t2,1 # yes,increment counter
continue:add $t1,$t1,1 # increase index
j loop # and continue
stringEnd:
la $a0,occurence # system call to print
li $v0,4 # out a message
syscall
move $a0,$t2 # system call to print
li $v0,1 # out the occurence worked out
syscall
la $a0,endl # system call to print
li $v0,4 # out a newline
syscall
li $v0,10
syscall # bye
Explanation / Answer
please rate - thanks
# Computer Organization
# A program that counts the occurrences of a specific character in string.
# data segment / input and output
.data
string: .space 50
character: .space 4
occurence: .asciiz "Character Occurence is "
endl: .asciiz " "
prompt1: .asciiz "Enter a string: "
prompt2: .asciiz "Enter the character to count:"
#text segment
.text
start:
# prompt for string
la $a0,prompt1
li $v0,4
syscall
#get string
la $a0,string
li $a1,50
li $v0,8
syscall
# prompt for character
la $a0,prompt2
li $v0,4
syscall
#get char
la $a0,character
li $a1,4
li $v0,8
syscall
li $t1,0 # $t1 will be the array index
li $t2,0 # $t2 will be the counter
lb $t3,character # and $t3 will hold the charecter
loop: lb $t0,string($t1) # fetch next chararacter
beqz $t0,stringEnd # if it's a null, exit loop
bne $t0,$t3,continue # not null; same as char?
add $t2,$t2,1 # yes,increment counter
continue:add $t1,$t1,1 # increase index
j loop # and continue
stringEnd:
la $a0,occurence # system call to print
li $v0,4 # out a message
syscall
move $a0,$t2 # system call to print
li $v0,1 # out the occurence worked out
syscall
la $a0,endl # system call to print
li $v0,4 # out a newline
syscall
li $v0,10
syscall # bye