[英]Mips Assembly toggle case
我正在尝试将给定的简单用户文本从小写切换为大写,反之亦然。 示例:运行后的“ Hello”实际上变成了切换案例的“ hELLO”。 该程序当前仅从小写转换为大写。 我的问题是,如何根据已写的内容实现从大写到小写的转换。 如果我做错了什么,请原谅我。 这是很新的。 这是我目前拥有的:
.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
您可能会尝试处理小写字母的情况,这很好,但是您也需要处理大写字母,除了大写字母到小写字母外,其他逻辑也非常相似。
使用您现有的资源并添加最少的更改:
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
有许多其他可以实现的方法可以提供更少的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.