MIPS 如何在堆栈上保存地址?
我有这样的代码:
.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要将 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.
您正在尝试执行未对齐的 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 address0x10010000 (array) + 1 (i)
. Try multiplying t0 (i) by 4 before adding it to t2 (array)不要将
i
加 1,而是尝试以下操作,而不是
将 32 位整数的大小(以字节为单位)添加到索引中。 MIPS 要求将 4 字节值存储到 4 字节倍数的地址并从该地址加载。 (即,低位两位为零。)
Rather than increment
i
by 1, try the followingrather than
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.)