# Program: Bubble sort
# Language: MIPS Assembly (32-bit)
# Arguments: 5 unordered numbers stored in $2 ~ $6 reg
# Author: brant-ruan
# Date: 2016-03-10
# IDE: MARS 4.5
# P.S.
#   This program is one of my homework and the number of assembly instructions allowed is just 31.
#   If I can use all the instructions of MIPS, optimizations will be considered.
#   (E.g. The index() function should have been not necessary)
# Minds:
#   It is not a good idea to store numbers from reg to mem. However, if using reg to sort directly,
#   the program will be ridiculous for the regs can't be indexed as an array(Can they ?).
#  My friend told me another brilliant idea that there's no need to swap $2 and $3, you can just store them to mem in the right order!!! Thanks!
######################################################################################################
.data
.word
array:  0, 0, 0, 0, 0
.text
# initialize $2 ~ $6
    xor $2, $2, $2
    xor $3, $3, $3
    xor $4, $4, $4
    xor $5, $5, $5
    xor $6, $6, $6
    add $2, $2, 7
    add $3, $3, 4
    add $4, $4, 8
    add $5, $5, 13
    add $6, $6, 2
# store values to be sorted into mem
    sw $2, array+0
    sw $3, array+4
    sw $4, array+8
    sw $5, array+12
    sw $6, array+16

    xor $5, $5, $5  # i
    xor $6, $6, $6  # j
    xor $7, $7, $7  # max_num
    add $7, $7, 4   # max_num = 5 - 1 (the number of elements minus 1)
    j begin
###############################################################################
# Sort procedure
sort:
    # prototype: sort($2, $3)
    # if $2 > $3 then swap($2, $3) else return
    slt $4, $3, $2  # $4 is flag
    beq $4, 0, sort_ret_src
    xor $2, $2, $3
    xor $3, $2, $3
    xor $2, $2, $3
sort_ret_src:
    j sort_ret_dst
###############################################################################
# Generate index procedure
index:
    # For the data-type is word, we should generate 4*j as index
    # prototype: index($12)
    # if $30 is 0, store valid index in $9 and return to load_0, else store in $11 and return to load_1
    xor $31, $31, $31     # count
    xor $29, $29, $29     # valid index which will be returned
multiply:
    beq $12, $31, ret     # if count == $12, return
    add $29, $29, 4       #  $29 += 4
    add $31, $31, 1       #  count++
    j multiply
ret:
    beq $30, 1, ret_1     # judge return where
ret_0:
    xor $9, $9, $9
    add $9, $9, $29
    j load_0
ret_1:
    xor $11, $11, $11
    add $11, $11, $29
    j load_1
###############################################################################
begin:
    beq $5, $7, end     # if i == max_num, go to end
    xor $10, $10, $10
    add $10, $6, 1      # $10 is j+1
    xor $12, $12, $12   # $12 is parameter of 'index' function
    add $12, $12, $6    # deliver $6 (j) to $12
    xor $30, $30, $30   # tell index() to return to load_0
    j index
load_0:
    lw  $2, array($9)   # load array[j] to $2 ($9 is index of j)
    xor $12, $12, $12
    add $12, $12, $10   # deliver $10 (j+1) to $12
    xor $30, $30, $30
    add $30, $30, 1     # tell index() to return to load_1
    j index
load_1:
    lw  $3, array($11)  # load array[j+1] to $3 ($11 is index of j+1)
    j sort
sort_ret_dst:
    sw  $2, array($9)   # store $2 into array[j]
    sw  $3, array($11)  # store $3 into array[j+1]
    add $6, $6, 1       # j++
    sub $8, $7, $5      # $8 is (max_num - i)
    bne $6, $8, next    # if j < max_num-i, go to next
    add $5, $5, 1       # i++
    xor $6, $6, $6      # j = 0
next:
    j begin
end:
# reload sorted array to $2 ~ $6
    lw $2, array+0
    lw $3, array+4
    lw $4, array+8
    lw $5, array+12
    lw $6, array+16

