純屬個人見解,歡迎指正
一.冒泡排序
首先初始化清空寄存器内容,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