Printing out integer in MIPS

edited March 2004 in Internet & Media
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!!!

Comments

  • LincLinc Owner Detroit Icrontian
    edited March 2004
    (bump back onto hot threads for a new member)

    Sorry mate, doesn't look like our knowledge base covers this... :-/
  • ShortyShorty Manchester, UK Icrontian
    edited March 2004
    If you can wait till Monday, I know someone at work who should be able to answer that question :)
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited March 2004
    I think your problem is that your code:
    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.
  • edited March 2004
    Thanks for the reply shwaip!!

    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?
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited March 2004
    are you trying to jump to a procedure later in the code? It would really help if you could attach the assembly file.
  • edited March 2004
    The code is not complete. What I want to do is at the beginning of main, jump to Constructor1 to set $s0 to 3 and then go to itsAge to set $s1 to $s0. This is sort of like a class in C++. Like I said before, I don't have Constructor1: or itsAge: in the code right now. Anyway, here is the code:


    .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.
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited March 2004
    Ok, I've never used MIPS to print things, but I've got a pretty good handle on what's going on:
    li $v0, 1                #preps for printing
    la $a0, whatToPrint #Loads the [b]Address[/b] of what to print
    syscall                  #does the actual printing
    

    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.
  • edited March 2004
    Thanks for your help. I have seen .word before but have never used it. I have a book "Computer Organization & Design: The Hardware/Software Interface" and it doesn't mention how to use .word as far as I can tell.
    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!!
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited March 2004
    it goes in the .data section

    if you want to store a single value, with a label
    LABEL: .word value
    
    if you want to store an array
    LABEL: .word value1,value2...
    

    you're just storing a value to memory.
  • edited March 2004
    I believe this will help. I really appreciate you taking time to help me out.
    THANKS!!
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited March 2004
    np :)

    If you have any more questions, feel free to post them.
Sign In or Register to comment.