得到一堆作为我的输出和程序崩溃。我究竟做错了什么?

时间:2016-09-18 21:16:03

标签: assembly mips risc

## Question:
## Swap each pair of elements in
## the string "chararray" and
## print the resulting string.
## There will always be an even number
## of characters in "chararray".
##
## Output format must be:
## "badcfe"

#################################################
#                       #
#       text segment            #
#                       #
#################################################

    .text       
        .globl main
main:       # execution starts here

# Any changes above this line will be discarded by
# mipsmark. Put your answer between dashed lines.
#-------------- start cut -----------------------

la $t1, chararray
la $t2, chararray 


nextCh: lb $t0, ($t1) # get a byte from string
        lb $t3, ($t2) #get a byte from string


        beqz $t0, strEnd   #zero means end of string

        add $t2, 1
        sb $t3, ($t1)
        sb $t0, ($t2)
        add $t1, 1

        j nextCh

strEnd: la $a0, chararray
        li $v0, 4
        syscall


        li $v0, 10
        syscall


#--------------  end cut  -----------------------
# Any changes below this line will be discarded by
# mipsmark. Put your answer between dashed lines.

#################################################
#                                               #
#               data segment                    #
#                                               #
    #################################################

        .data
chararray:
    .asciiz "abcdef"
endl:   .asciiz "\n"

##
## End of file loop4.a

我对这些东西很新。我想我没有正确地在阵列中骑行,或者我使用错误的指令。你们有什么想法?你有什么想法吗?该计划继续崩溃。

1 个答案:

答案 0 :(得分:0)

试试这个:

        la   $t3, chararray
nextCh:
        lb   $t0, 0($t3)  # even byte
        lb   $t1, 1($t2)  # odd byte

        beqz $t0, strEnd  # end of string?

        sb   $t0, 1($t3)  # swap bytes
        sb   $t1, 0($t3)

        add  $t3, 2       # advance by 2 bytes
        b nextCh