1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Bubble Sort in MIPS

Discussion in 'Assembly Language Programming (ALP) Forum' started by final_semester, Oct 7, 2009.

  1. final_semester

    final_semester New Member

    Joined:
    Sep 23, 2009
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    I have the C program for it but I need help converting it in MIPS assembly code.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include<conio.h>
     
    #define ARRAY_SIZE 10
     
    void main()
    {
      int iarray[ARRAY_SIZE];
      int x, y, holder;
    
      clrscr();
    
      printf("Enter 10 Array Elements :");
      for(x=0;x<ARRAY_SIZE;x++)
         scanf("%d",&iarray[x]);
    
      printf("\nBefore Sort\n---------------\n");
      for(x=0;x<ARRAY_SIZE;x++)
         printf("%d ",iarray[x]);
     
     
      //Bubble sort method
      for(x = 0; x < ARRAY_SIZE; x++)
        for(y = 0; y < ARRAY_SIZE-1; y++)
          if(iarray[y] > iarray[y+1])
          {
            holder = iarray[y+1];
            iarray[y+1] = iarray[y];
            iarray[y] = holder;
          }
     
      printf("\n\nAfter Sort\n---------------\n");
      for(x=0;x<ARRAY_SIZE;x++)
         printf("%d ",iarray[x]);
      getch();
    }
    this is what I have so far

    Code:
    .data
    array: .word 10, 8, 1, 5, 2, 9, 4, 3, 7, 6
    length: .word 10
    
    main:    la$    t3,    array                    # put address of input into $t3
            
            li $t2,    6                        # put the index into $t2
            
            add     $t2,    $t2,     $t2     # double the index
            
            add     $t2,    $t2,     $t2     # double the index again (now 4X)
            
            add        $t1,    $t2,    $t3        # combine the two components of address
    
            lw        $t4,    0($t1)            # get the value from array cell
        
            li        $v0,    1
            
            move    $a0,    $t4
            
            syscall
    
    exit:    li        $v0,    10
            
            syscall
    
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Many compilers have a setting to generate assembly output, why not check if your MIPS compiler has one of those, then you can use the compiler output for inspiration.
     

Share This Page