Printing newline in MIPS

gzg picture gzg · Mar 26, 2012 · Viewed 63.9k times · Source

I'm using MARS MIPS simulator and I want to print a newline in my program.

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

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

Instead of printing newline, it prints UUUU. What's that I'm doing wrong?

Answer

John picture John · Mar 3, 2014

If you're just trying to print a newline, it's simpler (and slightly more memory efficient) to do it using syscall 11 to print a single character.

.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