我的MIPS代码(数组)有什么问题?

我的MIPS代码(数组)有什么问题?,mips,Mips,好吧,我必须改变信仰 # j = 0 # x = a[j+k] # a[j+k] = a[j] # repeat n-1 times # j = (j+k) mod n # m = (j+k) mod n # y = a[m] # a[m] = x # x = y # end repeat 到MIPS代码。到目前为止,我已经想出了这个,但它打印出了不正确的输出。有什么帮助吗 .data .align 2 head1: .asciiz

好吧,我必须改变信仰

# j = 0
# x = a[j+k]
# a[j+k] = a[j]
# repeat n-1 times
#   j = (j+k) mod n
#   m = (j+k) mod n
#   y = a[m]
#   a[m] = x
#   x = y
# end repeat
到MIPS代码。到目前为止,我已经想出了这个,但它打印出了不正确的输出。有什么帮助吗

         .data
         .align 2
head1:   .asciiz "\n\nThis program reads a list of integers into an array, and then"
head2:   .asciiz "\nshifts the array to the right with wrap around."
head3:   .asciiz "\nThe list is terminated by a negative integer."
prompt1: .asciiz "\nEnter the shift amount first: "
prompt2: .asciiz "\nEnter array numbers (negative to end)"
newline: .asciiz "\n"
         .text
         .globl main
main:    
# print the prompt messages      See appendix B of P&H or chapter 4 of Britton
         li   $v0, 4           # $v0 = print_string system call code
         la   $a0, head1     # $a0 = address of first prompt
         syscall               # print first prompt
         li   $v0, 4           # $v0 = print_string system call code
         la   $a0, head2     # $a0 = address of second prompt
         syscall               # print second prompt
         li   $v0, 4           # $v0 = print_string system call code
         la   $a0, head3     # $a0 = address of third prompt
         syscall               # print third prompt
# Main program - calls read, rearrange and print functions
         li   $v0, 4           # $v0 = print_string system call code
         la   $a0, prompt1     # $a0 = address of fourth prompt
         syscall               # print fourth prompt
         li   $v0, 5           # $v0 = read_int system call code
         syscall               # read an integer - n
         move $a2, $v0         # $a2 = separation point for rearranging array
         li   $v0, 4           # $v0 = print_string system call code
         la   $a0, prompt2     # $a0 = address of fifth prompt
         syscall               # print fifth prompt
         li   $a0, 0x10000000  # $a0 = base address of array = 10000000 (hex)         
         jal  reada            # read numbers into array
         move $a1, $v0         # $a1 = number of ints read - size of array
         jal  shift            # shift the array to the right
         jal  printa           # print numbers from array to screen
# Call system exit
         li   $v0, 10          # $v0 = exit system call code
         syscall               # halt program execution

# Algorithm for reading numbers into an array
reada:   move $t0, $a0         # $t0 = base address of array
         move $t1, $zero       # $t1 = counter = 0
read:    li   $v0, 5           # $v0 = read_int system call code
         syscall               # read an integer - n
         slt  $t2, $v0, $zero  # if n < 0 then stop reading
         bne  $t2, $zero, exit # exit procedure and return to caller
         sw   $v0, 0($t0)      # a[i] = n
         addi $t1, $t1, 1      # i = i + 1
         addi $t0, $t0, 4      # $t0 = address of a[i + 1]
         j    read             # go back up and read another integer
exit:    move $v0, $t1         # return number of integers read into array
         jr   $ra              # return to calling routine

# Algorithm for printing numbers from an array
printa:  move $t0, $a0         # $t0 = base address of array
         move $t1, $zero       # $t1 = counter = 0
print:   beq  $t1, $a1, exitp  # if (array index = array size), then exit
         lw   $t2, 0($t0)      # $t2 = a[i]
         li   $v0, 4           # $v0 = print_string system call code
         la   $a0, newline     # $a0 = address of newline character
         syscall               # print new line
         li   $v0, 1           # $v0 = print_int system call code
         move $a0, $t2         # $a0 = a[i]
         syscall               # print a[i]
         addi $t1, $t1, 1      # i = i + 1
             addi $t0, $t0, 4      # $t0 = address of a[i + 1]
         j    print            # go back up and print another integer
exitp:   li   $v0, 4           # $v0 = print_string system call code
         la   $a0, newline     # $a0 = address of newline character
         syscall               # print new line
         jr   $ra

# Algorithm for rearranging array

# j = 0
# x = a[j+k]
# a[j+k] = a[j]
# repeat n-1 times
#   j = (j+k) mod n
#   m = (j+k) mod n
#   y = a[m]
#   a[m] = x
#   x = y
# end repeat

# Register usage
# $a0 = base address of array
# $a1 = n, size of array
# $a2 = k, the shift amount


shift:    li $t3, 0          #j = 0
          lw $t3, 0($a0)     #load the value of $a0 in to j
          add $t3, $t3, $a2  # j = j+k
          sw $t3, 4($a0)     # store the new value of j in to $a0

