1

在浏览 MIPS 代码时,我有些困惑。代码如下所示

.data
 key: .ascii "key: "              # "key:  \n"
char: .asciiz "  \n"               
 .text
 .globl main
main:
 jal getchar     

la $a0, char                    # $a0 contains address of char variable (" \n")
  sb $v0, ($a0)                   # replace " " in char with v0, which is read_character (X) 
 la $a0, key                     # now a0 will contain, address of "key: "  "X\n"

我不明白的是加载地址指令是如何工作的。第一个 a0 包含 char 变量的地址。在下一行中,我们将 v0 的值存储在该位置。( )没有偏移量$a0,是否像 0( ) 中那样假定为 0 $a0?为什么只有 " " 空白空间被替换为 v0,而 "\n" 为什么不被替换?或者也可能是空格和 \n 字符都被 v0 替换。

其次,当我们在a0中加载key的地址时,之前的地址应该会被覆盖。a0 应该只包含 key 的地址,但从评论看来,这两个字符串是连接的。怎么会这样。

4

1 回答 1

1

sb在内存中存储一​​个字节。

详细回答您的问题:

there is no offset with ($a0), is that assumed to be 0 like in 0($a0)?

是的。

Why only the " " empty space is replaced with v0, and why not the "\n" get replaced?

sb只存储一个字节,在这种情况下是 address 上的字节char,它是一个空格。换行符是下一个字节。

or It may also have been the case that both the empty space and \n character get replced by v0.

不,只有一个字节。

a0 should have contained the address of key only, but from comment it seems that the two strings are concatenated. How does that happen.

是的,$a0 包含地址key,但字符串由空字符关闭。当你这样做

key: .ascii "key: "

由 表示的字节"key: "被放置在内存中,末尾没有空字符(因为使用了 .ascii)。接下来,该指令char: .asciiz " \n"将 的字节" \n"放在内存中前一个字节的后面。在这种情况下,它们是空终止的,因为使用了 .asciiz(而不是 .ascii)。因此,该地址key指向一个在换行符之后以空结尾的字符串。或者,key是字符串第一个字符的地址。

为了更清楚

.asciiz "abc"

.ascii "a"
.ascii "b"
.asciiz "c"

是相同的。

于 2012-10-02T09:35:23.173 回答