【问题标题】:MIPS SPIM-CACHE simulator lw exceptionMIPS SPIM-CACHE 模拟器 lw 异常
【发布时间】:2016-01-30 09:38:37
【问题描述】:

所以我必须在 MIPS 中为我​​的大学写一个代码,它将 A[i][j] 与 B[i][j](A 和 B 是 4x4 数组)进行比较,并将放置最大的A[i][j] 中的数字和 B[i][j] 中的最小数字。这是我已经写的代码:

.data
Ann: .word 4, 6, 10, 5, 4, 7, 10, 8, 3, 6 ,7, 12, 3, 2, 15, 6
Ban: .word 3, 7, 5, 6, 5, 12, 18, 9, 7, 6, 12, 4, 2, 7, 8, 4


.text            
.globl main      

main:   la    $t0, Ann       # $t0 represents start address of A[i][j]
la    $t1, Ban           # $t1 represents start address of B[i][j]
lw    $s2,0($t0)
lw    $s1,0($t1)                         

addi   $s5, $zero, 4    # set maximum iteration to be 4 
addi   $s6, $zero, 0     # set i = 0

loopi:  addi   $s7, $zero, 0     # set j = 0
jal    loopj             # starts inner loopj
addi   $s6, $s6, 1       # i++
bne    $s5, $s6, loopi   # continue loopi if i < 4
j      finish            

loopj:  sll    $t7, $s6, 2       
add    $t7, $t7, $s7
sll    $t7, $t7, 2       # 4 * ((i * 4) + j)  
add    $t9, $t7, $s2     # address of A[i][j]
lw     $t6, 0($t9)       # value of A[i][j]
add    $t4, $t7, $s1     # address of B[i][j]
lw     $t5, 0($t4)       # value of B[i][j]


slt $t8,$t6,$t5         
beqz $t8,cont
add $t2,$zero,$t6
add $t6,$zero,$t5
add $t5,$zero,$t2
sw $t6,0($t4)
sw $t5,0($t9)        
cont:   addi   $s7, $s7, 1       # j++
bne    $s5, $s7, loopj   # continue loopj if j < 4
jr $ra







 finish: li  $v0, 1           
    add $a0,$s5, $zero 
    syscall

当我尝试将它运行到 spim 缓存时,我在指令“lw $t6, 0($t9) # value of A[i][j]”处遇到异常 .... 可能是什么问题,我不知道:\

【问题讨论】:

    标签: arrays compare 2d mips


    【解决方案1】:

    我建议您学习如何使用 SPIM 和 MARS 等模拟器提供的调试功能,因为找到此类错误的原因非常容易。

    如果你只是尝试正常运行程序,你会得到一堆 execptions,第一个出现在 PC=0x400064,即lw $t6, 0($t9)

    因此,您在 0x400064 处设置了一个断点,当断点被触发时,您检查寄存器值以查看它们是否合理。好吧,$t9 包含值 4,这不是一个可以尝试读取的合理地址。

    您通过添加$t7$s2 得到$t9 的值,所以接下来要做的是查看这两个寄存器。 $t7 的值为 0,这似乎没问题,因为我们正在进行第一次迭代,其中 ij 都是 0。$s2 的值为 4,这似乎是错误的,因为我要去假设这应该是数组的基地址。

    你是怎么得到$s2=4的?通过执行lw $s2,0($t0)Ann 数组中的第一个值加载到$s2 中。这就是你的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-23
      • 1970-01-01
      相关资源
      最近更新 更多