1

I am quite new to MIPS and trying to understand the difference and the purpose between the two following assembly directives. In the beginning of the program I declare:

.data
   msg:       .word  msg_data
   msg_data:  .asciiz "Hello world"

so in the main function both of the following works, but what's the purpose of each one?

la $t0, msg
lw $a0, 0($t0)
li $v0, 4
syscall

and the other one is:

la $a0, msg_data
li $v0, 4
syscall
Jens
  • 8,423
  • 9
  • 58
  • 78
zipp
  • 23
  • 1
  • 2
  • 5
  • The first one loads a pointer from memory which could point anywhere and can be changed, the second one takes the string's address directly. – Jester May 20 '16 at 16:44

1 Answers1

1

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.

Community
  • 1
  • 1
Jens
  • 8,423
  • 9
  • 58
  • 78