Mips Assembly 开关盒
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
还有许多其他方法可以实现,这些方法可能会提供更少的代码。
我正在尝试将给定文本的简单用户从小写切换为大写,反之亦然。示例:“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
还有许多其他方法可以实现,这些方法可能会提供更少的代码。