Thread: MIPS Assembly Bubble Sort

  1. #1
    Registered User cornfeller's Avatar
    Join Date
    Sep 2011
    Location
    Orlando,Florida
    Posts
    10

    Question MIPS Assembly Bubble Sort

    Hi, I have to write some assembly code for a homework assignment in one of my classes. I'm not a very good programmer and not a cs major but they make me take this class regardless. The assignment is shown below. Any help would be grateful, thanks.

    Write an assembly program that reads a list of characters (letters from the alphabet) from the keyboard and writes the sorted list in ascending order, over and over again. Any lowercase letter should be translated to the corresponding uppercase letter, while other characters should remain unchanged.Use the ACII table. The max list of characters inputed is ten.
    EX. If the user enters [ZaYScBdxEW]
    The program should list [ABCDESWXYZ]

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Swing and a miss!

    This is not a "MIPS Assembly".

    You may get lucky and find someone here who might help you, but I rather doubt that.

    Search for a forum that does have members who "speak" "MIPS Assembly"; you are much more likely to find help at such a place.

    Soma

  3. #3
    Registered User cornfeller's Avatar
    Join Date
    Sep 2011
    Location
    Orlando,Florida
    Posts
    10
    yep i figured, thanks

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Indeed. In the meantime: moved to Tech Board.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    The first question is:
    Can you complete the assignment in a programming language of your choice?

    If you can write this in C (say), and know exactly how the program works, the task is well past 50% solved.
    Once you've got C code, you basically turn each line of C code into an assembler comment, and then write a few instructions to do the 'C' code.

    Or you cheat and find a C cross-compiler targeted to generate MIPS code

    But if you're still clueless on what "bubble sort" is, then you need to work on this part first before getting anywhere near MIPS
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User cornfeller's Avatar
    Join Date
    Sep 2011
    Location
    Orlando,Florida
    Posts
    10
    Well I know what the function of bubble sort does, I maybe should have mentioned that the assignment involves bubble sort in the original post. The instructor also expects it. I also have somewhat of a C code written for a bubble sort function, that is sorting numbers in order. If I didn't per say,I could easily look one up somewhere online. Im not really sure how to convert C code into assembly, but i guess that could be looked up as well. And the cross compiler sounds awesome, Ill look into that. I also forgot to mention the compiler I am using, its called PCSPIM. I would like to learn assembly just for the sake of this class, but then again i just want to pass with a descent grade! So ill do anything. Its not due till june 20th so I've got some time to try and figure it out.
    Thanks (laserlight) for moving to tech board!

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Check your compiler, it may have an "output assembly code" switch. Try -S.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User cornfeller's Avatar
    Join Date
    Sep 2011
    Location
    Orlando,Florida
    Posts
    10
    would it be easier to write it in c and then try and convert it?

  9. #9
    Registered User cornfeller's Avatar
    Join Date
    Sep 2011
    Location
    Orlando,Florida
    Posts
    10
    I've been working on this project here and I split it up into parts. Ive almost got 2 out 3! Ive gotten the program to take an input and return it (semi alphabetically sorted). Ill worry about the capitalization part later. Can anyone help me figure out why my bubble sort wont sort correctly?

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Maybe, but you would need to post your code first.
    Make sure it is well commented though (like every line). Posting your reference 'C' implementation as well would be good as well if you have one.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    And, Post your input and the output your program gives.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  12. #12
    Registered User cornfeller's Avatar
    Join Date
    Sep 2011
    Location
    Orlando,Florida
    Posts
    10
    Here's what I have so far.I've Commented it to the best of my ability, or at least to what i think is going on....
    Heres one test of the program(it semi-sorts)

    Please Enter Your Letters:
    dfgfdgdddfg

    Result: ddffddgddfg
    Code:
    ##############################################################################################################
    .data
    
    insert:
        .word 4
    
    string_ask:
        .asciiz "\nPlease Enter Your Letters:\n " ##address = string_ask
    
    string_return:
        .asciiz "\nResult: "                       ##address = string_return
    
    
    .text                                   ##enables text input/output
    
    main:
    la $a0, string_ask
    li $v0, 4
    syscall
    
    
    #-----------------------Read in String----------------------------------#
    
    li $v0, 8             ##Read in String
    move $s0, $a0         ##Copy to $s0
    syscall
    
    #-----------------------Sort the string---------------------------------#
    
    move $v0, $s0        ##Copy string to $v0 to pass to sort function
    jal sort             ##Sort the string
    
    move $s0, $v0        ##Save sorted string into $s0
    
    
    #---------------------Print the sorted string---------------------------#
    
    la $a0,string_return   ##
    li $v0, 4
    syscall
    
    move $a0, $s0
    li $v0, 4
    syscall
    
    li $v0, 10             ## return from main routine
    syscall
    .end main
    
    #####################################~END MAIN~###############################
    
    #----------------------Bubble Sort---------------------------------------#
    sort:
    move $t9, $v0             ## t9=Address of string
    lw $t1,insert             ## $t1 = insert
    li $t0, 0                 ## $t0 = 0 (i)
    
    loop1:
    beq $t1, $t0, exit        ## if $t0 == insert, exit
    addi $t4, $t0, -1         ## $t4(J) = counter-1
    
    loop2:
    bltz $t4, exit2        ## j < 0
    mul $t5, $t4, 2            ## $t5 = 2 * j
    
    add $t5, $t9, $t5          ## $t5 = a[2j]
    
    lb $t2, 0($t5)             ## $t2 = a[j]
    lb $t3, 1($t5)             ## $t3 = a[j+1]
    ble $t3, $t2, else         ## a[j+1] >= a[j]
    j swap
    
    else:
    addi $t4, -1                ## --j
    j loop2
    
    exit: 
    move $v0, $t9        ##Return $t9 (sorted string)
    jr $ra
    
    exit2:
    addi $t0, 1                ## ++i
    j loop1
    
    swap: 
    sb $t3, 2($t5)
    sb $t2, 1($t5)
    j loop2
    #########################################################################################################
    Last edited by Salem; 06-16-2012 at 11:54 AM. Reason: added code tags!

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Where do you actually say "read a string into this buffer"?

    Shouldn't you be passing the address of insert (I'm guessing this is your buffer) to one (or more) of those syscalls?

    Though part of your sort seems to treat insert as a length (guessing here)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #14
    Registered User cornfeller's Avatar
    Join Date
    Sep 2011
    Location
    Orlando,Florida
    Posts
    10
    I'm not entirely sure to be honest. I goes that is another question of mine.. What is insert actually for? Is it just the space designated for the input string? or what?

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    The importance of writing it in C first (or a very detailed pseudo-code)
    Code:
    main:
    # printf("\nPlease Enter Your Letters:\n ")
    la $a0, string_ask
    li $v0, 4
    syscall
    
    
    #-----------------------Read in String----------------------------------#
    # char buffer[40]
    # fgets(buffer,40,stdin)
    # TODO: does this syscall take a buffer length?
    li $v0, 8             ##Read in String
    move $s0, $a0         ##Copy to $s0
    syscall
    
    # get the length of valid data
    # does the syscall return a length, or append a \0 ?
    
    #-----------------------Sort the string---------------------------------#
    # sort(buffer,length)
    move $v0, $s0        ##Copy string to $v0 to pass to sort function
    jal sort             ##Sort the string
    If your starting point is working C code, then it's an easier job.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. convert c programm to mips assembly
    By mariakat in forum Tech Board
    Replies: 8
    Last Post: 02-14-2011, 08:39 PM
  2. Converting C into MIPS Assembly
    By clag in forum C Programming
    Replies: 5
    Last Post: 02-13-2010, 07:48 PM
  3. A question of the MIPS assembly program
    By ok_good in forum Tech Board
    Replies: 0
    Last Post: 05-03-2006, 10:13 PM
  4. bubble sort in assembly language using c++
    By lorenzohhh in forum C++ Programming
    Replies: 3
    Last Post: 04-15-2002, 09:06 PM
  5. bubble sort in assembly language!!!!!!
    By lorenzohhh in forum C++ Programming
    Replies: 1
    Last Post: 04-15-2002, 08:30 PM