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?
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
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
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
try this.. it works for me
.data
newLine .asciiz "\n"
.text
(your code)
la $a0, newLine
addi $v0, $0, 4
syscall
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With