3

所以我现在正在做一个项目,我们必须接受一个字符串并将其转换为所有大写字母,然后如果 toUpper 或 toLower 字节不是 0,则转换为小写字母。现在我仍在处理大写部分(猜测小写字母几乎相同)但是当我通过字符串查找大写字母或调用系统调用进行打印时,我被卡住了。任何帮助都会很棒。这是我到目前为止所拥有的:

    .data

    toUpper: .byte   1
    toLower: .byte   0

    string:
        .asciiz "   A long time ago in a Galaxy far, far away...."

# Your code goes below this line

    origString:
        .asciiz "Original string:\n"

    toUpForwardStr:
        .asciiz "\nConverted to upper-case:\nForward:\n"

    toLowerForwardStr:
        .asciiz "\nConverted to lower-case:\nForward:\n"

    backwardStr:
        .asciiz "Backward:"

.text



main:   
    # Function prologue
    subu    $sp, $sp, 24                    # allocate stack space -- default of 24 here
    sw      $fp, 0($sp)                     # save caller's frame pointer
    sw      $ra, 4($sp)                     # save return address
    addiu   $fp, $sp, 20                    # setup main's frame pointer

    la      $a0, origString             #Print "Original String:"
    addi    $v0, $zero, 4
    syscall

    la      $a0, string                 #Print string
    addi    $v0, $zero, 4
    syscall

    la      $s0, toUpper
    sb      $s1, 0($s0)                 #toUpper stored in $s1

    la      $s0, toLower
    sb      $s2, 0($s0)                 #toLower stored in $s2

    bne     $s1, $zero, toUpperCase     #Jump toUpperCase if toUpper ≠ 0



    toUpperCase:
        la      $a0, toUpForwardStr     #Print "Converted to upper-case:"
        addi    $v0, $zero, 4               #      "Forward:"
        syscall

        la      $s3, string             #$s3 holds address to string

        addi    $s1, $zero, 0               #$s1 = i = 0

        j   upperCaseLoop                   #Goto upperCaseLoop

        upperCaseLoop:

            # Compute address of string[i]

            add     $t2, $s3, $s1           # $t2 = address of string[i]
            lb      $t3, 8($t2)             # $t3 = elements[i]

            beq     $t3, $zero, upperDone   # test if for loop is done

            addi    $t6, $zero, 96          #$t6 = 96 (lowercase letters)
            bgt     $t3, $t6, isLowercase1#If letter is lowercase goto isLowercase1

            comeBackFromLowercaseIfs:

            move    $t3, $a0
            addi    $v0, $zero, 11
            syscall




            addi    $s1, $s1, 1             # i++
            j       upperCaseLoop

    upperDone:

    bne     $s2, $zero, toLowerCase     #Jump toLowerCase if toLower ≠ 0


    toLowerCase:

        la      $a0, toLowerForwardStr  #Print "Converted to lower-case:"
        addi    $v0, $zero, 4               #      "Forward:"
        syscall
        j       done                        #The END!!







    isLowercase1:
        addi    $t7, $zero, 123         #$t7 = 123
        blt     $t3, $t7, isLowercase2  #Goto isLowercase2
        j       comeBackFromLowercaseIfs    #Go back to uppercaseLoop

    isLowercase2:
        addi    $t3, $zero, -30         #changes letter to lowercase
        j       comeBackFromLowercaseIfs    #Go back to uppercaseLoop



done:       
    # Epilogue for main -- restore stack & frame pointers and return
    lw    $ra, 4($sp)     # get return address from stack
    lw    $fp, 0($sp)     # restore the caller's frame pointer
    addiu $sp, $sp, 24    # restore the caller's stack pointer
    jr    $ra             # return to caller's code
4

1 回答 1

4

1 这个

move    $t3, $a0

应该

move    $a0, $t3

2 这个

lb      $t3, 8($t2)

应该

lb      $t3, 0($t2)

3 这个

addi    $t3, $zero, -30         #changes letter to lowercase

应该

addi    $t3, $t3, -32         #changes letter to lowercase
于 2012-10-05T09:15:49.877 回答