【问题标题】:MIPS instruction of an array of 32 bits integer32位整数数组的MIPS指令
【发布时间】:2015-09-17 22:17:27
【问题描述】:

所以我目前被困在一个问题上,我很高兴看到是否有人可以详细说明我的问题。

问题如下

令寄存器 $s1 为数组 A 的基数,

注册$v0为变量X,

注册$t0为索引i,

然后为以下语句编写 MIPS 指令

K = A[i] + A[i+3];

解决方法如下

sll $t1, $t0, 2
add $t2, $s1, $t1
lw $t3, 0($t2)
lw $t4, 12($t2)
add $v0, $t3, $t4

我对这个问题的解决方案是

addi $t1, $t0, 3  # for the $t1 <- i+3
lw $t2, 0($t1)    # for the $t2 <- A[i+3]
lw $t3, 0($t0)    # for the $t3 <- A[i]
add $v0, $t2, $t3 # for the K <- A[i+3] + A[i]

所以问题是,

我真的不明白为什么解决方案没有将索引添加 3 (i+3) 然后从添加的索引加载。

也是解决方案的第二行,添加 $t2、$s1、$t1,如何添加带有索引的基数(即 $t2

提前致谢。

如有类似问题的任何详细说明,将不胜感激。

【问题讨论】:

    标签: arrays mips


    【解决方案1】:

    请记住,A 的每个索引都不是 32 位的。 A的每个索引只有8位。上面的代码做了以下事情:

    sll $t1, $t0, 2   # take $t0 and shift it over 2 bits
                      # (equivalent to multiply by 4)
    add $t2, $s1, $t1 # from the 0 index, add on $t1 to get into the register for A[i]
    lw $t3, 0($t2)    # load 4 bytes (the entire 32bits) from A[i]
    lw $t4, 12($t2)   # load 4 bytes (the entire 32bits) from A[i + 3] 
                      # where 3 is actually 12 because it's 8bits per index
                      # so multiply by 4
    add $v0, $t3, $t4
    

    要实现的最重要的事情是“数组”的 8 位(字节)与 32 位(字)索引。

    查看您的代码:

    addi $t1, $t0, 3  # actually just moves this over 3 bytes, not 3 words
    lw $t2, 0($t1)    # gets the word at A[i / 4]
    lw $t3, 0($t0)    # gets the word at A[(i + 3) / 4]
    add $v0, $t2, $t3 # and now you can see why this would be wrong.
    

    【讨论】:

    • 感谢您的解释!那么,如果第 2 行的加法是访问 A[i] 寄存器,那么在加法之前 lw $t3, 0($t2) 将访问 A 的寄存器的空索引?
    • 你说的是正确的代码还是错误的代码?如果可能的话,也将不胜感激!
    • 我说的是正确的代码,如果 'lw $t3, 0($t2)' 在 'add $t2, $s1, $t1' 之前执行,那么它会访问寄存器 A 的空索引?
    • 另外,我刚刚获得了 15 的人气,我一定会支持你
    • 在第 2 行,$t1 = $t0 * 4。假设 $t0 为 0。假设 A[0] 位于地址 400(任意数)。然后我们需要确保 ADD 400 到 $t0 以获得正确的索引,因为 $t1= 0 和 lw $t3, 0($t1) 不会在 A 的内存范围内。我们需要添加A[0] 到 $t1 的地址,以便在 A 的内存范围内获取我们的索引
    猜你喜欢
    • 1970-01-01
    • 2016-08-09
    • 1970-01-01
    • 1970-01-01
    • 2020-07-26
    • 1970-01-01
    • 1970-01-01
    • 2011-09-01
    • 1970-01-01
    相关资源
    最近更新 更多