1

I am just starting to understand MIPS, and the particular instruction "lw" confuses me.

From the thread, Understanding how `lw` and `sw` actually work in a MIPS program, this is what I have gathered:

If, say, we have:

lw a, 4(b) // a and b are registers

Then, it just means that we will get the data (or usually referred to a "word", which I still don't get why...) at the memory address of b incremented by 4. Then, store it in a.Simply in other words, getting the next data stored at the address right after the address of b since each "word" is 4 bytes.

Also, does this mean that the data stored in b should be a data structure, like an array? So that we can get the data at the memory address of b+4?

Sorry, I know my wording is quite confusing... but am I on the right track? And would anyone please explain to me in, simple terms, what exactly a "word" is? Is it basically a data?

Tina
  • 179
  • 1
  • 3
  • 13

1 Answers1

1

You are on the right track. A "word" is 4 bytes of data.

lw a, 4(b)

This instruction loads one word from the address in b with an offset of 4. It would make sense if on that address is a data structure like an "array" but it is not needed. Depends on what you want to achieve.

I will give you a short example:

.globl main

.data
    array: .word 1, 2, 3, 4, 5, 6,    # numbers

.text
main:
    addi $t0, $0, 0                 # p value in $t0
    addi $t1, $0, 0                 # array in $t1

    la $t1, array                      # load array into $t1
    lw $t0, 0($t1)                  # load first word into $t0 --> 1
    lw $t0, 4($t1)                  # load second word into $t0 --> 2

    addi $t1, $t1, 4                    # increase address  += 4
    lw $t0, 0($t1)                  # load first word into $t0 --> 2
    lw $t0, 4($t1)                  # load second word into $t0 --> 3

    addi $v0, $0, 10                    # load exit syscall 
    syscall                                     

Lets say the array starts on example address 1000 and we load this address into $t1 using the pseudoinstruction la. lw $t0, 0($t1) loads the first word into $t0 (loads data from example addresses 1000 - 1003). lw $t0, 4($t1) loads the second word into $t0 (loads data from example addresses 1004 - 1007).

You can use http://spimsimulator.sourceforge.net/ to debug the program. It will show you which address actually is in b.

Hope that helps.

AndiCover
  • 1,724
  • 3
  • 17
  • 38