I have tried so many different ways to write this program. This is something tha
ID: 3536625 • Letter: I
Question
I have tried so many different ways to write this program. This is something that I tried for reversing a string...however it doesn't really work. Plus I also need to incorporate how to print out the frequency of each character. So, first function needs to read a string from used and print the frequency of each character (including spaces) and second function is to print the reverse of the string.
ex. John Smith
Output:
J 1
o 1
h 2
n 1
S 1
m 1
i 1
t 1
(space) 1
Reversal: htimS nhoJ
this is what i have so far:
.data
prompt: .asciiz " Given String is = "
str: .space 50
newline: .asciiz " "
ans: .asciiz " The String reversed is = "
.text
.globl main
main:
la $a0, prompt #calling opening prompt
li $v0, 4
syscall
li $v0,8
la $a0, str
syscall
la $a0, newline #newline
syscall
la $a0, ans #initial text for reversed string
syscall
li $t2, 0
strLen: #getting length of string
lb $t0, str($t2) #loading value
add $t2, $t2, 1
bne $t0, $zero, strLen
li $v0, 11 #load imediate - print low-level byte
Loop:
sub $t2, $t2, 1 #this statement is now before the 'load address'
la $t0, str($t2) #loading value
lb $a0, ($t0)
syscall
#This is where the sub statement used to be, which caused the loop to terminate too early
bnez $t2, Loop
li $v0, 10 #program done: terminating
syscall
Explanation / Answer
this is working for me in my laptop excet for special characters