MIPS input/output

  1. 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.

    set1:   .space 32

    msg2: .asciiz "please enter a value: "

        .globl 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
  2. jcsd
  3. Mark44

    Staff: Mentor

    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.
  4. Mark44

    Staff: Mentor

    That seems to be the problem, so change this line near the bottom
    Code (Text):

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

    lw $a0, 0($a1)
    BTW, since the offset is 0, you can omit that 0, and just use this:
    Code (Text):

    lw $a0, ($a1)
  5. 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
  6. Mark44

    Staff: Mentor

    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?
  7. 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
  8. Mark44

    Staff: Mentor

    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 (Text):

        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
  9. 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?
  10. Because the "print integer" call expects it to be in $a0.

    Here's a page I found:


    The arguments are "$a0 = integer to print", so that's where you must put it.
  11. Mark44

    Staff: Mentor

    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.

    Attached Files:

  14. Mark44

    Staff: Mentor

    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.
  15. Mark44

    Staff: Mentor

    Edit: Never mind. I saved the file to WordPad, and then saved it to a file that my MIPS emulator (PCSmip) can run.
  16. Mark44

    Staff: Mentor

    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. 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...
  18. Mark44

    Staff: Mentor

    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. 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. Mark44

    Staff: Mentor

    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.
Know someone interested in this topic? Share this thead via email, Google+, Twitter, or Facebook

Have something to add?