【问题标题】:Using arrays in MIPs在 MIP 中使用数组
【发布时间】:2015-12-24 10:28:14
【问题描述】:

有人可以帮我看看这个 MIP 代码中的某些行在做什么吗?

C 代码是B[8] = A[i - j],其中 i = $s3、j = $s4、&A[] = $s6 和 &B[] = $s7

MIPs代码如下...

sub $t0, $s3, $s4  # i - j
sll $t0, $t0, 2 #Gets the offset of 8 for B[]
add $t0, $s6, $t0 #Goes to the offset in B[] ?
lw $t1, 0($t0) #????
sw $t1, 32($s7) #????

当它到达最后 3 行时,我有点迷失了。

为什么是 0($t0) 和 32($s7)?或者为什么是 0 和 32?

【问题讨论】:

  • 什么是MIPs?那是MIPS 的28 位版本吗?

标签: c arrays assembly mips


【解决方案1】:
sll $t0, $t0, 2   // This multiplies (i-j) * 4, not 8. Because the indexes are 4-byte ints
add $t0, $s6, $t0 // $t0 = A + (i-j)*4. So $t0 = &A[i-j]
lw $t1, 0($t0)     // $t1 = A[i-j]
sw $t1, 32($s7)   // B[32/4] = $t1

32($s7) 表示 $s7 + 32。之所以添加 32,是因为您正在访问整数数组的第 8 个元素,该元素位于内存地址 B + 8*sizeof(int) = B + 32

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-02
    • 1970-01-01
    • 2022-11-10
    相关资源
    最近更新 更多