# MIPS input/output

by Snoogx
Tags: input or output, mips
 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 $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   Mentor P: 20,433 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.  Mentor P: 20,433 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) P: 22 ## MIPS input/output  Quote by Mark44 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
 Mentor P: 20,433 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?
 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
 Mentor P: 20,433 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  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? P: 296  Quote by Snoogx 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. P: 22  Quote by Grep 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.  Mentor P: 20,433 Yes, that was the problem. The syscall to print an integer expects the value to be displayed to be in register$a0.
 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.
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
 test.txt (7.7 KB, 19 views)
 Mentor P: 20,433 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...
Mentor
P: 20,433
Edit: Never mind. I saved the file to WordPad, and then saved it to a file that my MIPS emulator (PCSmip) can run.
 Quote by Mark44 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...
 Mentor P: 20,433 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.
P: 22
 Quote by Mark44 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.
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 1, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.26450470089912415, "perplexity": 1021.0906962167628}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2014-10/segments/1394010934950/warc/CC-MAIN-20140305091534-00029-ip-10-183-142-35.ec2.internal.warc.gz"}