汇编语言——练习题3.2

题目:无符号数排序

   内容: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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值