简体   繁体   English

尖头组装拨动盒

[英]Mips Assembly toggle case

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. 示例:运行后的“ Hello”实际上变成了切换案例的“ hELLO”。 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. 有许多其他可以实现的方法可以提供更少的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM