题目:无符号数排序
内容:TAB开始的单元中存放N个字节无符号数,请按照从大到小排序后,存入DAT单元中。(注意:TAB数据保持不变)
TAB DB X1,……,XN
DAT DB N DUP(?)
要求:熟练掌握循环程序设计方法
这个属于非就地排序,我利用的是冒泡的思想——每增加一个值,冒泡一遍。
附上C++代码:
// Bubblesort.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include "pch.h"
#include <iostream>
using namespace std;
int a[100];
int b[100];
void swap(int i, int j)
{
int tmp;
tmp = b[i];
b[i] = b[j];
b[j] = tmp;
}
void BubbleSort(int n)
{
for (int i = 0; i < n; i++)
{
b[i] = a[i];
for (int j = i-1; j >= 0; j--)
{
if (b[j] < b[j + 1])
swap(j, j + 1);
else
break;
}
}
}
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
BubbleSort(n);
for (int i = 0; i < n; i++)
{
cout << b[i] << " ";
}
system("pause");
}
/*
5
1 6 -2 0 4
*/
汇编代码:
INCLUDE irvine32.inc
.data
TAB db 55,33,23,6,7,0,12,4,12,45
cnt equ ($-TAB)
org 20h
DAT db 10 dup(?)
sd db ?
.code
start:
call BubbleSort
xor esi,esi
xor eax,eax
mov ecx,cnt
print_again:
mov al,DAT[esi]
call writeint
call crlf
inc esi
loop print_again
exit
swap proc
xor eax,eax
mov al,DAT[ecx]
xchg al,DAT[ecx+1]
xchg al,DAT[ecx]
ret
swap endp
BubbleSort proc
xor esi,esi
xor ebx,ebx
xor eax,eax
sort_again:
cmp esi,cnt
jge over
;b[j]=a[i]
mov al,TAB[esi]
mov DAT[ebx],al
;j=i-1
mov ecx,ebx
dec ecx
;这个again是冒泡
again:
cmp ecx,0
jl next
mov al,DAT[ecx]
cmp al,DAT[ecx+1]
jge next
call swap
dec ecx
jmp again
next:
inc esi
inc ebx
jmp sort_again
over:
ret
BubbleSort endp
end start