Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing newline in MIPS

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?

like image 583
gzg Avatar asked Mar 26 '12 15:03

gzg


4 Answers

I came here trying to find the answer for the same question you asked. It's been a while since you asked this question. Let me answer it anyhow for anyone that might look this feed in the future.

Everything else is good in your code, except that "space" is a reserved word in Mips. I think it is used to create arrays. So, if you replace space with some other word, I used "newline". It works the way it is supposed to.

.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
like image 122
nabin Avatar answered Sep 17 '22 08:09

nabin


This code prints a newline using the ASCII value 10 and syscall 11, the service to print a character:

.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
like image 29
user17001604 Avatar answered Sep 19 '22 08:09

user17001604


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
like image 24
John Avatar answered Sep 21 '22 08:09

John


try this.. it works for me

     .data
newLine  .asciiz "\n"

     .text
     (your code)

     la     $a0, newLine
     addi   $v0, $0, 4
     syscall
like image 26
Chon Avatar answered Sep 21 '22 08:09

Chon