【问题标题】:mips .word function and differences between 2 codesmips .word 功能和2个代码的区别
【发布时间】:2016-05-20 16:37:58
【问题描述】:

我对 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

【问题讨论】:

  • 第一个从内存中加载一个指针,该指针可以指向任何地方并且可以更改,第二个直接获取字符串的地址。

标签: assembly mips


【解决方案1】:

在 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,这是一个用于将 32 位立即数加载到寄存器中的 lui/ori 指令对的宏。该立即值是您的地址标签的地址,导致la,即加载地址

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-29
    • 1970-01-01
    • 1970-01-01
    • 2017-03-26
    相关资源
    最近更新 更多