mips .word函数和2个代码之间的区别

mips .word function and differences between 2 codes

我是 MIPS 的新手,正在尝试了解以下两个汇编指令之间的区别和用途。在程序的开头我声明:

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

所以在 main 函数中以下两个都有效,但是每个的目的是什么?

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

另一个是:

la $a0, msg_data
li $v0, 4
syscall

在 MIPS 上,指向数据的指针的大小为 .word,通常为 32 位。此外,MIPS 要求您 data is aligned to certain addresses 取决于数据本身的类型(即大小)。

首先让我们看一下您在程序的 .data 部分中声明的指针和数据:

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

msgmsg_data可以被认为是标签符号名称,你可以在您的代码中使用这些标签来引用您的数据。

现在介绍将指针加载到字符串的两种不同方式。第一种方式是间接的:

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'

第二种方式是直接加载字符串地址:

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

您还可以查看 la pseudo-instruction,一个用于 lui/ori 指令对的宏,用于将 32 位立即值加载到寄存器中。该立即值是您的地址标签的地址,导致 la,即 加载地址