[理工] [計組]MIPS
看板Grad-ProbAsk作者ericzx70116 (PigJames)時間9年前 (2016/10/27 21:17)推噓10(10推 0噓 20→)留言30則, 5人參與討論串3/3 (看更多)
這個是題目
For the following C statement, what is the corresponding MIPS assembly code?
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
$7, respectively.
B[8] = A[i–j];
然後我在網路上找的答案說
sub $t0, $s3, $s4 # $t0 = i-j
sll $t0, $t0, 2 # $t0 = (i-j) * 4
lw $t1, 0($s6) # $t1 = A[0]
add $t1, $t1, $t0 # $t1 = &A[i-j] <--
lw $t1, 0($t1) # $t1 = A[i-j]
sw $t1, 32($s7) # B[8] = A[i-j]
為什麼 add $t1, $t1 ,$t0 會是 $t1 = &A[i-j]
而不是 $t1 = A[0] + (i-j)呢?
會有這個疑問是因為我在老師上課的投影片中看到類似的是,
題目
g = h + A[8];
$s1=g
$s2=h
$s3=A[0]
答案
la $s3, value # $s3 = &A[0]
lw $t0, 32($s3) # $t0=A[8]
li $s2, 1 # h=1
add $s1, $s2, $t0 # g=h+A[8]=1+9=10 <--
value: .word 1, 2, 3, 4, 5, 6, 7, 8, 9 # A[0]…A[8]
______________________________________________________
還有一個問題就是我不太了解 $s3 = A[0] << 這個意思是什麼意思,
老師上課說這表示 $s3 存的是 A[]的base address
可是如果$s3存的是address的話,上面的 g=h+A[8] 不就不成立了嗎?
因為$t0是 32($s3) 是 A[8]的address , address應該不可以跟數值相加才對不是嗎?
麻煩各位大大了,抱歉><
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.119.121.6
※ 文章網址: https://www.ptt.cc/bbs/Grad-ProbAsk/M.1477574265.A.350.html
※ 編輯: ericzx70116 (140.119.121.6), 10/27/2016 21:22:04
※ 編輯: ericzx70116 (140.119.121.6), 10/27/2016 21:24:54
推
10/27 22:11, , 1F
10/27 22:11, 1F
→
10/27 22:13, , 2F
10/27 22:13, 2F
推
10/27 22:16, , 3F
10/27 22:16, 3F
→
10/27 22:17, , 4F
10/27 22:17, 4F
→
10/27 22:18, , 5F
10/27 22:18, 5F
→
10/28 11:28, , 6F
10/28 11:28, 6F
推
10/28 22:22, , 7F
10/28 22:22, 7F
→
10/28 22:22, , 8F
10/28 22:22, 8F
→
10/28 22:22, , 9F
10/28 22:22, 9F
→
10/28 22:22, , 10F
10/28 22:22, 10F
推
10/28 22:26, , 11F
10/28 22:26, 11F
推
10/28 22:28, , 12F
10/28 22:28, 12F
→
10/28 23:35, , 13F
10/28 23:35, 13F

→
10/28 23:37, , 14F
10/28 23:37, 14F
→
10/28 23:37, , 15F
10/28 23:37, 15F
推
10/29 01:45, , 16F
10/29 01:45, 16F
→
10/29 01:46, , 17F
10/29 01:46, 17F
推
10/29 01:56, , 18F
10/29 01:56, 18F
→
10/29 08:35, , 19F
10/29 08:35, 19F
→
10/29 08:35, , 20F
10/29 08:35, 20F
→
10/29 08:40, , 21F
10/29 08:40, 21F
→
10/29 08:46, , 22F
10/29 08:46, 22F
→
10/29 08:46, , 23F
10/29 08:46, 23F
推
10/29 11:11, , 24F
10/29 11:11, 24F
→
10/29 11:11, , 25F
10/29 11:11, 25F
→
10/29 11:12, , 26F
10/29 11:12, 26F
推
10/29 11:43, , 27F
10/29 11:43, 27F
→
10/29 11:44, , 28F
10/29 11:44, 28F
推
10/29 11:47, , 29F
10/29 11:47, 29F
→
10/29 13:04, , 30F
10/29 13:04, 30F
討論串 (同標題文章)