我在MARS模拟器中运行以下MIPS代码:
add $t0, $zero, $zero # i = 0
add $t4, $zero, $zero # initialize the sum to zero
add $t5, $zero, $zero # initialize temporary register to zero
la $a0, array # load address of array
la $a1, array_size # load address of array_size
lw $a1, 0($a1) # load value of array_size variable
loop:
sll $t1, $t0, 2 # t1 = (i * 4)
add $t2, $a0, $t1 # t2 contains address of array[i]
sw $t0, 0($t2) # array[i] = i
addi $t0, $t0, 1 # i = i+1
add $t4, $t4, $t0 # sum($t4) = ($t4 + array[i])
slt $t3, $t0, $a1 # $t3 = ( i < array_size)
bne $t3, $zero, loop # if ( i < array_size ) then loop
add $t5, $a0, $zero # save contents of $a0 in temporary reg $t5
nop # done.
在机器代码中,bne
指令如下:00010101011000001111111111111001
。在这种情况下,立即数为:1111111111111001
等于:0xFFF9
。 MIPS将采用它,将其向左移位2(将其乘以4)并将其程序计数器转换为该数字。但是,0xFFF9
乘以4是0x3FFE4
。怎么可能? SLL
的程序计数器应为0x18而不是0x3FFE4
。我在这里缺少什么?
谢谢,
答案 0 :(得分:1)
这里有两点需要注意: