汇编语言小测试--期中篇(对寄存器,过程,循环,寻址的综合测试)

该博客详细介绍了如何使用汇编语言实现数组的冒泡排序、快速排序算法,并提供了偶数个数的判断方法。博主龙文汉在代码中展示了创建数组、显示数组的过程,同时对快速排序和冒泡排序进行了详细的步骤分解,最后还实现了对数组中偶数个数的统计。
摘要由CSDN通过智能技术生成

题目

对数组实现冒泡排序快速排序偶数个数判断

简单解析

主要实现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


总结(遇见的问题)

  1. can not use [arradd+ebx]
    应该通过寄存器作为中转站,问题应该是不能内存到内存
mov eax,left;同上,应该是寻址问题,不能内存到内存
mov ebx,right;
cmp eax,ebx;
  1. 数组的索引也是 [0~n-1]
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hanzoe_lwh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值