在MIPS中打印换行符

11

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

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

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

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


对我来说很好用(使用MARS 4.1) - gusbro
我正在使用4.2版本,但它无法正常工作。 - gzg
1
尝试重新安装您的JRE,或者更好的方法是更新它。 - m0skit0
5个回答

19

我来到这里是为了寻找与你所问的同样问题的答案。自你提问以来已经有一段时间了。无论如何,让我回答这个问题,以供将来可能查看此反馈的任何人参考。

除了"MIPS"中的 "space" 是一个保留字用于创建数组外,您的代码其他部分都很好。因此,如果您将 "space" 替换为其他单词,例如我用的 "newline",它将按预期工作。

.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

17

如果你只是想打印换行符,使用系统调用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

这段代码使用 ASCII 值 10syscall 11 来打印换行符,syscall 11 是用来打印字符的服务。

.data
.text
.globl main
main:
    li $v0 11  # syscall 11: print a character based on its ASCII value
    li $a0 10  # ASCII value of a newline is "10"
    syscall

1
在打印值的代码块后初始化新行,使其显示为:

如下:

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

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

1
尝试这个... 对我有效。
     .data
newLine  .asciiz "\n"

     .text
     (your code)

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接