I'm trying to toggle a simple user given text from lower case to upper and vice versa. Example: “Hello” after running it becomes “hELLO” essentially toggling cases. The program currently only converts from lower case to upper. My question is, how can the transition from Uppercase to lowercase be implemented based on what's already written. Forgive me if I'm doing anything wrong. Am fairly new to this. This is what I currently have:
.data
promp: .asciiz "Enter string: "
str: .space 81
toogle_msg: .asciiz "The result is "
.text
.globl main
main:
li $v0,4 # syscall code to print
la $a0,promp # promp message tag
syscall # print promp
li $v0, 8 #syscall code to read string
li $a1, 81 # space for string
la $a0, str # str message tag
syscall # print
li $v0, 4
li $t0, 0
loop:
lb $t1, str($t0)
beq $t1, 0, exit # go to exit if $t1=0
blt $t1, 'a', case # go to case if $t1>a
bgt $t1, 'z', case # go to case if $t1<z
sub $t1, $t1, 32 # subtract 32 and save in $t1
sb $t1, str($t0)
case:
addi $t0, $t0, 1
j loop # go to loop
exit:
li $v0,4 # syscall code to print
la $a0,toogle_msg # toogle message tag
syscall # print toggle_msg
li $v0, 4 # syscall code to print
la $a0, str # str message tag
syscall # print str
li $v0, 10 # syscall code to exit
syscall # exit
You look to be attempting to handle the case where it is a lower case letter which is fine - however you also need to handle the upper case - pretty much similar logic as you have, except for upper to lowercase.
Using you existing and adding minimal changes:
loop:
lb $t1, str($t0)
beq $t1, 0, exit # go to exit if $t1=0
blt $t1, 'a', not_l_case # go to case if $t1>a
bgt $t1, 'z', not_l_case # go to case if $t1<z
sub $t1, $t1, 32 # subtract 32 and save in $t1
sb $t1, str($t0)
j next_char
not_l_case:
blt $t1, 'A', not_u_case # go to case if $t1>A
bgt $t1, 'Z', not_u_case # go to case if $t1<Z
add $t1, $t1, 32 # add32 and save in $t1
sb $t1, str($t0)
not_u_case:
next_char:
addi $t0, $t0, 1
j loop
There are a number of other ways it could be implemented which may provide less code.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.