Search code examples
mipsmips32

What is the difference between the sets of MIPS codes?


What is the difference between two codes?

.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)

Solution

  • Lets understand the code line-by-line:

    Right:

        .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
    

    Left:

        .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
    

    Notes: Addresses in MIPS are in bytes and integers (.word) are 4 bytes. Hence, if we increment base addresses of array (base address of array) by 4, it will result in address of second element of array.

    Therefore: The two codes are identical. In the end, content of second element of array (at index = 1) will be loaded into register $t0.