Removing the last value in a string each time it is printed MIPs

21 Views Asked by At



#########################################

.text 
main: 
    # Prompt for the string to enter
        li $v0, 4 
        la $a0, prompt
    syscall 

        # Read the string. 
        li $v0, 8   
        la $a0, word
        lw $a1, inputSize 
        syscall
        la $t0, word #set it so that word is in t0
        li $t1, 0 #length of string
        jal string_length #find length of stirng and store it in string_length
        li $t2, 0 #counter for print_loop
        jal print_loop
        j end
string_length:
    lb $s0, ($t0) #set byte to be checked to s0
    bnez $s0, not_end_string_length #if not at end of string then return to start of loop
    jr $ra #return to main, or wherever function was called
not_end_string_length: 
    addiu $t0, $t0, 1 #increment pointer    
    addiu $t1, $t1, 1 #add 1 to counter with length of string
    j string_length #go back to string_length
print_loop:  #print out for the amount of times that is the length of word entered  
    la $t0, word #put word in t0
    li $t3, 0 #counter for inner loop
    addi $t4, $1, -1 #set length of string to be printed to be one less than previous length printed
    # print out a new line  (formatting)
    li $v0, 4
    la $a0, newLine 
    syscall
    #works until here
        blt $t2, $t1, print_out #if counter is less than string length then print_out
        jr $ra #return to where called (end loop)
print_out:
    bge $t3, $t4, print_loop #go back to print loop if amount of character has reached amount that should be printed
    j not_end_print_out
not_end_print_out: ######################################## Errors are here 
    #print out char num t3#
    addi $t0, $t0, 1
    #set char to be value t3
    lb $a0, 0($t0)
    li $v0, 11
    syscall
    
    addi $t3, $t3, 1
    j print_out #go back to print_out
####################################################################
end:
    # Output end message
    li $v0,8
    la $a0, endMessage
    syscall
    #

.data
prompt: .asciiz "\nEnter a string: "
word: .space 31
char: .space 2
inputSize: .word 30
charSize: .word 1
newLine: .asciiz "\n"
endMessage: .asciiz "\nYou have reached the end. Goodbye"

I need my code to print out the string that the user enters, where each time the last value is removed. For example if "hi" was entered then

hi

h

would be printed out.

I tried to switch up a few thing with the code for printing out the characters, but honestly I dont really know what to do. I tried to change it so that there were a few different methods for printing stuff out, but nothing. For some reason the first char is not printed, and then only one more line is printed. And that line has some garbage values.

1

There are 1 best solutions below

3
Victoria On

If anyone was wondering I managed to fix this by changing around a few small errors. Turns out most of the problems were small things like writing $1 instead of $t1. Hopefully next time I write code I will check it over a few more times.