MIPS 如何在堆栈上保存地址?

发布于 2024-11-09 07:24:51 字数 1140 浏览 4 评论 0原文

我有这样的代码:

.data

    array: .word 13, 11, 5, 9, 0, -3
    size: .word 6

.text

Main:
    la $a0, array
    lw $a1, size
    jal PrintIntArray
    j Exit

# $a0 - array, $a1 - size
PrintIntArray:
addi $sp, $sp, -12
li $t0, 0
sw $t0, 0($sp) # i
sw $a0, 4($sp) # array
sw $a1, 8($sp) # size

li $a0, '['
li $v0, 11
syscall

lw $t1, 8($sp) # size
ble $t1, $0, EmptyArray
    PrintLoop:
        lw $t1, 8($sp) # size
        lw $t0, 0($sp) # i
        bge $t0, $t1, PrintLoopEnd
            lw $t0, 0($sp) # i
            lw $t2, 4($sp) # array
            add $t2, $t2, $t0

            lw $a0, 0($t2) # <====== RUNTIME EXCEPTION AT THIS LINE !!!
            li $v0, 1
            syscall

            li $a0, ','
            li $v0, 11
            syscall

            lw $t0, 0($sp) # i
            add $t0, $t0, 1
            sw $t0, 0($sp)
            j PrintLoop
    PrintLoopEnd:
EmptyArray:

li $a0, ']'
li $v0, 11
syscall

jr $ra

Exit:

我标记的行产生以下运行时异常:

util.asm 第 37 行错误:运行时 0x00400060处的异常:获取地址 未在字边界上对齐 0x10010001

我做错了什么?我想我在加载/存储地址时犯了一些错误。

I have this code:

.data

    array: .word 13, 11, 5, 9, 0, -3
    size: .word 6

.text

Main:
    la $a0, array
    lw $a1, size
    jal PrintIntArray
    j Exit

# $a0 - array, $a1 - size
PrintIntArray:
addi $sp, $sp, -12
li $t0, 0
sw $t0, 0($sp) # i
sw $a0, 4($sp) # array
sw $a1, 8($sp) # size

li $a0, '['
li $v0, 11
syscall

lw $t1, 8($sp) # size
ble $t1, $0, EmptyArray
    PrintLoop:
        lw $t1, 8($sp) # size
        lw $t0, 0($sp) # i
        bge $t0, $t1, PrintLoopEnd
            lw $t0, 0($sp) # i
            lw $t2, 4($sp) # array
            add $t2, $t2, $t0

            lw $a0, 0($t2) # <====== RUNTIME EXCEPTION AT THIS LINE !!!
            li $v0, 1
            syscall

            li $a0, ','
            li $v0, 11
            syscall

            lw $t0, 0($sp) # i
            add $t0, $t0, 1
            sw $t0, 0($sp)
            j PrintLoop
    PrintLoopEnd:
EmptyArray:

li $a0, ']'
li $v0, 11
syscall

jr $ra

Exit:

The line marked by me produces the following run-time exception:

Error in util.asm line 37: Runtime
exception at 0x00400060: fetch address
not aligned on word boundary
0x10010001

What did I do wrong? I suppose I made some mistake in loading/storing the address.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寄风 2024-11-16 07:24:51

您需要将 i 乘以数组元素的大小,然后将其添加到数组的基地址以计算第 i 个元素的地址。请注意,如果元素大小为 4 字节,则可以通过左移两位轻松执行此乘法。

You need to multiply i by the size of the array element, then add it to the base address of the array in order to compute the address of the ith element. Note that, if the element size is 4 bytes, this multiplication can be performed easily by a left shift of two bits.

凉宸 2024-11-16 07:24:51

您正在尝试执行未对齐的 32 位加载,这在(通用)MIPS 架构上是不允许的。当i等于1时,您尝试加载表单地址0x10010000(数组)+ 1(i)。尝试将 t0 (i) 乘以 4,然后将其添加到 t2(数组)

You are trying to do an unaligned 32-bit load, which is not allowed on (generic) MIPS architectures. When i equals one, you're trying to load form address 0x10010000 (array) + 1 (i). Try multiplying t0 (i) by 4 before adding it to t2 (array)

掌心的温暖 2024-11-16 07:24:51

不要将 i 加 1,而是尝试以下操作,

add $t0, $t0, 4

而不是

add $t0, $t0, 1

将 32 位整数的大小(以字节为单位)添加到索引中。 MIPS 要求将 4 字节值存储到 4 字节倍数的地址并从该地址加载。 (即,低位两位为零。)

Rather than increment i by 1, try the following

add $t0, $t0, 4

rather than

add $t0, $t0, 1

This will add the size, in bytes, of a 32-bit integer to your index. The MIPS requires that 4-byte values be stored to and loaded from addresses which are multiples of 4-bytes. (I.e., with the low-order two bits zero.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文