Search code examples
assemblymipsmips32

Combining two MIPS programs to convert lowercase to uppercase and vice versa


I am trying to build a basic Mips program that takes a hardcoded string and converts lowercase chars to uppercase and vice-versa. I have managed to make two programs, one for upper to lower and one for lower to upper, but I cannot figure out how to combine the two. I believe the error in my program is coming from my checking for spaces and trying to skip over them, but I'm really not sure. With the nested case calls I am trying to mimic if-else logic

.data
string: .asciiz "\nThis Is A Test\n"
newline: .asciiz "\n"

.text
main:
    la  $t0, string # $t0 is pointer to first el of str, loading string
    li  $v0, 4      # print the original string
    la  $a0, string
    syscall
    
    li $s2, ' '
    li $v0, 4
    li $t0, 0

loop:
    lb $t1, string($t0)
    beq $t1, 0, exit
    beq $t1, $s2, neither
    
    j caseLower
   
caseLower:
    blt $t1, 'A', caseUpper
    bgt $t1, 'Z', caseUpper
    addi $t1, $t1, 32
    sb $t1, string($t0)
    addi $t0, $t0, 1
    j loop
    
    
caseUpper:
    blt $t1, 'a', neither
    bgt $t1, 'z', neither
    addi $t0, $t0, -32 
    sb $t1, string($t0)
    addi $t0, $t0, 1
    j loop
    
neither:
    addi $t0, $t0, 1
    j loop
    
exit:
    li $t0, 4
    la $a0, string
    syscall

    li $v0, 10
    syscall

This is the output I get:


This Is A Test

this Is A Test

-- program is finished running --

The output I want for the second line of output is: tHIS iS a tEST


Solution

  • addi $t0, $t0, -32 is faulty.

    It should be addi $t1, $t1, -32 .

    .data
    string: .asciiz "\nThis Is A Test\n"
    newline: .asciiz "\n"
    
    .text
    main:
        la  $t0, string # $t0 is pointer to first el of str, loading string
        li  $v0, 4      # print the original string
        la  $a0, string
        syscall
        
        li $s2, ' '
        li $v0, 4
        li $t0, 0
    
    loop:
        lb $t1, string($t0)
        beq $t1, 0, exit
        beq $t1, $s2, neither
        
        j caseLower
       
    caseLower:
        blt $t1, 'A', caseUpper
        bgt $t1, 'Z', caseUpper
        addi $t1, $t1, 32
        sb $t1, string($t0)
        addi $t0, $t0, 1
        j loop
        
        
    caseUpper:
        blt $t1, 'a', neither
        bgt $t1, 'z', neither
        addi $t1, $t1, -32 
        sb $t1, string($t0)
        addi $t0, $t0, 1
        j loop
        
    neither:
        addi $t0, $t0, 1
        j loop
        
    exit:
        li $t0, 4
        la $a0, string
        syscall
    
        li $v0, 10
        syscall