题目
对数组实现冒泡排序,快速排序,偶数个数判断。
简单解析
主要实现5个过程,方便循环调用:
- 创建数组
- 显示数组
- 快速排序
- 冒泡排序
- 偶数判断
代码实现
;Program: 冒泡排序
;Author: 龙文汉
;Date: 2020.12.1
.386
.MODEL FLAT
ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD
INCLUDE io.h ; header file for input/output
cr EQU 0dh ; carriage return character
Lf EQU 0ah ; line feed
.STACK 4096 ; reserve 4096-byte stack
.DATA ; reserve storage for data
str1 BYTE "Please input the arry length:",cr,Lf,0;
str2 BYTE "The QuikSort begin:",cr,Lf,0;
str3 BYtE "After QuikSort:",cr,Lf,0;
str4 BYTE "Before QuikSort:",cr,Lf,0;
str5 BYTE "Please input the arry:",cr,Lf,0;
str6 BYTE "Create End:",cr,Lf,0;
str7 BYTE "The count of even-number is:",cr,Lf,0;
str8 BYTE "The BubblSort:",cr,Lf,0;
HH BYTE cr,Lf,0;
TT BYTE "**",cr,Lf,0;
arrylen DWORD 100 DUP(?);
arrya DWORD 100 DUP(?);
temp DWORD 100 DUP(?);
;QuikSort
mid DWORD 100 DUP(?);
key DWORD 100 DUP(?);
left DWORD 100 DUP(?);
right DWORD 100 DUP(?);
arradd DWORD 100 DUP(?);
.CODE ; start of main program code
;创建数组过程
CreateArry PROC NEAR32
push eax;
push ebx;
push ecx;
;输入数组长度
output str1;
input arrylen,100;
atod arrylen;
mov arrylen,eax;
mov ecx,arrylen;
output str5;
lea ebx,arrya;
;创建数组
Dowhile:
input temp,100;
atod temp;
mov [ebx],eax;
add ebx,4;
loop Dowhile;
output str6;
pop ecx;
pop ebx;
pop eax;
ret
CreateArry ENDP;
;显示数组过程
PrintArr PROC NEAR32
push eax;
push ebx;
push ecx;
lea ebx,arrya;
mov ecx,arrylen;
DowhilePrint:
mov eax,[ebx];
dtoa temp,eax;
output temp;
add ebx,4;
loop DowhilePrint
output HH;
pop ecx;
pop ebx;
pop eax;
ret
PrintArr ENDP;
;快速排序过程
;Get position
;param:arry address,left position,right position
;return edx
Position PROC NEAR32
push ebp;
mov ebp,esp;
push left;left address
push arradd;arry address
push right;right address
push key;
push eax;
push ebx;
push ecx;
mov eax,[ebp+8];
mov right,eax;
mov eax,[ebp+12];
mov arradd,eax;
mov eax,[ebp+16];
mov left,eax;
mov ebx,left;
add ebx,arradd;!!!!can not use [arradd+ebx]
mov eax,[ebx];不能直接
mov key,eax;
BeginWhile:
mov eax,left;同上,应该是寻址问题,不能内存到内存
mov ebx,right;
cmp eax,ebx;
jnl Endwhile;
RightWhile:
;left<righ and arr[right]>key
mov eax,left;
mov ebx,right;
cmp eax,ebx;
jnl EndRightWhile
mov eax,key;
mov ebx,right;
add ebx,arradd;
cmp [ebx],eax;
jng EndRightWhile;
sub right,4;
jmp RightWhile;
EndRightWhile:
mov eax,left;
add eax,arradd;
mov ebx,right;
add ebx,arradd;
mov ecx,[ebx];
mov [eax],ecx;
LeftWhile:
;left<right and arr[left]<=key
mov eax,left;
mov ebx,right;
cmp eax,ebx;
jnl EndLeftWhile
mov eax,key;
mov ebx,left;
add ebx,arradd;
cmp [ebx],eax;
jnle EndLeftWhile
add left,4;
jmp LeftWhile;
EndLeftWhile:
mov eax,right;
add eax,arradd;
mov ebx,left;
add ebx,arradd;
mov ecx,[ebx];
mov [eax],ecx;
jmp BeginWhile;
Endwhile:
mov eax,left;
add eax,arradd;
mov ebx,key;
mov [eax],ebx;
mov edx,left;
pop ecx;
pop ebx;
pop eax;
pop key;
pop right;
pop arradd;
pop left
pop ebp;
ret
Position ENDP;
;param: arry address,left position,right position
QuikSort PROC NEAR32
push ebp;保留当前栈底
mov ebp,esp;赋值当前的栈顶指针
push left;left address
push arradd;arry address
push right;right address
push mid;
push eax;
push ebx;
push edx;
;mov right,[ebp+8];get tright position
;mov arradd,[ebp+12];get arry address
;mov left,[ebp+16];get left position
mov eax,[ebp+8];
mov right,eax;
mov eax,[ebp+12];
mov arradd,eax;
mov eax,[ebp+16];
mov left,eax;
mov eax,left;
mov ebx,right;
cmp eax,ebx;
jnl Endpro
push left;
push arradd;
push right;
sub edx,edx;
call Position;
mov mid,edx;
pop right;
pop arradd;
pop left;
mov eax,mid;
sub eax,4;
mov ebx,mid;
add ebx,4;
push left;
push arradd;
push eax;
call QuikSort;
pop eax;
pop arradd;
pop left;
push ebx;
push arradd;
push right;
call QuikSort;
pop right;
pop arradd;
pop ebx;
Endpro:
pop edx;
pop ebx;
pop eax;
pop mid;
pop right;
pop arradd;
pop left;
pop ebp;
ret
QuikSort ENDP;
ProEve PROC NEAR32
push ebp;
mov ebp,esp;
push ebx;
push ecx;
push edx;
mov ecx,[ebp+8];
mov ebx,[ebp+12];
mov eax,0;
BeginJuage:
push eax;
mov eax,[ebx];
mov temp,2;
sub edx,edx;reset edx
idiv temp;
pop eax;
cmp edx,0;
jne Next
inc eax;
Next:
add ebx,4;
loop BeginJuage;
pop edx;
pop ecx;
pop ebx;
pop ebp;
ret
ProEve ENDP;
BubblSort PROC NEAR32
push EBP;
mov EBP,ESP;
push eax;
push ebx;
push ecx;
push edx;
mov ecx,[ebp+8];
dec ecx;
DoWhile1:
push ecx;
mov eax,[ebp+12];
Dowhile2:
mov ebx,[eax];
mov edx,[eax+4];
cmp ebx,edx;
jng NextWhile;
mov ebx,[eax];
xchg [eax+4],ebx;
xchg [eax],ebx;
NextWhile:
add eax,4;
loop Dowhile2;
pop ecx;
loop DoWhile1;
pop edx;
pop ecx;
pop ebx;
pop eax;
pop ebp;
ret
BubblSort ENDP;
_start:
;create arry
call CreateArry;
output str4;
call PrintArr;
output str3;
;QuikSort
lea ebx,arrya;
mov left,0;
mov eax,arrylen;
dec eax;
mov ecx,4;
imul ecx;
mov right,eax;
mov arradd,ebx;
;存放的都是地址0——个数*4
push left;
push arradd;
push right;
call QuikSort;
call PrintArr
;ProEve
lea ebx,arrya;
push ebx;
push arrylen;
call ProEve;
dtoa temp,eax;
output str7;
output temp;
output HH;
;BubblSort
lea ebx,arrya;
push ebx;
push arrylen;
call BubblSort;
output str8;
call PrintArr
INVOKE ExitProcess, 0 ; exit with return code 0
PUBLIC _start ; make entry point public
END ; end of source code
总结(遇见的问题)
can not use [arradd+ebx]
应该通过寄存器作为中转站,问题应该是不能内存到内存
mov eax,left;同上,应该是寻址问题,不能内存到内存
mov ebx,right;
cmp eax,ebx;
- 数组的索引也是 [0~n-1]