Load adress into register

Beginner Question: If I know the memory-adress of some data, how do I load it into a register?
If I want to load a byte from memory for example, I probably have to use lb $d i($s) or lbu $d i($s) , wich loads it into $d but what about i($s)? What does that stand for? Where do I put the adress?

Hey there,

normally you have given something like:

# $a0 contains the start address of the board
start:
    # some code

If you have given the address of something in the buffer (memory) in a register (e.g. $a0) you can load the contents from this address like that:

# $a0 contains the start address of the board
start:
    # loading a byte from the given address 
    lb $t0 ($a0)
    # loading a halfword (2 bytes) from the given address 
    lh $t0 ($a0)
    # loading a word (4 bytes) from the given address 
    lw $t0 ($a0)

Now, we have the contents at the address of $a0 stored in $t0.

If you wanna write something to the buffer yourself, it would probably look like that:

.data

my_data:
    # load the integer 42 to the buffer
    .byte 42 

.text

# compute the address of my_data into $a0
la $a0 my_data
# load a byte from $a0
lb $t0 $a0

Now we reached the same point as before. We had our address in $a0 (because we computed it from the label “my_data” to $a0 and we loaded a byte from this address and $t0=42 now.

The i in lb $d i($s) is simply the offset from the in $a0 given address.
So

lb $t0 2($a0)

is the same as

addiu $a0 $a0 2
lb $t0 ($a0)

except that $a0 does not change.

4 Likes