1

我正在编写一个 MIPS 程序,提示用户输入一个字符串,然后解析字符串,交换每个字符的大小写。例如->
嘿你变成嘿你。我的问题是,如果遇到空格,我如何让它什么都不做?任何帮助都会很棒!

      .data

string4:.space      82              # space for input string

strPrompt:.asciiz   "Please enter a string: "

       .text
       .globl main

main:

    la  $a0, strPrompt      # print prompt
    li  $v0, 4
    syscall
    la  $a0, string4        # read string
    li  $a1, 80
    li  $v0, 8
    syscall

    la  $a0, string4        # print string
    li  $v0, 4
    syscall
    la  $t0, string4        # $t0 = &string4[0]
    li  $t1, 0

loop4:

    lb  $t1, 0($t0)
    nop
    nop
    beqz    $t1, done4      # if $t1 == NUL, we are done
    bgt $t1, 90, else       # if char > 90 its a lower case
    nop
    addi    $t1, $t1, 0x20      # if < 90 its upper case add 32
    sb  $t1, 0($t0)
    addi    $t0, $t0, 1     # add 1 to pointer
    j   loop4                   # jump back to loop
    nop

else:

    addi    $t1, $t1, -0x20
    sb  $t1, 0($t0)
    addi    $t0, $t0, 1
    j   loop4
    nop         

done4:  
4

1 回答 1

1

$t1这是一种无分支的方法,如果它在范围内,则更改字符的大小写A..Zor a..z,如果它超出该范围,则不执行任何操作:

# if (upper($t1) >= 'A' && upper($t1) <= 'Z') $t2 = 1; else $t2 = 0;
andi $t3,$t1,0xDF    # clear bit 5, if $t1 was an alphabetic character $t3 will now be uppercase
li $t2,'A'-1
sltu $t2,$t2,$t3
sltiu $t3,$t3,'Z'+1
and $t2,$t2,$t3

sll $t2,$t2,5      # $t2 = 0x20, or 0 
xor $t1,$t1,$t2  # either swap case or do nothing
于 2013-10-22T08:50:10.817 回答