Printing out integer in MIPS
I don't want to hard code an integer to be printed out such as:
li $v0, 1 # system call code for print_int
la $a0, 5 # address of int to print
syscall # print integer
which prints out integer 5.
What I need to do is the following:
li $v0, 1 # system call code for print_int
la $a0, INT # address of int to print; this line goes to INT: where
# $s0 = 0 + 5 and returns to go to syscall to print integer
syscall # print integer
where the code below is located somewhere else in the code.
INT: add $s0, $zero, $t0 # where $t0 has 5 stored in it
I get errors when I run this code. I would really appreciate help with this.
THANKS!!!
li $v0, 1 # system call code for print_int
la $a0, 5 # address of int to print
syscall # print integer
which prints out integer 5.
What I need to do is the following:
li $v0, 1 # system call code for print_int
la $a0, INT # address of int to print; this line goes to INT: where
# $s0 = 0 + 5 and returns to go to syscall to print integer
syscall # print integer
where the code below is located somewhere else in the code.
INT: add $s0, $zero, $t0 # where $t0 has 5 stored in it
I get errors when I run this code. I would really appreciate help with this.
THANKS!!!
0
Comments
Sorry mate, doesn't look like our knowledge base covers this...
la $a0, INT
...
INT: add $s0, $0, $t0
is loading the address of the add instruction, not the result of the add instruction.
could you post the entirety of your code...I think I may know what's going on, but I'm not sure.
You are correct. My problem is with the code la $a0, INT. It doesn't like the INT; it only works if I hard code it: la $a0, 3.
I am looking for a way to print 3 without having to hard code it.
I am guessing the la doesn't work unless I hardcode the 3. I don't know. Any suggestions?
.data
#*********************************************************
# Class Cat
#*********************************************************
# Constructor of Cat
# void Cat::Cat(int initialAge)
Constructor1: addi $s0, $zero, 3
# GetAge, returns value of itsAge member
itsAge: add $s1, $zero, $s0
# void Cat::Meow()
Meow: .asciiz "Meow."
#End of Class Cat
#*********************************************************
#*********************************************************
Cookie1: .asciiz "Cookie is a cat who is "
Cookie2: .asciiz "Now Cookie is "
Purry1: .asciiz "Purry is a cat who is "
Purry2: .asciiz "Now Purry is "
YearsOld: .asciiz " years old."
NewLine: .asciiz "\n"
#*********************************************************
.text
.globl main
main: # Start of Main() program
li $v0, 4 # system call code for print_str
la $a0, Meow # address of string to print "Meow."
syscall # print the string
li $v0, 4 # system call code for print_str
la $a0, NewLine # address of string to print "\n"
syscall # print the string
li $v0, 4 # system call code for print_str
la $a0, Cookie1 # address of string to print "Cookie is a
# cat who is "
syscall # print the string
li $v0, 1 # system call code for print_int
la $a0, 3 # address of int to print 3 Cookies age
syscall # print the integer
li $v0, 4 # system call code for print_str
la $a0, YearsOld # address of string to print "years old."
syscall # print the string
li $v0, 4 # system call code for print_str
la $a0, NewLine # address of string to print "\n" New line.
syscall # print the string
li $v0, 4 # system call code for print_str
la $a0, Meow # address of string to print "Meow."
syscall # print the string
li $v0, 4 # system call code for print_str
la $a0, NewLine # address of string to print "\n" New line.
syscall # print the string
li $v0, 4 # system call code for print_str
la $a0, Cookie2 # address of string to print "Now Cookie is "
syscall # print the string
li $v0, 1 # system call code for print_int
la $a0, 5 # address of int to print 5 CookiesAge
syscall # print the integer
li $v0, 4 # system call code for print_str
la $a0, YearsOld # address of string to print "years old."
syscall # print the string
li $v0, 4 # system call code for print_str
la $a0, NewLine # address of string to print "\n" New line.
syscall # print the string
li $v0, 4 # system call code for print_str
la $a0, NewLine # address of string to print "\n" New line.
syscall # print the string
li $v0, 4 # system call code for print_str
la $a0, Meow # address of string to print "Meow."
syscall # print the string
li $v0, 4 # system call code for print_str
la $a0, NewLine # address of string to print "\n" New line.
syscall # print the string
li $v0, 4 # system call code for print_str
la $a0, Purry1 # address of string to print "Purry is a cat
# who is "
syscall # print the string
li $v0, 1 # system call code for print_int
la $a0, 1 # address of int to print 1 PurrysAge
syscall # print the integer
li $v0, 4 # system call code for print_str
la $a0, YearsOld # address of string to print "years old."
syscall
li $v0, 4 # system call code for print_str
la $a0, NewLine # address of string to print "\n" New line.
syscall # print the string
li $v0, 4 # system call code for print_str
la $a0, Meow # address of string to print "Meow."
syscall # print the string
li $v0, 4 # system call code for print_str
la $a0, NewLine # address of string to print "\n" New line.
syscall # print the string
li $v0, 4 # system call code for print_str
la $a0, Purry2 # address of string to print "Now Purry is "
syscall
li $v0, 1 # system call code for print_int
la $a0, 2 # address of int to print 2 PurrysAge
syscall # print the integer
li $v0, 4 # system call code for print_str
la $a0, YearsOld # address of string to print "years old."
syscall # print the string
li $v0, 4 # system call code for print_str
la $a0, NewLine # address of string to print "\n" New line.
syscall # print the string
# End of main() program
#Cat program output:
# Meow.
# Cookie is a cat who is 3 years old.
# Meow.
# Now Cookie is 5 years old.
# Meow.
# Purry is a cat who is 1 years old.
# Meow.
# Now Purry is 2 years old.
You're going to need to store the each of the cats age in memory if you want to print it. I would create a .word for each of the cats age. When you call the constructor, set the cat's age in memory, and then when you try to print it, it should work.
Anyway, I have seen it used as follows:
.word 2, 1, 3, 5, 4, etc.......
What exactly is the .word doing and what do the numbers mean?
Thanks again!!
if you want to store a single value, with a label if you want to store an array
you're just storing a value to memory.
THANKS!!
If you have any more questions, feel free to post them.