加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

【代码】MIPS指令实现冒泡排序法

(2011-04-17 21:43:42)
标签:

mips

冒泡排序

杂谈

分类: 自己写的代码
注意:代码原创,转载注明出处

体系结构的作业,交了有段时间了,放上来吧~老师啊如果你看到了要注意这个真的是我自己写的,网上那些大几十行的不是我的,我这个短小精悍,输出是用逗号分开的,感动吧老师~

#############################
#       计科0806班          #         
#############################

.globl main
.data
sortarry:.word 15,23,14,25,60,10,9,11,60,40
seprate:.asciiz ","
.text
main:
 addi $t5,$zero,10    # size of arry
 addi $t4,$zero,9     #intialize first loop counter i=10
loop1:
 la   $t1,sortarry    #load base address of arry,t1 as the offset address of the count
 addi $t6,$zero,0     #intialize second loop counter j=0
loop2:
 lw   $t0,0($t1)      #initialize t0=arry[j] as the temp value
 lw   $t7,4($t1)      # t7=arry[j+1]
 sub  $t2,$t7,$t0     #t2=arry(j+1)-temp
 bgtz  $t2,skip2      # if temp<arry(j+1),goto skip2
    sw   $t7,0($t1)     #  arry[j]=arry[j+1]
    sw   $t0,4($t1)    #   arry[j+1]=temp
 skip2:
 addi $t6,$t6,1        #j=j+1 
 addi $t1,$t1,4
 sub  $t2,$t4,$t6      #t2=i-j
 bgtz  $t2,loop2    #if(j<i) back to loop2

 addiu $t4,$t4,-1        #i=i-1 
 lw    $a0,0($t1)
 li    $v0,1             #out put segment
 syscall
 la    $a0,seprate
 li    $v0,4 
 syscall
 bne  $t4,$zero,loop1

 la   $t1,sortarry        #loop1 over,output the last number
 lw    $a0,0($t1)
 li    $v0,1
 syscall

 li   $v0,10
 syscall 

==================================================================

这个C语言的冒泡排序也是原创,是为了参照这个写mips写的。不过大家的都大同小异,总之就是原创,谢谢!
#include <stdio.h>

int main(){
int sortarry[]={15,23,14,25,60,10,9,11,60,40};
for(int i=10;i>0;i--){
     for(int j=0;j<i;j++){
               int temp=sortarry[j];
         if(sortarry[j+1]<temp){
               sortarry[j]=sortarry[j+1];
               sortarry[j+1]=temp;
}
     }
printf("%d ",sortarry[i-1]);
}
printf("\n");
int t=0;
while(t<=9){
printf("%d ",sortarry[t]);
t++;
}
 return 0;
}

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有