What is the correct way to store and print a character in MIPS?

32 Views Asked by At

I writing a simple program to reverse the case of characters that the user inputs and it prints out the reversed string and the lowest character in the new string (according to it's ASCII value). The issue arises when I try to print the character. I can see that a value is retained in register $s1, but printing the character yields a box instead of any actual character, and if I try to print it as a string it simply prints as an empty line.

Here is my program:

main:   
#
# system call to prompt user for input
#
     la $a0,outpPrompt  # system call 4 for print string needs address of string in $a0
     li $v0,4       # system call 4 for print string needs 4 in $v0
     syscall    
#
# system call to store user input into string thestring
#
    li $v0,8        # system call 8 for read string needs its call number 8 in $v0
                    # get return values
    la $a0,varStr       # put the address of thestring buffer in $t0
    li $a1,32           # maximum length of string to load
        syscall
#
# system call to display "You entered the string: "
#
     la $a0,outpStr     # system call 4 for print string needs address of string in $a0
     li $v0,4       # system call 4 for print string needs 4 in $v0
     syscall    

     la $a0,varStr      # system call 4 for print string needs address of string in $a0
     li $v0,4       # system call 4 for print string needs 4 in $v0
     syscall

    jal revCase     # Invoking revCase, string stored in $a0 printed

# Exit gracefully from main()
         li   $v0, 10       # system call for exit
         syscall            # close file

revCase:
    
    la $t1, varStr      # Loading user input
    la $t2, varStrRev   # Designated output string
    
    revLoop:
        lb $t0, 0($t1)      # loads char of $t1 into $t0
        beqz $t0, doneRev       # when 0 is hit, ends the loop
        
        blt $t0, 'A', saveChar  # if $t0 < A then saveChar
        ble $t0, 'Z', lowerCase # if $t0 >= A and <= Z jump to lowerCase
        blt $t0, 'a', saveChar  # if $t0 < a then saveChar
        ble $t0, 'z', upperCase # if $t0 >= a and <= z jump to upperCase
        j saveChar      # if $t0 > z then saveChar
        
        upperCase:
        subi $t0, $t0, 32   # converts uppercase letter to lowercase letter
        j saveChar
        
        lowerCase:
        addi $t0, $t0, 32   # converts lowercase letter to uppercase letter
        j saveChar
        
        saveChar:
        sb $t0, 0($t2)      # character saved to $t2
        
        addi $t1, $t1, 1    # increment input
        addi $t2, $t2, 1    # increment output
        
    j revLoop           # continues the loop
    
    doneRev:
    sb $zero, 0($t2)    # adds a 0 to end of string
    la $v0, varStrRev   # Stores address of converted string into $v0

#
# This is the system call to display "Your string in reverse case is: "
     la $a0,outpStrRev  # system call 4 for print string needs address of string in $a0
     li $v0,4       # system call 4 for print string needs 4 in $v0
     syscall    
# system call to display the user input that is in reverse case saved in the varRevStr buffer
     la $a0,varStrRev   # system call 4 for print string needs address of string in $a0
     li $v0,4       # system call 4 for print string needs 4 in $v0
     syscall    

    move $s0, $ra
    jal findMin     # calls findMin, replaces $ra value

jr $s0          # Return to original function call address

findMin:
    la $t1, varStrRev       # Loading reverse input
    la $s1, varStrMin       # Designated output string
    
    minLoop:
        lb $t0, 0($t1)      # setting $t0 to first char of $t1
        beqz $t0, doneMin       # break loop when $t0 = 0
        
        blt $t0, $s1, lowestChar    # if current char < saved char, jump to lowestChar
        j nextChar      # if current char >= lowest char, jump to nextChar
        
        lowestChar:
        sb $t0, 0($s1)      # save lowest char to $s1
        j nextChar
        
        nextChar:
        addi $t1, $t1, 1    # increment to next char

    j minLoop           #continue loop
    doneMin:
#
# system call to display "The min ASCII character after reversal is:  "
     la $a0, outpStrMin     # system call 4 for print string needs address of string in $a0
     li $v0, 4      # system call 4 for print string needs 4 in $v0
     syscall    

    la $a0, varStrMin
    li $v0, 11  
    syscall

jr $ra              # Return to address in revCase

I have tried printing using both syscall 4 & 11. I have watched the assembler in real-time to ensure that a value is being stored in register $s1. The first loop properly prints out the inverted string, so I know that these values are correct. I have tested the loop in findMin() to make sure it is running the appropriate number of times instead of exiting early. I have exhausted my knowledge of anything that could possibly be wrong. My thought is that the value is correctly stored but I am accessing it incorrectly. Any help in understanding where the program is breaking would be greatly appreciated.

0

There are 0 best solutions below