MIPS提取地址未在字边界上对齐,已使用.align 4,仍然无法执行 [英] MIPS fetch address not aligned on word boundary, used .align 4, still no go

查看:558
本文介绍了MIPS提取地址未在字边界上对齐,已使用.align 4,仍然无法执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么想法吗?为什么我得到: 运行时异常位于0x00400020:提取地址未在字边界0x00000007上对齐 问题行是:lw $ s1,0($ a1)#copy arg2 =数组大小

any ideas? Why I am getting: Runtime exception at 0x00400020: fetch address not aligned on word boundary 0x00000007 Problem line is: lw $s1,0($a1) #copy arg2 = size of array

.data
    .align 4 #added this, didnt work
    size:   .word   7
    .align 4 #added this, didnt work
    search: .word   30
    .align 4 #added this,didnt work
    array:  .word 10,20,30,40,50,60,70
    .align 4

.text

main:

            la  $a0,array   #$a0 = address of array
            lw  $a1,size    #a1 = size of array
            lw  $a2,search  #$a2 = search key


COUNT:
            lw $s0,0($a0)   #copy arg1 = address array
            addi $s1,$zero,7
            lw $s1,0($a1)   #copy arg2 = size of array
            lw $s2,0($a2)   #copy arg3 = search key (n)
            addi $s2,$zero,30
            COUNTLOOP:
            add $v0,$zero,$zero #v0 = res
            add $t0,$zero,$zero #$t0 = init i to 0
            slt $t1,$t0,$s1     #check if i > size of array
            beq $t1,$zero,DONECOUNT #i is n so end
            sll $t2,$s0,2       #$t2 = get off set for a[i]
            lw  $t3,0($t2)      #$t3 = get value of a[i]
            bne $t3,$s2,CLOOPBTM #check if a[i] == seach key
            addi $v0,$v0,1      #if above then increment res
            CLOOPBTM:
            addi $t0,$t0,1
            j COUNTLOOP
            DONECOUNT:

推荐答案

代码的问题是,您使用的不是存储大小的地址,而是大小本身:

The problem with the code is, that you're not using the address where the size is stored but the size itself:

在这里将地址加载到A0中,将大小(7)加载到A1中:

Here you load the address into A0 and the size (7) into A1:

        la  $a0,array   
        lw  $a1,size    #a1 = size of array

在这里加载数组中存储的第一个单词(将加载10).这不是您想要的.

Here you load the first word stored at your array (that will load a 10). This is not what you've intended.

        lw $s0,0($a0)   #copy arg1 = address array
        addi $s1,$zero,7

在这里加载存储在位置0x000007的第一个单词. (您的尺码).这 也可能不是故意的,并且会因为地址未对齐而导致异常:

Here you load the first word stored at location 0x000007. (your size). This is probably also not intended and will cause an exception because the address is not aligned:

        lw $s1,0($a1)   #copy arg2 = size of array

以此类推.

在我看来,您对LW指令的功能有误解.它将存储单元读取到寄存器中.您希望在循环的序幕中制作一个寄存器的副本.

It seems to me, that you have a misunderstanding what the LW instruction does. It reads a memory location into a register. What you want in the prolog of your loop is to make copies of a register.

为此,如果汇编程序支持,可以使用move伪指令.否则,使用OR指令复制寄存器,如下所示:

To do so you can use the move pseudo instruction if your assembler supports it. Otherwise use the OR instruction to copy registers like this:

COUNT:
            or    $s0, $a0, $a0   #copy arg1 = address array
            addi  $s1, $zero,7
            or    $s1, $a1, $a1   #copy arg2 = size of array
            or    $s2, $a2, $a2   #copy arg3 = search key (n)
            addi  $s2, $zero,30
            COUNTLOOP:

            ...

有关线性搜索循环的完整示例,请尝试以下操作(未经测试,并期望汇编程序在乎延迟槽)

for a complete example of a linear search loop try this (untested and expects that the assembler cares about the delay slots)

main:

            la  $a0,array            # $a0 = address of array
            lw  $a1,size             # $a1  = size of array
            lw  $a2,search           # $a2 = search key


            beq $a1, $zero, NOTFOUND # handle the size==0 case..
            or  $v0, $zero, $zero    # init counter to zero

LOOP:
            lw  $s0, 0($a0)          # load element
            beq $s0, $a2, FOUND      # branch if key found:

            addiu $a0, $a0, 4        # increment array pointer
            addiu $v0, $v0, 1        # increment loop counter
            bne   $v0, $a1, LOOP     # repeat until we've processed the array.

NOTFOUND:
            # --------------------------------------
            # if you reach this, key does not exist:
            # --------------------------------------
            li  $v0, -1              # load a -1 to signal key not found.
            jr  $lr                  # return to caller

FOUND:
            # -----------------------------------------
            # v0 now contains the position of the key.
            # -----------------------------------------
            jr  $lr

这篇关于MIPS提取地址未在字边界上对齐,已使用.align 4,仍然无法执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