MIPS数据/堆栈读取中的错误地址

时间:2017-11-09 01:46:38

标签: assembly mips spim

我花了好几个小时看着这个,打印出地址并且无法解决问题。这是一个通过QTSpim运行的简单汇编程序。程序意味着接受大小为20的数组的值,在数组中找到两个最小的整数,添加并返回到main,然后将它们存储到数组后面的位置的内存中。

程序接受数组的20个值,但随后尝试计算并吐出数据/堆栈中的错误地址:0x10040000。我所有的其他数据都在0x100100XX内存位置,不知道40000为什么会出现。

.data
Fresh: .space 80
freshSmallestSum: space 4

.text
sumMin:
    lw      $t2, 0($a0) # $t2 = smallest array[i]
    addi    $t4, $t0, 1 # $t4 = i + 1
    sll     $t4, $t4, 2 # $t4 = i * (2^2)
    add     $t4, $t4, $a0   # $t4 = absolute address array[i+1]
    lw      $t3, 0($t4) # $t3 = array[i+1]
    li      $t4, 2      # i = 2
    ble     $t2, $t3, loopSmall # if $t2 <= t3 jump
    move    $t9, $t2
    move    $t2, $t3
    move    $t3, $t9
loopSmall:
    bge     $t4, $t1, ret1
    sll     $t5, $t4, 2 # $t5 = i * (2^2)
    add     $t5, $t5, $a0   # absolute address array[i]
    lw      $t5, 0($t5) # $t5 = array[i]
    ble     $t3, $t5, next2 # if $t3 <= array[i], jump
    move    $t3, $t5    # else $t3 = array[i]
    ble     $t2, $t3, next2 # if $t2 <= $t3, jump
    move    $t9, $t2    # else swap two smallest
    move    $t2, $t3
    move    $t3, $t9
next2:
    addi    $t4, $t4, 1 # i++
    j   loopSmall
ret1:
    add     $v0, $t2, $t3   # $v0 = smallest + secondSmallest
    jr      $ra

.text
.globl main
main: 
    li      $t3, 0
    li      $t0, 0      # i = 0
    li      $t1, 20     # $t1 = 20
    la      $s2, Fresh

inLoop: 
    bge $t3, $t1, next  # If $t3 > array length, jump
    sll     $t2, $t3, 2
    add     $t4, $t2, $s2   # Absolute address of fresh
    li      $v0, 5
    syscall
    sw      $v0, 0($t4)
    addi    $t3, $t3, 1 # Increment counter
    j       inLoop      
move        $a0, $s2    # $a0 = address Fresh
    jal     sumMax      # Get max sum of Fresh
    la      $t1, freshLargestSum
    sw      $v0, 0($t1) # Store sum in freshLargestSum

    move    $a0, $s2
    jal     sumMin      # Get min sum of Fresh
    la      $t1, freshSmallestSum
    sw      $v0, 0($t1) # Store sum in freshSmallestSum

exit:       li  $v0, 10
    syscall

1 个答案:

答案 0 :(得分:1)

这里的大部分代码都很棒,但这不是完整的文件吗?

看起来你的sumMin函数想要数组大小为t1,但是它给出了一个freshLargestSum地址?

您是否尝试逐行逐步执行代码? 特别是对于MIPS,大多数错误通常会覆盖我们稍后在代码中需要的寄存器;特别是当我们回来的时候。

一个好的调试技术也是测试所有功能都是按预期独立工作的;如果是这样,你知道错误存在于主代码中;并可能与:

  • 覆盖寄存器
  • jal到错误的地方
  • 覆盖退货登记册
  • 逻辑错误

希望这可以帮助您解决问题!