Register to reply

MIPS input/output

by Snoogx
Tags: input or output, mips
Share this thread:
Snoogx
#1
Mar31-11, 07:52 PM
P: 22
I am writing a bigger program, but having trouble right now seeing where my mistake is on the output side of the code. The snippet of code below prints a message to the screen, takes the users input (which is supposed to be an integer), then is supposed to print it back out to the screen. The code seems to work fine but when it prints out the integer to the screen it's not correct. It gives me a different, longer number. Maybe someone can shed some light to my dilemma. Thanks.

The code in orange (after the #) is omitted because I'm working this as an array in my bigger program.

PHP Code:
    .data
set1
:    .space 32

msg2
: .asciiz "please enter a value: "

    
.text
    
.globl main
main
:
    
li $v04                         #load value for print_string
    
la $a0msg2                   #load address of msg2 into $a0
    
syscall                           #print msg2 to screen
    
li $v05                         #load value for read_int
    
syscall                           #read inputted value
    
move $a1$v0                #move value into $a1
    
la $a0set1                    #load address of set1 into $a0
    #move $t2, $s1
    #sll $t2, $t2, 2                
    #add $t1, $t2, $a0
    
sw $a10($a0)               #store $a1 into first spot of $a0
    
    
li $v01                         #load value for print_int
    
la $a0set1                    #load address of set1
    #move $t2, $s1
    #sll $t2, $t2, 2
    #add $t1, $t2, $a0
    
lw $a10($a0)                #load contents of $a0 with offset 0 into $a1
    
syscall                           #print $a1 to screen
    
    
li $v010                        #load value for exit
    
syscall                            #exit program 
Phys.Org News Partner Science news on Phys.org
Wildfires and other burns play bigger role in climate change, professor finds
SR Labs research to expose BadUSB next week in Vegas
New study advances 'DNA revolution,' tells butterflies' evolutionary history
Mark44
#2
Mar31-11, 08:46 PM
Mentor
P: 21,214
I think that your problem is that you haven't loaded the integer you want to display in the right register. For the syscall to print an integer, $a0 should hold the integer. Instead, what is in $a0 is the address of your set1 block of memory.
Mark44
#3
Apr1-11, 12:52 AM
Mentor
P: 21,214
That seems to be the problem, so change this line near the bottom
lw $a1, 0($a0)
to this:
lw $a0, 0($a1)
BTW, since the offset is 0, you can omit that 0, and just use this:
lw $a0, ($a1)

Snoogx
#4
Apr1-11, 07:15 PM
P: 22
MIPS input/output

Quote Quote by Mark44 View Post
That seems to be the problem, so change this line near the bottom
lw $a0, 0($a1)
That gives me a Runtime error. "address out of range"
My understanding behind 'lw' is that is takes the second register (the source) and loads it into the first register. When I run the program step-by-step I can see that the 'lw' line is working and $a1 holds inputed value at the end, but its not whats being printed to screen.

I always seem to get this output: 268500992
Mark44
#5
Apr1-11, 07:31 PM
Mentor
P: 21,214
It worked for me using a MIPS simulator, and as I recall, that was the only change I made. If I entered, say, 8, in the input part, the program displayed 8 in the output part. The error you had was that $a0 has to hold the value being displayed, and you were putting it in $a1. Did you change anything else from what you first posted?
Snoogx
#6
Apr1-11, 09:32 PM
P: 22
No, I haven't changed anything from the code posted.
I tried switching the lw line like you said but I keep getting an "bad address" error.

Will keep messing with this, Thanks Mark44
Mark44
#7
Apr1-11, 11:20 PM
Mentor
P: 21,214
Sorry, my error. I actually changed two lines, but forgot that I had changed one of them. The 2nd and 3rd lines of the block of code below have been changed. This should work for you.
    li $v0, 1                       #load value for print_int 
    la $a1, set1                  #was la $a0, set1
    lw $a0, ($a1)                #was lw $a1, 0($a0) 
    syscall                         #print $a1 to screen 
     
    li $v0, 10                      #load value for exit 
    syscall                         #exit program
Snoogx
#8
Apr4-11, 11:24 PM
P: 22
Wow you're right that does work. So now this confuses me even more. All that was switched were the two registers $a0 and $a1. Why in my code did it not output the contents of $a1 which should have been the inputed integer, but it works in your example where $a0 is the register holding the inputed integer?
Grep
#9
Apr5-11, 05:32 AM
P: 296
Quote Quote by Snoogx View Post
Wow you're right that does work. So now this confuses me even more. All that was switched were the two registers $a0 and $a1. Why in my code did it not output the contents of $a1 which should have been the inputed integer, but it works in your example where $a0 is the register holding the inputed integer?
Because the "print integer" call expects it to be in $a0.

Here's a page I found:

http://courses.missouristate.edu/Ken...scallHelp.html

The arguments are "$a0 = integer to print", so that's where you must put it.
Snoogx
#10
Apr6-11, 05:33 PM
P: 22
Quote Quote by Grep View Post
Because the "print integer" call expects it to be in $a0.

Here's a page I found:

http://courses.missouristate.edu/Ken...scallHelp.html

The arguments are "$a0 = integer to print", so that's where you must put it.
Thanks Grep. Didn't realize this.
Mark44
#11
Apr7-11, 02:19 PM
Mentor
P: 21,214
Yes, that was the problem. The syscall to print an integer expects the value to be displayed to be in register $a0.
Snoogx
#12
Apr7-11, 08:28 PM
P: 22
Yes, I read through the list grep gave. Apparently i've been misusing registers lol. MIPS is a little different from what I'm used to.
Snoogx
#13
Apr9-11, 02:31 PM
P: 22
Ok one more time guys. I've compiled everything and it runs, but for output all I get are zeros. I'm not sure where my error is, I was hoping to get a fresh pair of eyes. :)

