mips .word function and differences between 2 codes

On MIPS, the size of a pointer to your data is .word, usually 32 bits. Furthermore, MIPS requires that your data is aligned to certain addresses depending on the type (i.e. size) of the data itself.

First let’s look at the pointers and data that you declare in your program’s .data section:

msg:       .word  msg_data       ; Declare a pointer: use the address of msg_data as value for msg
msg_data:  .asciiz "Hello world" ; Declare a zero-terminated string in memory

msg and msg_data can be considered labels or symbolic names for your data, and you can use these labels in your code to refer to your data.

Now for the two different ways of loading the pointer to your string. First way is by indirection:

la $t0, msg    ; Load address of .word 'msg' into $t0
lw $a0, 0($t0) ; Load pointer from address in $t0+0, this loads the pointer stored in 'msg' which is the address of 'msg_data'

The second way is by loading the address of the string directly:

la $a0, msg_data ; Load the address of 'msg_data' into $a0

You may also take a look at the la pseudo-instruction, a macro for a lui/ori instruction pair which is used to load a 32 bit immediate value into a register. That immediate value is the address of your address label, resulting in la, i.e. load address.

Leave a Comment