2016-09-18 48 views
0
## 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

你在做什么错不使用调试器。这会告诉你你坠毁了哪条指令,并让你检查regs/mem。您甚至可以一次单步执行一条指令。在没有调试器的情况下写入asm就像在蒙上眼睛的时候构建一个机器人。也就是说,你确实描述了你的程序如何失败,并且有一些评论,所以也许有人想为你调试你的代码将很容易回答。 –

+0

当然,由于代码显然是写在SPIM/MARS中执行的,所以你甚至不需要找到调试器,因为模拟器有一个内置的等待你使用它。 – Michael

回答

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 
相关问题