2014-09-05 145 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]