Bubble Sort [ASM-MIPS]的更多相关文章

  1. Java中的经典算法之冒泡排序(Bubble Sort)

    Java中的经典算法之冒泡排序(Bubble Sort) 神话丿小王子的博客主页 原理:比较两个相邻的元素,将值大的元素交换至右端. 思路:依次比较相邻的两个数,将小数放在前面,大数放在后面.即在第一 ...

  2. Bubble Sort (5775)

    Bubble Sort Problem Description   P is a permutation of the integers from 1 to N(index starting from ...

  3. HDU 5775 Bubble Sort(冒泡排序)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  4. 2016 Multi-University Training Contest 4 Bubble Sort(树状数组模板)

    Bubble Sort 题意: 给你一个1~n的排列,问冒泡排序过程中,数字i(1<=i<=n)所到达的最左位置与最右位置的差值的绝对值是多少 题解: 数字i多能到达的最左位置为min(s ...

  5. 快速幂取模 POJ 3761 bubble sort

    题目传送门 /* 题意:求冒泡排序扫描k次能排好序的全排列个数 数学:这里有一个反序列表的概念,bj表示在j左边,但大于j的个数.不多说了,我也是看网上的解题报告. 详细解释:http://blog. ...

  6. 冒泡排序(Bubble Sort)

    常见的排序算法有Bubble Sort.Merge Sort.Quick Sort 等,所有排序算的基本法思想都是把一个无限大的数据规模通过算法一步步缩小,指导最后完成排序. 这里分享一下Buuble ...

  7. [算法] 冒泡排序 Bubble Sort

    冒泡排序(Bubble Sort,台湾另外一种译名为:泡沫排序)是一种简单的排序算法.它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.走访数列的工作是重复地进行直到没 ...

  8. HDU 5775 Bubble Sort (线段树)

    Bubble Sort 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5775 Description P is a permutation of t ...

  9. c++算法联系,冒泡排序,bubble sort,插入排序,insert sort,

    #include <iostream.h> #define  MAX 100 void dispaly(int a[],int n) {     for(int i=0;i<n;i+ ...

随机推荐

  1. C# 如何在Excel 动态生成PivotTable

    Excel 中的透视表对于数据分析来说,非常的方便,而且很多业务人员对于Excel的操作也是非常熟悉的,因此用Excel作为分析数据的界面,不失为一种很好的选择.那么如何用C#从数据库中抓取数据,并在 ...

  2. Dense.js - 响应式的视网膜(Rtina)图像支持

    Dense 是一款 jQuery 插件,它提供一个简单的方法为设备提供精密像素比的图像,为你的网站带来视网膜支持,清除模糊,图像更清晰.通过简单地包括 jQuery 插件的页面上,就能实现响应式的视网 ...

  3. Snabbt.js – 极简的 JavaScript 动画库

    Snabbt.js 是一个简约的 JavaScript 动画库.它会平移,旋转,缩放,倾斜和调整你的元素.通过矩阵乘法运算,变换等可以任何你想要的方式进行组合.最终的结果通过 CSS3 变换矩阵设置. ...

  4. BookBlock - 效果非常真实的书本翻页预览

    这个名为 BookBlock 的图片预览效果是一个书展示或网上书店的概念,已全屏打开3D页面导航网格的形式显示图书的详细信息.我们可以打开书预览的摘录,其中有一些细节的网格.对于图书预览,我们在使用 ...

  5. cocoapods安装完第三方类库后不生成workspace

    cocoapods的版本太低,更新cocoapod版本:sudo gem install cocoa pods

  6. 巩固一下:SpringMVC详细示例实战教程

    一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于SpringMVC的配置 1 2 3 4 5 6 ...

  7. Android Studio教程--Android Studio 2.1安装与配置

    1.下载Android Studio 去官网https://developer.android.com/studio/index.html下载最新版的Android Studio2.1(自备梯子) 或 ...

  8. 【代码笔记】iOS-把<br!>换成\n

    代码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. // ...

  9. mvc4 用NPOI导出Excel

    第一次做数据导出Excel,刚看到这个的时候,首先想到的就是新建一个excel的文件,然后将它下载,后来询问别人,他们都推荐使用NPOI,发现这个很好用,就用博客记录一下 1.引用NPOI包 2.导出 ...

  10. .NET读写Excel工具Spire.XlS使用(DataExport )

    Introduction E-ICEBLUE is developing office.net component, the main products include Spire.Doc, Spir ...