repeat:   beq $a1, $zero, end
          sub $a1, $a1, 1      #n = n-1
          add $t4, $t3, $a2    #j+k
          div $t4, $a1         #divide (j+k) by n
          mfhi $t5        

          move $t5, $t3        #j = (j+k) mod n

          add $t4, $t5, $a2    #j+k
          div $t4, $a1         #divide (j+k) by n
          mfhi $t5             #move rem into t2
          move $t5, $t3        #m = (j+k) mod n
          sw $t3, 4($a0)
          jr $ra
          lw $t3, 0($a0)
          b repeat

end:    
.data
.对齐2
head1:.asciiz“\n\n此程序将整数列表读取到数组中,然后”
head2:.asciiz“\n使用环绕将数组向右移动。”
head3:.asciiz“\n列表以负整数结尾
提示1:.asciiz“\n请先输入班次金额:”
prompt2:.asciiz“\n输入数组号(从负到尾)”
换行符:.asciiz“\n”
.文本
格洛博梅因酒店
主要内容:
#打印提示信息,参见P&H附录B或Britton第4章
li$v0,4#$v0=打印字符串系统调用代码
la$a0,head1#$a0=第一个提示的地址
系统调用#打印第一个提示
li$v0,4#$v0=打印字符串系统调用代码
la$a0,head2#$a0=第二个提示的地址
系统调用#打印第二个提示
li$v0,4#$v0=打印字符串系统调用代码
la$a0,head3#$a0=第三个提示的地址
syscall#打印第三个提示
#主程序-调用读取、重新排列和打印功能
li$v0,4#$v0=打印字符串系统调用代码
la$a0,prompt1#$a0=第四个提示的地址
系统调用#打印第四个提示
li$v0,5#$v0=读取系统调用代码
syscall#读取整数-n
移动$a2,$v0#$a2=重新排列阵列的分离点
li$v0,4#$v0=打印字符串系统调用代码
la$a0,prompt2#$a0=第五个提示的地址
系统调用#打印第五个提示
li$a0,0x10000000#$a0=数组的基址=10000000(十六进制)
日航reada#将数字读入数组
move$a1,$v0#$a1=整数读取数-数组大小
日航移位#将阵列向右移位
jal printa#将数字从阵列打印到屏幕
#呼叫系统出口
li$v0,10#$v0=退出系统调用代码
系统调用#停止程序执行
#将数字读入数组的算法
reada:move$t0,$a0#$t0=数组的基址
移动$t1,$零#$t1=计数器=0
read:li$v0,5#$v0=读取系统调用代码
syscall#读取整数-n
slt$t2、$v0、$0#如果n<0,则停止读取
bne$t2,$zero,退出#退出过程并返回给调用方
sw$v0,0($t0)#a[i]=n
加上$t1,$t1,1#i=i+1
addi$t0,$t0,4#$t0=a[i+1]的地址
j read#返回并读取另一个整数
退出:移动$v0,$t1#返回读取到数组中的整数数
jr$ra#返回呼叫例行程序
#从数组打印数字的算法
printa:move$t0,$a0#$t0=数组的基址
移动$t1,$零#$t1=计数器=0
打印:beq$t1,$a1,exitp#如果(数组索引=数组大小),则退出
lw$t2,0($t0)#$t2=a[i]
li$v0,4#$v0=打印字符串系统调用代码
la$a0,换行符#$a0=换行符的地址
syscall#打印新行
li$v0,1#$v0=打印系统调用代码
移动$a0,$t2#$a0=a[i]
syscall#打印一个[i]
加上$t1,$t1,1#i=i+1
addi$t0,$t0,4#$t0=a[i+1]的地址
j print#返回并打印另一个整数
exitp:li$v0,4#$v0=打印字符串系统调用代码
la$a0,换行符#$a0=换行符的地址
syscall#打印新行
jr$ra
#阵列重排算法
#j=0
#x=a[j+k]
#a[j+k]=a[j]
#重复n-1次
#j=(j+k)模n
#m=(j+k)模n
#y=a[m]
#a[m]=x
#x=y
#结束重复
#注册使用
#$a0=阵列的基址
#$a1=n,数组大小
#$a2=k,班次金额
班次:李$t3,0#j=0
lw$t3,0($a0)#将$a0的值加载到j中
加上$t3、$t3、$a2#j=j+k
sw$t3,4($a0)#将j的新值存储到$a0
重复:beq$a1,$0,结束
低于$a1,$a1,1#n=n-1
加上$t4、$t3、$a2#j+k
div$t4,$a1#除以(j+k)n
mfhi$t5
移动$t5、$t3#j=(j+k)模块n
加上$t4、$t5、$a2#j+k
div$t4,$a1#除以(j+k)n
mfhi$t5#将rem移动到t2
移动$t5,$t3#m=(j+k)模n
sw$t3,4($a0)
jr$ra
lw$t3,0($a0)
b重复
完:

jr$ra之后的lw$t3将永远不会出现