在MIPS中打印换行符

时间:2012-03-26 15:56:46

标签: mips system-calls

我正在使用MARS MIPS模拟器,我想在我的程序中打印换行符。

.data
space: .asciiz "\n"
.text

    addi $v0, $zero, 4  # print_string syscall
    la $a0, space       # load address of the string
    syscall

不打印换行符,而是打印UUUU。我做错了什么?

4 个答案:

答案 0 :(得分:12)

如果您只是尝试打印换行符,则使用系统调用11打印单个字符会更简单(并且内存效率更高)。

.text
main:   addi $a0, $0, 0xA #ascii code for LF, if you have any trouble try 0xD for CR.
        addi $v0, $0, 0xB #syscall 11 prints the lower 8 bits of $a0 as an ascii character.
        syscall

答案 1 :(得分:10)

我来到这里试图找到你问的同一个问题的答案。你问这个问题已经有一段时间了。无论如何,让我回答任何可能在将来看这个饲料的人。

除了" space"之外,其他所有代码都很好。是Mips中的保留字。我认为它用于创建数组。所以,如果你用其他单词替换空格,我使用"换行符#34;。它按照预期的方式工作。

.data
 newline: .asciiz "\n"
.text

li $v0, 4       # you can call it your way as well with addi 
la $a0, newline       # load address of the string
syscall

答案 2 :(得分:2)

在打印值的代码块之后初始化新行。

所以它写着:

 addi $v0, $zero, 4  # print_string syscall
    la $a0, space       # load address of the string
    syscall

.data
space: .asciiz "\n"
.text

答案 3 :(得分:1)

尝试这个..它对我有用

     .data
newLine  .asciiz "\n"

     .text
     (your code)

     la     $a0, newLine
     addi   $v0, $0, 4
     syscall