簡體   English   中英

尖頭組裝撥動盒

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM