MIPS Assembly programming. Not C or anything else Write a program that reads a s
ID: 3854694 • Letter: M
Question
MIPS Assembly programming. Not C or anything else
Write a program that reads a string (from a keyboard), stores it in the memory, and computes and prints the frequency of each upper case, each lowercase, and the space character.
I have coded most of it I just need someone to tell me what is wrong and how to fix it
.data
prompt: .asciiz "Enter a string: "
arrayInput: .space 100
arrLowerCase: .word 0:26
arrUpperCase: .word 0:26
msg: .asciiz "The letter "
msg1: .asciiz " has a frequency of "
newLine: .asciiz " "
space: .word 0:10
.text
main: la $a0, prompt
li $v0, 4
syscall
la $a0, arrayInput
li $a1, 20
li $v0, 8
syscall
la $a0, arrayInput
jal compute
la $a0, arrLowerCase
li $a1, 0
jal print
li $v0, 10
syscall
print: move $t0, $a0
printA: lw $t1, 0($t0)
beqz $t1, dont
beq $a1, 26, endme
add $t0, $t0, 4
add $a1, $a1, 1
la $a0, msg
li $v0, 4
syscall
li $t2, 0
add $t2, $t1, 97
move $a0, $t2
li $v0, 11
syscall
la $a0, msg1
li $v0, 4
syscall
move $a0, $t1
li $v0, 1
syscall
la $a0, newLine
li $v0, 4
syscall
dont: add $t0, $t0, 4
beq $a1, 26, endme
add $a1, $a1, 1
b printA
endme: jr $ra
compute: lb $t0, 0($a0)
add $a0, $a0, 1
beqz $t0, endy
beq $t0, 13, endy
bge $t0, 97, lower
bge $t0, 65, upper
beq $t0, 32 spacy
b compute
spacy: li $t1, 1
li $t3, 0
li $t4, 0
lw $t2, space($t4)
add $t3, $t1, $t2
sw $t3, arrLowerCase($t4)
b compute
lower: ble $t0, 122, contL
b compute
contL: add $t0, $t0, -97
mul $t5, $t0, 4
li $t1, 1
li $t3, 0
lw $t2, arrLowerCase($t5)
add $t3, $t1, $t2
sw $t3, arrLowerCase($t5)
b compute
upper: ble $t0, 90, contU
b compute
contU: add $t0, $t0, -65
mul $t5, $t0, 4
li $t1, 1
li $t3, 0
lw $t2, arrUpperCase($t5)
add $t3, $t1, $t2
sw $t3, arrUpperCase($t5)
b compute
endy: jr $ra
Explanation / Answer
Below is the fixed code with output. Changes are in bold. It now prints frequencey of upper case as well space. In print routine , intead of adding the to $a1, you were adding 97 to $t1 which was wrong. Also since print should handle both lower case and upper case, the value to be added will be stored in $a3. (either 97 or 65) before print is called.
Also in spacy, instead of storing the count back to space, you were saving it arrLowerCase , which is incorrect. Fixed that.
Changes are shown in bold.
Please post a comment in case of any issues. If happy with the answer, please rate it . Thank you.
.data
prompt: .asciiz "Enter a string: "
arrayInput: .space 100
arrLowerCase: .word 0:26
arrUpperCase: .word 0:26
msg: .asciiz "The letter "
msg1: .asciiz " has a frequency of "
msg2: .asciiz "Space has a frequency of "
newLine: .asciiz " "
space: .word 0:10
.text
main: la $a0, prompt
li $v0, 4
syscall
la $a0, arrayInput
li $a1, 20
li $v0, 8
syscall
la $a0, arrayInput
jal compute
la $a0, arrLowerCase
li $a1, 0
li $a3, 97
jal print
la $a0, arrUpperCase
li $a1, 0
li $a3, 65
jal print
la $a0, msg2
li $v0, 4
syscall
lw $a0, space
li $v0, 1
syscall
la $a0, newLine
li $v0, 4
syscall
li $v0, 10
syscall
print: move $t0, $a0
printA: lw $t1, 0($t0)
beqz $t1, dont
beq $a1, 26, endme
la $a0, msg
li $v0, 4
syscall
li $t2, 0
add $t2, $a1, $a3
move $a0, $t2
li $v0, 11
syscall
la $a0, msg1
li $v0, 4
syscall
move $a0, $t1
li $v0, 1
syscall
la $a0, newLine
li $v0, 4
syscall
add $t0, $t0, 4
add $a1, $a1, 1
b printA
dont: add $t0, $t0, 4
beq $a1, 26, endme
add $a1, $a1, 1
b printA
endme: jr $ra
compute: lb $t0, 0($a0)
add $a0, $a0, 1
beqz $t0, endy
beq $t0, 13, endy
bge $t0, 97, lower
bge $t0, 65, upper
beq $t0, 32 spacy
b compute
spacy: li $t1, 1
li $t3, 0
li $t4, 0
lw $t2, space($t4)
add $t3, $t1, $t2
sw $t3, space($t4)
b compute
lower: ble $t0, 122, contL
b compute
contL: add $t0, $t0, -97
mul $t5, $t0, 4
li $t1, 1
li $t3, 0
lw $t2, arrLowerCase($t5)
add $t3, $t1, $t2
sw $t3, arrLowerCase($t5)
b compute
upper: ble $t0, 90, contU
b compute
contU: add $t0, $t0, -65
mul $t5, $t0, 4
li $t1, 1
li $t3, 0
lw $t2, arrUpperCase($t5)
add $t3, $t1, $t2
sw $t3, arrUpperCase($t5)
b compute
endy: jr $ra
output
Enter a string: Good MorninG
The letter d has a frequency of 1
The letter i has a frequency of 1
The letter n has a frequency of 2
The letter o has a frequency of 3
The letter r has a frequency of 1
The letter G has a frequency of 2
The letter M has a frequency of 1
Space has a frequency of 1
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.