MIPS 动态数组错误

MIPS Dynamic Array Error

我正在尝试分配一个动态数组并让它接受来自控制台的输入,但是一旦我开始向数组中输入一些数字,它就会说出现异常 7 错误。 (错误数据地址)

这是我在 运行 使用 read_array 从控制台读取数字的子程序之前使用的代码:

la $a0, bIntro_p
li $v0, 4
syscall

li $a0, 0 #reset
li $a1, 0 #reset

li $v0, 5
syscall
move $t0, $v0 #moves length into $t0 for allocation, keeps length there

li $v0, 9 #allocation, sets base address into $v0
move $a0, $t0
syscall #base address is now in $v0
move $t1, $v0 #base now in $t1

move $a0, $t0 #length ($t0) goes into $a0 before reading
move $a1, $t1 #base address ($t1) goes into $a1 before reading

jal read_array

我知道参数传递有很多多余的移动命令,但这主要来自故障排除。据我了解,动态数组应该在 运行 系统调用 9 之后将它们的基地址存储在 $v0 中,对吗? (一个月前才开始学习MIPS。)

这是 read_array 的子程序:

read_array:
# Read words from the console, store them in
# the array until the array is full
li $t0, 0
li $t1, 0

move $t0, $a0 #length
move $t1, $a1 #base address
li $t9, 0 #makes sure count is reset before engaging
sw $t1, myBaseHolder #save the base address into the holder word

rWhile:

bge $t9, $t0, endR #branch to end if count > length

li $v0, 5 #call for an int from console
syscall

sw $v0, 0($t1) #saves the word from the console into the array

addiu $t9, $t9, 1 #count++
addiu $t1, $t1, 4 #increments the address
b rWhile

endR:

jr $ra

奇怪的是,这段代码对于我必须在程序的前面分配的静态数组来说工作得很好,但是动态数组似乎破坏了我的代码,我不知道是不是因为我没有将正确的值传递给子程序,或者是因为子程序一开始就有缺陷。

为了更深入地了解子程序参数传递结构,我已将该程序的全部代码上传到 Pastebin here。任何见解将不胜感激!

您的程序将动态分配的数组视为其容量(或您所说的长度)等于分配的字节数,即,就好像您在数组中保留的元素每个都是一个字节。
但是您并没有将字节写入数组;你正在写字,每个字有 4 个字节。因此,当您提示用户输入数组长度时,您需要在进行分配之前将获得的数字乘以 4,因为您的数组所需的字节数是 length * 4.