I have to write a program that takes a string input, prints the frequency of eac
ID: 3536624 • Letter: I
Question
I have to write a program that takes a string input, prints the frequency of each character and also prints the string in reverse. I think I got how to print the frequency, but I dont know how to add to this to prin the reversal. Please please help me, I'm going crazy staring at the screen and all this mips programming is giving me a headache. Here is what I have so far:
.data
welcome_msg: .asciiz "Enter text, followed by $: "
.globl main
frequency: .space 104 # This is the String where we keep how many times each letter #occurs. The space left
# of space so 26*4=104
.text
main:
li $s0, 90 # Load the code for ANSII Z. We will need that when telling the program to use different
# cycles for lowercase and uppercase letters since they have different ANSII codes.
li $s1, 36 # Store the $ character to detect string end
li $s2, 32 # Store ANSII space
li $s3, 9 # Store ANSII tab
li $s4, 10 # Store ANSII new line feed
li $s5, 15 # Store ANSII carriage return
li $s6, 58 # Store ANSII Colon used for formatting string
li $v0, 4 # Load code for printing string.
la $a0, welcome_msg # Print the welcome message
syscall
input:
li $v0,12 # Load the syscode for reading a char.
syscall # Read a char from the command line.
beq $v0, $s1, print_prep # Jump to printing preparation if we encounter the end of the string:
beq $v0, $s2, input # Jump to input in case of space
beq $v0, $s3, input # Jump to input in case of tab
beq $v0, $s4, input # Jump to input in case of new line feed
beq $v0, $s5, input # Jump to input in case of carriage return
bgt $v0, $s0, lowercase # If we encouter a lowercase letter give it to lowercase to process
addi $v0, -65 # Get the input symbols so that A=0, B=1, etc...)
array_op:
sll $v0, $v0, 2 # Multiplies $v0 to 4 so that we get the necessary position
la $t0, frequency # Load the array in $t0
add $t0, $t0, $v0 # Get the counter to point to the right position of current letter
lw $t1, 0($t0) # Load the current number count for the letter needed in $t1
addi $t1, 1 # Increment the counter for the particular letter
sw $t1, frequency($v0) # Store back to the array
j input # loop back to process the next input symbol.
lowercase:
addi $v0, -97 # Get the input symbols so that a=0, b=1, etc...
j array_op # Jump to the part of the code that actually works with the array.
print_prep:
li $s0, 64 # We'll use $s0 to print the letters A, B... in the loop. The ANSII character for A is 65 so
# we Load the previous number so that we can do incremet in the actual loop.
li $s1, 0 # go through the array and get how many times each letter occured by using this variable.
li $s3, 25 # This variable contains the number of English letters -1
li $v0, 11 # Load code for printing char.
move $a0, $s4 # Print new line to get good formatting.
syscall
print_loop:
addi $s0, 1 # Increment the letter printing variable by one
move $a0, $s0 # Move to the printing address
syscall
move $a0, $s6 # Move the colon symbol to the printing address
syscall
move $a0, $s2 # Move space to the printing address
syscall
sll $t0, $s1, 2 # Multiply the array variable by four so that we get to the desired element of the array
lw $a0, frequency($t0) # Load the integer value from the desired position in the array to the printing address.
li $v0, 1 # Load print integer code and print.
syscall
li $v0, 11 # Load code for printing char.
move $a0, $s4 # Print new line to get good formatting.
syscall
beq $s3, $s1 end # Check if we've reached the end element of the array.
addi $s1, 1
j print_loop # Repeat to print the next letter.
end:
li $v0, 10
syscall # Load the exit code and exit the program.
Explanation / Answer
this code is working perfectly fine on my lappy