簡體   English   中英

將C轉換為MIPS代碼(了解MIPS中的內存訪問)

[英]Translate C to MIPS code (understanding memory access in MIPS)

假設變量f和g分配給寄存器$ s0,$ s1。 假定數組A和B的基地址分別在寄存器$ s6和$ s7中。 以下是我編寫的用於翻譯A [2 *(f + g)] = B [B [16 + f / 2]]的MIPS代碼:

# accessing the correct address for A[2*(f+g)] 
line 1. add $t0, $s0, $s1    # $t0 = f + g
line 2. add $s6, $s6, $t0    # A[0] should update to A[(f+g)/4]
line 3. sll $s6, $s6, 3      # A[(f+g)/4] should update to A[(8*((f+g)/4)]

line 4. srl $s0, $s0, 1      # f = f/2
line 5. addi $s0, $s0, 16    # f = f/2 + 16
line 6. sll $s0, $s0, 2      # f = (f/2 + 16) * 4
line 7. add $s7, $s0, $s7    # B[0] should update to B[f/2 + 16] 

line 8. sll $s7, $s7, 2      # B[f/2 + 16] should update to (B[f/2 + 16]) * 4
line 9. add $t0, $s7, $0     # $t0 = (B[f/2 + 16]) * 4
line 10. sw $s6, $t0($s7)     # should be storing B[(B[f/2 + 16]) * 4] in A[2*(f+g)]

我想當您需要兩次訪問數組B內的內存位置時,我搞砸了。 有人可以幫忙嗎?

我相信正確的解決方案如下:

add $t0, $s0, $s1
sll $t0, $t0, 3
add $s6, $t0, $s6

srl $t1, $s0, 1
addi $t1, $t1, 16
sll $t1, $t1, 2
add $t2, $s7, $t1
lw $t3, 0($t2)
add t4, $s7, $t3
lw $t5, 0($t4)
sw $t5, 0($s6)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM