MIPS代码集之间有什么区别?

时间:2016-09-29 04:23:56

标签: mips mips32

两个代码之间有什么区别?

.data            |  .data
A:  .word   0:100|  A:  .word   0:100
                 |
.text            |  .text
li  $t1, 4       |  la  $t1, A
lw  $t0, A($t1)  | lw    $t0, 4($t1)

1 个答案:

答案 0 :(得分:2)

让我们逐行理解代码:

从右

    .data         # static variables are defined in .data block
A:  .word   0:100 # array 100 of integers each initialized to 0 

    .text         # MIPS code block
li  $t1, 4        # loading a 16 bit constant value 4 into register $t1

lw  $t0, A($t1)   # going to the memory at the address of A (address
                  # of first element of array) + $t1 (which contains
                  # value 4) and fetch it into register $t0

<强>左

    .data         # static variables are defined in .data block
A:  .word   0:100 # array 100 of integers each initialized to 0 

    .text         # MIPS code block
la  $t1, A        # load the address of first element of array 
                  # into register $t1

lw $t0, 4($t1)    # going to the memory at the address of $t1 + 4 
                  # and fetch it into register $t0

注意: MIPS中的地址以字节为单位,整数(.word)为4个字节。因此,如果我们将数组的基址(数组的基址)增加4,它将导致数组的第二个元素的地址。

<强>因此: 这两个代码是相同的。最后,数组的第二个元素(index = 1)的内容将被加载到寄存器$t0中。