【问题标题】:MIPS: Reading a string from command line argumentMIPS:从命令行参数读取字符串
【发布时间】:2013-06-03 06:27:56
【问题描述】:

我是大会的新手。 我在从命令行参数读取字符串时遇到了一些问题。

我想将第二个参数中的字符串 thisismymessage 读入缓冲区。

我想用SYSCALL,但不知道怎么用。

$ spim -f program.s file thisismymessage

【问题讨论】:

  • 您是否尝试过将$a0 用作argc$a1 用作argv
  • 如果你还需要答案,我在下面简单总结一下。

标签: assembly command-line-arguments mips computer-architecture spim


【解决方案1】:

这里有几行代码来说明你在问什么:

# $a0 = argc, $a1 = argv
# 4($a1) is first command line argv 8($a1) is second

main:
    lw $a0, 8($a1)       # get second command line argv
    li $v0, 4              # print code for the argument (string)
    syscall                # tells system to print
    li $v0, 10              # exit code
    syscall                # terminate cleanly

参数的数量在 $a0 中,您可以根据加载到临时寄存器中的整数值 (li) 检查参数的数量以进行验证。

命令行参数值 argv 存储在 $a1 中,可以通过加载单词来访问。一个字是 4 个字节,因此我们可以使用 0($a1) 访问 argv[0],使用 4($a1) 访问 argv[1] 等等。

在这种情况下,我们需要 argv[2],我们可以将单词 (lw) 从 8($a1) 加载到我们选择的任何寄存器中。在这种情况下,我将它加载到 $a0 中,因为我之后会直接打印它。

回顾一下:

# $a0 is argc, $a1 is argv
lw $t0, 8($a1)             # gets argv[2] and stores it in $t0

【讨论】:

    猜你喜欢
    • 2015-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-04
    • 1970-01-01
    • 2016-02-24
    • 1970-01-01
    相关资源
    最近更新 更多