How to print .word value that is in the memory in Assembly without using la/li

By : Lin
Source: Stackoverflow.com
Question!

How do I print .word value that is in the memory in Assembly without using la/li? I can only use the basic functions. I can print .asciiz, but I don't know what I am doing wrong that it is not printing the values (integer) in the memory addresses :(

Here is my code for printing out my asciiz:

lui $a0, 0x1001
addi $a0, $a0, 12 # set the address to my string location
addi $v0, $0, 4 
syscall

I wrote the same code except with different addresses but it is not printing out anything. I also tried to search but couldn't find exact answer to this question.

Please advise. Any help will be greatly appreciated. Please correct me if I have misunderstood anything. Thank you in advance.

By : Lin


Answers

There are a couple of errors in your code.

First, if you want to print an integer you should use syscall #1 instead of #4

Then, if you want to print an integer which is stored on memory, you have to load that word from memory.

Your code should look something like this:

  lw $a0, 0x100C   # Load the contents of word stored at address 0x1000   12
  addi $v0, $0, 1  # Set service #1 (which prints an integer)
  syscall          # Do the system call
By : gusbro


This video can help you solving your question :)
By: admin