The purpose of the program is to take an even number of user inputed integers and sort them (using merge sort). It prints the sorted list to the screen. The program first takes the set size, then asks for the integers. It splits them into two even separate sets, and from here sorts them in ascending order. When both sets are sorted, the program will take merge both sets together in a final sorted set. The final set is then printed to the screen.

My program is attached in the .txt file
And help is greatly appreciated.
Attached Files
File Type: txt test.txt (7.7 KB, 20 views)
Mark44
#14
Apr9-11, 07:40 PM
Mentor
P: 21,214
Please put your code directly in the same text box you use to reply to a post, preferably between [ code] and [ /code] tags. The text file you provided apparently uses different line-end codes than Windows recognizes, so your formatting comes out all mangled.
Thanks...
Mark44
#15
Apr9-11, 07:42 PM
Mentor
P: 21,214
Edit: Never mind. I saved the file to WordPad, and then saved it to a file that my MIPS emulator (PCSmip) can run.
Quote Quote by Mark44 View Post
Please put your code directly in the same text box you use to reply to a post, preferably between [ code] and [ /code] tags. The text file you provided apparently uses different line-end codes than Windows recognizes, so your formatting comes out all mangled.
Thanks...
Mark44
#16
Apr9-11, 10:57 PM
Mentor
P: 21,214
Your mergesort program is fairly long, given my small amount of experience at working with the MIPS instruction set. What I would do is work with a small amount of data, say 6 numbers. Figure out where they're getting stored in memory, and where the merged and sorted numbers are being stored. Before you display the sorted array, make sure that the numbers are all in sorted order. If they are, then you are probably not pointing to the right location in memory, or possibly you haven't set up the registers correctly for the syscall that prints an integer.
Snoogx
#17
Apr9-11, 11:32 PM
P: 22
Quote Quote by Mark44 View Post
Your mergesort program is fairly long, given my small amount of experience at working with the MIPS instruction set. What I would do is work with a small amount of data, say 6 numbers. Figure out where they're getting stored in memory, and where the merged and sorted numbers are being stored. Before you display the sorted array, make sure that the numbers are all in sorted order. If they are, then you are probably not pointing to the right location in memory, or possibly you haven't set up the registers correctly for the syscall that prints an integer.
Sorry, I use Notepad++
Also thought it would be easier to view it in notepad than on here, because it is long. I'm trying to work my way step by step with a set size of 2-4 elements to see whats happening. Will continue...
Mark44
#18
Apr10-11, 06:40 PM
Mentor
P: 21,214
One thing that jumps out at me is your declarations for the three arrays. The first two of them are allocated 69 bytes of storage, and the third is allocated 138 bytes. Since integers are 32 bits (four bytes), your should allocate a size for your first two arrays that is evenly divisible by 4, and definitely not an odd number. And of course, set3 should be allocated twice as much storage as either set1 or set2.

It might be useful to allocate, say, 8 or 12 bytes each for set1 and set 2, and 16 or 24 bytes for set3, and then keep an eye on these locations while your program runs.


Register to reply

Related Discussions
Two V input and output Introductory Physics Homework 0
Need 0-3v output from 0-12v input Electrical Engineering 6
Digital output and input Electrical Engineering 2
Output/input of inverter Electrical Engineering 6
Input/output question General Engineering 9