实验二——排序

纯属个人见解,欢迎指正

一.冒泡排序
首先初始化清空寄存器内容,R4从0x2400开始,放入定义好的数组(而不带冒号的标号可以在程序中找到代码位置的同时知道所表示的参数的大小),再定义一个末尾的数放入R5,因为是连续储存单元

R4等寄存器显示的就是地址
add.w #1,R4 就是地址加1 等同于inc

mov.w R4,R5 意思是将R4内容(数据)放入R5中
此时修改R5,等同于在修改R5
mov.b @R4,R5 是将R4的首地址中的内容放入R5
tst.w R8 意思是将R8与0相减
注意:
MSP430 储存是左高右低,即0x2401是MSB,0x2400是LSB

冒泡排序的实现
1.C语言实现


1. 算法步骤
比较相邻的元素。如果第一个比第二个大,就交换他们两个。

对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对。这步做完后,最后的元素会是最大的数。

针对所有的元素重复以上的步骤,除了最后一个。

持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要比较。

#include <stdio.h>
void bubble_sort(int arr[], int len) {
        int i, j, temp;
        for (i = 0; i < len - 1; i++)
                for (j = 0; j < len - 1 - i; j++)
                        if (arr[j] > arr[j + 1]) {
                                temp = arr[j];
                                arr[j] = arr[j + 1];
                                arr[j + 1] = temp;
                        }
}
int main() {
        int arr[] = { 22, 34, 3, 32, 82, 55, 89, 50, 37, 5, 64, 35, 9, 70 };
        int len = (int) sizeof(arr) / sizeof(*arr);
        bubble_sort(arr, len);
        int i;
        for (i = 0; i < len; i++)
                printf("%d ", arr[i]);
        return 0;
}

2.MSP430汇编语言实现

	.data
Array .byte 1,3,2,5,9,8,7,6,13,42,55,12,32,67,87,32,54,21,22   ;don't need :
Last  .byte 10
	.text
	;init register
	clr.b R4
	clr.b R5
	clr.b R6
	clr.b R7
	clr.b R8   
	clr.b R9   
	clr.b R10
	clr.b R11
	call #len	;count length
	mov.b R5,R8          ;count loop1,length->R8
	
loop1:
	mov.w R4,R10         ;put R4 in R10
	dec.w R8			 ;count loop1
	mov.b R5,R9          ;count loop2
	sub.b R11,R9         ;length - i
	inc R11              ;loop1 -> i
;	clr.w R9
loop2:
	mov.b @R10,R6      ;R4 first number address => a[i]-> R6
	inc R10			   ;a[i+1]
	mov.b @R10,R7	   ;a[i+1] -> R7
	cmp.b R7,R6        ;a[i]-a[i+1]
	jl notChange       ;a[i]<a[i+1] not change
	;Change
	mov.b R6,0(R10)   ;first number a[i]->a[i+1]
	dec.w R10		  ;R10 has increasae 1
	mov.b R7,0(R10)   ;a[i+1]->a[i]
	inc.w R10		  ;return
notChange:
	sub.b #1,R9          ;count loop2
	jne loop2            ;when R9=0 end loop2

	tst.w R8
	jeq end              ;end loop1 if R8 = 0
	jmp loop1

len:
	mov.w #Array,R4      ; R4 init address 0x2400 (first byte)
	mov.w #Last,R5       ;last byte address
	sub.w R4,R5          ;R5 store length - 1
	inc R5               ;length
	ret
end:
	jmp $  

结果
在这里插入图片描述

二.选择排序

1.C语言实现

void swap(int *a,int *b) //交換兩個變數
{
    int temp = *a;
    *a = *b;
    *b = temp;
}
void selection_sort(int arr[], int len)
{
    int i,j;

        for (i = 0 ; i < len - 1 ; i++)
    {
                int min = i;
                for (j = i + 1; j < len; j++)     //走訪未排序的元素
                        if (arr[j] < arr[min])    //找到目前最小值
                                min = j;    //紀錄最小值
                swap(&arr[min], &arr[i]);    //做交換
        }
}

2.MSP430汇编语言实现

	.data
Array .byte 1,3,2,5,9,8,6,4,21,22,23,24,12,11,80,81,15,20,31,32   ;don't need :
Last  .byte 10
	.text
	;init register
	clr.b R4
	clr.b R5
	clr.b R6
	clr.b R7
	clr.b R8
	clr.b R9
	clr.b R10
	clr.b R11
	clr.b R12
	call #len		     ;count length
	mov.b R5,R8          ;count loop1,length->R8
loop1:
	mov.w R4,R12
	mov.w R4,R10         ;put R4 in R10    
	dec.w R8			 ;count loop1
	mov.b R5,R9          ;count loop2
	sub.b R11,R9         ;length - i
	inc R11              ;loop1 + 1 = i
	mov.b @R10,R6        ;min
loop2:
	inc R10		       ;a[j],j = i+1
	mov.b @R10,R7	   ;a[j] -> R7
	cmp.b R7,R6        ;a[min]-a[j]
	jl notChange       ;a[min]<a[j] not change
	;Change min
	;mov.b R6,R12
	mov.b @R10,R6
	mov.b @R4,R12
	jmp Change
Change:
	dec R10
	mov.b R6,0(R4)   ;first number a[min]->a[j]
	inc R10
	mov.b R12,0(R10)   ;a[j]->a[min]
notChange:
	sub.b #1,R9          ;count loop2
	jne loop2            ;when R9=0 end loop2

	tst.w R8
	jeq end              ;end loop1 if R8 = 0

	inc R4
	jmp loop1

len:
	mov.w #Array,R4      ; R4 init address 0x2400 (first byte)
	mov.w #Last,R5       ;last byte address
	sub.w R4,R5          ;R5 store length - 1
	inc R5               ;length
	ret
end:
	jmp $

结果
在这里插入图片描述

	.data
Scores:	.byte 17,18,20,0,6,10,16,19,13,16,14,18,16,14
Last:	.byte 16
findnum: .byte 0
times:	.space 4
maximum:	.space 2

mode:	.space 2
	.text
main:
	clr.b R12	;findnum
	clr.b R13	;start address
	clr.b R14	;end address
	clr.b R15	;maximum number
	clr.b R10
	clr.b R9
	clr.b R8
	clr.b R7
	clr.b R6
	call #len	;calculate length
	clr.b R5
	mov.b R7,R5	;R5=len
	call #max
	mov.b R15,maximum	;max number
	call #histo
	mov.b R15,times	;record appear times

histogram:
	clr.b R5	;counter
	mov.b R7,R5	;R5=len
	mov.w R13,R11
	inc R10	;which number
loop1:	;each num appear times
	mov.b @R11,R12
	mov.b @R11,R10
	call #histo
	push R10	;record a[i]
	push R15	;record a[i] times
	inc R11	;next number
	dec R5
	jne loop1

nmode:
	mov.w SP,R13	;stack address ->R13
	add.w #30,R13	;end address
	mov.w R13,R14
	sub.w #30,R13
	call #max
	mov.b R15,mode
	jmp $
max:
	mov.w R13,R4
	clr.w R15	;max
	mov.b @R4,R15	;first number->a[i](max)
	inc R4	;a[i+1]
	mov.b @R4,R6
	cmp.b R15,R6
	jl notchange	;R6<R15
	;change
	mov.b @R6,R15
notchange:

	dec R5	;len-1
	jne max

	ret

histo:
	mov.w R13,R9
	clr.w R15	;counter
	mov.b findnum,R12
loop:
	mov.b @R9,R8
	cmp.b R8,R12
	jne next	;R8!=R12
	inc R15	;counter+1
next:
	inc R9
	cmp.w R9,R14	;is end?
	jne loop
	jmp end
end:
	ret

len:
	mov.w #Scores,R13
    mov.w #Last,R14
    mov.w R14,R7
    sub.w R13,R7
    inc R7	;length
    ret

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值