Why Is My MIPS Code Printing the Wrong Integer?

  • Thread starter Thread starter Snoogx
  • Start date Start date
  • Tags Tags
    Mips
Click For Summary

Discussion Overview

The discussion revolves around troubleshooting a MIPS assembly code snippet that is intended to read an integer input from the user and print it back to the screen. Participants are exploring issues related to register usage and memory addressing in the context of MIPS programming.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related
  • Mathematical reasoning

Main Points Raised

  • One participant describes their issue with the output of their MIPS code, noting that it prints an incorrect integer.
  • Another participant suggests that the problem may stem from not loading the integer into the correct register for printing.
  • A different participant proposes changing the register usage in the code to correct the output issue.
  • One participant reports encountering a runtime error after attempting suggested changes, indicating confusion about the behavior of the 'lw' instruction.
  • Another participant claims their modified code works correctly, leading to further questions about why the original code did not produce the expected output.
  • Several participants discuss the requirement for the syscall to print an integer to have the integer in register $a0, clarifying the expected register usage.
  • A participant mentions that their program is intended to sort user-inputted integers using merge sort, but they are still facing issues with outputting zeros.
  • Another participant advises on testing with a smaller dataset to identify where the sorting and merging process may be failing.

Areas of Agreement / Disagreement

Participants generally agree on the importance of correct register usage for the syscall to print integers, but there is no consensus on the specific reasons for the output issues encountered. The discussion remains unresolved regarding the final output of the sorting program.

Contextual Notes

Participants note potential issues with memory addressing and register setup, but these remain unresolved. There are also indications of formatting issues with code shared in text files, which may affect the clarity of the discussion.

Who May Find This Useful

This discussion may be useful for individuals learning MIPS assembly programming, particularly those encountering issues with register management and syscall usage.

Snoogx
Messages
22
Reaction score
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
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.
 
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)
 
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
 
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?
 
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
 
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
 
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?
 
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

  • #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. :)
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 1 ·
Replies
1
Views
8K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 1 ·
Replies
1
Views
7K
  • · Replies 1 ·
Replies
1
Views
8K
  • · Replies 5 ·
Replies
5
Views
10K
  • · Replies 1 ·
Replies
1
Views
6K