2014-09-05 176 views
0

您好,我是MIPS彙編語言的新手,我試圖編寫相當於B[8]=A[i-j]的變量f,g,h,i和j分配給寄存器$ s0,$ s1,$ s2, $ s3和$ s4。我假設數組A和B的基地址分別在寄存器$ s6和$ s7中。mips地址超出範圍

我的代碼

# Gets A[i-j] 
sub $t1, $s3, $s4 
sll $t1, $t1, 2 
add $t1, $s6, $t1 

lw $t0, 0($t1) 

# Set B[8] equal to above 
addi $t2, $s0, 8 
sll $t2, $t2, 2 
add $t2, $s7, $t2 

lw $t2, 0($t2) 
sw $t2, 0($t0) 

但是,這將引發一個運行時異常在0x0040000c:地址超出範圍00000000,有什麼建議?

+0

火起來的調試和回答以下問題:哪個指令是在地址0x0040000c?這些登記冊當時包含什麼內容?他們如何能夠包含這些內容? – moonshadow 2014-09-05 09:19:14

+0

您是否驗證過'$ s6'實際上包含有效的地址?你在's3'和'$ s4'中有什麼值? – Michael 2014-09-05 09:19:16

回答

2
# Gets A[i-j] 
sub $t1, $s3, $s4 
sll $t1, $t1, 2 
add $t1, $s6, $t1 

lw $t0, 0($t1) 

# Set B[8] equal to above 
addi $t2, $s0, 8    #this actually computes something like &B[f+8], not &B[8] 
sll $t2, $t2, 2 
add $t2, $s7, $t2 

lw $t2, 0($t2)    #this loads the value B[f+8] 
sw $t2, 0($t0)    #this stores B[f+8] to *(int *)A[i-j], which is probably not an address. 

如果您希望您的代碼以符合你的描述,你必須:

# Gets A[i-j] 
sub $t1, $s3, $s4 
sll $t1, $t1, 2 
add $t1, $s6, $t1 

lw $t0, 0($t1) 

# Set B[8] equal to above 
addi $t2, $zero, 8 
sll $t2, $t2, 2 
add $t2, $s7, $t2 

##deleted##lw $t2, 0($t2) no need to load B[8] if you just want to write to it 
sw $t0, 0($t2) #this stores A[i-j] to &B[8]