Fixing MIPS Input/Output Error

  • Thread starter Snoogx
  • Start date
  • Tags
    Mips
In summary, I am having trouble 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.
  • #1
Snoogx
22
0
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:
	.data
set1:	.space 32

msg2: .asciiz "please enter a value: "

	.text
	.globl main
main:
	li $v0, 4                         #load value for print_string
	la $a0, msg2                   #load address of msg2 into $a0
	syscall                           #print msg2 to screen
	li $v0, 5                         #load value for read_int
	syscall                           #read inputted value
	move $a1, $v0                #move value into $a1
	la $a0, set1                    #load address of set1 into $a0
	#move $t2, $s1
	#sll $t2, $t2, 2                
	#add $t1, $t2, $a0
	sw $a1, 0($a0)               #store $a1 into first spot of $a0
	
	li $v0, 1                         #load value for print_int
	la $a0, set1                    #load address of set1
	#move $t2, $s1
	#sll $t2, $t2, 2
	#add $t1, $t2, $a0
	lw $a1, 0($a0)                #load contents of $a0 with offset 0 into $a1
	syscall                           #print $a1 to screen
	
	li $v0, 10                        #load value for exit
	syscall                            #exit program
 
Technology news on Phys.org
  • #2
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.
 
  • #3
That seems to be the problem, so change this line near the bottom
Code:
lw $a1, 0($a0)

to this:
Code:
lw $a0, 0($a1)

BTW, since the offset is 0, you can omit that 0, and just use this:
Code:
lw $a0, ($a1)
 
  • #4
Mark44 said:
That seems to be the problem, so change this line near the bottom
Code:
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 what's being printed to screen.

I always seem to get this output: 268500992
 
  • #5
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?
 
  • #6
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
 
  • #7
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.
Code:
    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
 
  • #8
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?
 
  • #9
Snoogx said:
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/KenVollmar/Mars/Help/SyscallHelp.html

The arguments are "$a0 = integer to print", so that's where you must put it.
 
  • #11
Yes, that was the problem. The syscall to print an integer expects the value to be displayed to be in register $a0.
 
  • #12
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.
 
  • #13
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.
 

Attachments

  • test.txt
    7.7 KB · Views: 641
  • #14
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...
 
  • #15
Edit: Never mind. I saved the file to WordPad, and then saved it to a file that my MIPS emulator (PCSmip) can run.
Mark44 said:
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...
 
  • #16
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.
 
  • #17
Mark44 said:
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 what's happening. Will continue...
 
  • #18
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.
 
  • #19
Mark44 said:
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.

Well the space is allocated like that because the program is supposed to be able to handle up to 32 integers (138/4 = 32) and set1 & set2 are half that (69*2 = 138) because I split the set in half.
 
  • #20
Snoogx said:
Well the space is allocated like that because the program is supposed to be able to handle up to 32 integers (138/4 = 32) and set1 & set2 are half that (69*2 = 138) because I split the set in half.

Your arithmetic is off -- 4 * 32 = 128, not 138. Also, you have .align 2. I would think you want to align on 4-byte boundaries, not just even boundaries.

For the purposes of testing, you could temporarily work with smaller arrays (say 8 bytes, 8 bytes, and 16 bytes), and a list of 4 numbers. Your program seems to be storing the input values in the right places (i.e., in the two smaller arrays). Check that the larger array ends up with the numbers in the right order. Then check that your code for displaying the contents of the larger array is working with the right address for each number in the array.

After you get the code working with the smaller arrays, then change the .space statements to 64, 64, and 128.
 
  • #21
Got it working, finally. Lots of small errors that were kicking me in the butt. Appreciate the 3rd perspective and all the help. :)
 

1. What is a MIPS input/output error?

A MIPS input/output error is an error that occurs when there is an issue with the input or output operations of a MIPS (Microprocessor without Interlocked Pipeline Stages) system. This can happen due to a variety of reasons, such as incorrect input data or faulty hardware.

2. How do I fix a MIPS input/output error?

The first step to fixing a MIPS input/output error is to identify the source of the error. This can be done by checking the input data and ensuring it is correct, as well as checking for any hardware malfunctions. Once the source of the error is identified, appropriate measures can be taken to resolve the issue, such as debugging the code or replacing faulty hardware.

3. Can a MIPS input/output error be prevented?

While some MIPS input/output errors may be unavoidable, there are steps that can be taken to prevent them. These include thorough testing of input data and ensuring hardware components are functioning properly. It is also important to follow best practices for programming in MIPS to minimize the risk of errors.

4. Do I need special tools to fix a MIPS input/output error?

In most cases, special tools are not necessary to fix a MIPS input/output error. Debugging tools and software may be helpful in identifying and resolving the issue, but they are not always required. Basic troubleshooting techniques and knowledge of MIPS programming can often be sufficient in fixing these errors.

5. Is a MIPS input/output error a common issue?

MIPS input/output errors can occur for a variety of reasons and can be common in certain systems or programs. However, with proper programming techniques and regular maintenance and testing, they can be minimized. If a MIPS input/output error does occur, it is important to address it promptly to prevent any potential negative impacts on the system or program.

Similar threads

  • Programming and Computer Science
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
4K
  • Programming and Computer Science
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
22
Views
4K
  • Programming and Computer Science
Replies
1
Views
6K
  • Programming and Computer Science
Replies
1
Views
7K
  • Programming and Computer Science
Replies
1
Views
8K
  • Programming and Computer Science
Replies
1
Views
7K
  • Programming and Computer Science
Replies
5
Views
9K
Back
Top