How do I print values (integer) from memory addresses?
I did it with ascii string value, but I can't get it to work with integer value :/

Here is my code for printing ascii string:

lui $a0, 0x1001
addi $a0, $a0, 4 
addi $v0, $0, 4 

I tried the following code with printing the integer:

lui $a0, 0x1001
addi $a0, $a0, 0  
addi $v0, $0, 1 

It prints out 9 digits (no clue what that 9 digits are...) I need to print out the value that is stored in that memory address 0x10010000 :/ but I can't manage to get it to work...

I am only allowed to use the basic ones, so I can't use la to print >.< or something like that.

Please help >.< Please correct me if I have used any term incorrectly or missed any threads (my searches didn't return anything helpful).

Thank you very much.

5 Years
Discussion Span
Last Post by Schol-R-LEA

Assuming that you're using one of the commonly used MIPS simulators such as SPIM or MARS, then the print integer system call expects the value to be printed to be in $a0, not a pointer to the value. You'll need to put the pointer to the memory location into a temporary variable and dereference it into $a0, rather than simply loading the pointer into $a0. Thus, you would want to write it as something like this:

lui $t0, 0x1001
addi $t0, $t0, 0
lw $a0, 0($t0)  
addi $v0, $0, 1 

BTW, just as a way of saving some trouble for you as a coder (by letting the assembler do the heavy lifting), does the MIPS assembler your using support the move , la and li pseudo-instructions? This would let you use a label for the address rather than forcing you to write the address out explicitly:

la $a0, myString
    move $v0, 4

Where myString is at some labelled data location.

Even better would be if the assembler allows named constants, as you could give a name for the syscall arguments. This thread discusses the use of equates to simplify coding, and gives the list of the most common constants used (in SPIM specifically; AFAIK, the MARS simulator's assembler does not support equates).

Edited by Schol-R-LEA: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.