返回列表 發帖

[【學科】] 【問題】計算機組織

Assume that the variables f, g, h, i, and j are assigned to registers $s0, $s1, $s2, $s3, and $s4, respectively. Assume that the base address of the arrays A and B are in registers $s6 and $s7, respectively. Answer the following question.

1.For the following C statement, what is the corresponding MIPS assembly code?
B[8] = A[i−j]

我的過程:
sub $t0,s2,s3
lw $t0,t0($s6)
sw $t0,32($s7)

想問這樣對嗎 還是第二行應該要改成lw $t1,t0($s6)第三行改成sw $t1,32($s7)

2.For the MIPS assembly instructions below, what is thecorresponding C statement?
sll $t0, $s0, 2 # $t0 = f * 4
add $t0, $s6, $t0 # $t0 = &A[f]
sll $t1, $s1, 2 # $t1 = g * 4
add $t1, $s7, $t1 # $t1 = &B[g]
lw $s0, 0($t0) # f = A[f]
addi $t2, $t0, 4
lw $t0, 0($t2)
add $t0, $t0, $s0
sw $t0, 0($t1)

我的答案是B[4g]=A[4f+4]+f 總覺得怪怪的 想確認對不對
星之城
EE

本帖最後由 39475494 於 2020-9-23 14:21 編輯

我沒有很熟
大致看了一下
1.
sub $t0,s3,s4
sll $t1, $t0, 2
lw $t0,t1($s6)
sw $t0,32($s7)

lw $t0,t0($s6)用同一個暫存器,不確定可不可以,問你的老師或同學吧

2.
因為 lw $s0, 0($t0) # f = A[f]
所以最後應該是 B[g] = A[f+1] + A[f]
功夫派~ 冰語

TOP

本帖最後由 22169751 於 2020-9-25 02:43 編輯

1.
想問一下為什麼要把(i-j)乘以4
sll $t1, $t0, 2這行

2.
同上 不懂這兩行的用意
sll $t0, $s0, 2 # $t0 = f * 4
sll $t1, $s1, 2 # $t1 = g * 4

是因為向左2bit 剛好對應word+1嗎?
可是這樣的話g為什麼沒+1
還有add為什麼變成取址的作用
星之城
EE

TOP

1. 因為位址定的是 1 個 byte
而一格是 1 word (4 byte)
所以要指到下一格,位址要 +4
2. 就是 *4,同 1.
g +1 在哪一行?
B[g] 的位址 在 B[0] 的位址往後數 g 個 word
所以要 + 4 * g
add 沒有取址的作用
add $t0, $s6, $t0       # $t0 = &A[f]
$t0 = $s6 + $t0 ,它只是加起來
但 $s6 裡面放的是A[0]的位置呀

sll $t0, $s0, 2            # $t0 = f * 4
add $t0, $s6, $t0       # $t0 = &A[f]
sll $t1, $s1, 2            # $t1 = g * 4
add $t1, $s7, $t1       # $t1 = &B[g]
lw $s0, 0($t0)            # $s0 = A[f]
addi $t2, $t0, 4          # $t2 = &A[f] + 4 = &A[f+1]
lw $t0, 0($t2)            # $t0 = A[f+1]
add $t0, $t0, $s0       # $t0 = A[f+1] + A[f]
sw $t0, 0($t1)           # B[g] = A[f+1] + A[f]
功夫派~ 冰語

TOP

返回列表