Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confusion about load word (lw) vs load address(la) and offsets in mips assembly?

Tags:

assembly

mips

So I'm pretty new to assembly and I got so many questions. For example, if in the data segment I type this

.data

n:.word 4

And in the text segment

.text
lw $t0, n

Does $t0 now store the value 4 or the address of n? Because I know that if n were an array and if I type lw $t0 4(n) $t0 stores the first VALUE of n(the content). And if I type lw $t0 n then $t0 stores the address.

Also I am wondering if I were to set an offset of 4 to register $0 like this:

lw $t0 4($0)

would $t0 just hold 0?

like image 409
user9535888 Avatar asked Jan 02 '23 14:01

user9535888


1 Answers

lw load a word from memory.
lw $t0, n reads from the address of the symbol n.
lw $t0, 4($t1) reads from the address generated as $t1 + 4.
lw $t0, 0x10000 reads from the address 0x10000.

Apart from the second, all are pseudo-instructions.

la load an address.
la $t0, n puts the address of the symbol n in $t0.
la $t0, 4($t1) put the address generated as $t1 + 4 in $0.

These are all pseudo-instructions.

li load an immediate.
li $t0, 10000 puts the immediate 10000 in $t0.

This is a pseudo instruction.


The central point is that MIPS has 16-bit immediates (constants) for I-type instructions, so the real form of li and lw don't permit to move a value greater than 0x10000 or access and address above 0x10000.
The assembler gets around it by generating two or more instructions.
la isn't needed in theory, li could be used to load the address of a symbol since the said address is an immediate in this context but a specific mnemonic was introduced instead.

like image 74
Margaret Bloom Avatar answered Jan 05 '23 17:01

Margaret Bloom