(x86 MASM Intel)需要在我的汇编代码中解释气泡排序 [英] (x86 MASM Intel) Need explain in my assembly code for bubble sort

查看:82
本文介绍了(x86 MASM Intel)需要在我的汇编代码中解释气泡排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有用于冒泡排序的汇编代码,但不太确定mov edx,0和mov ebp,0对我的程序有何贡献,我只是随机测试并能正常工作,但idk为什么,希望你们能帮忙

so I have my assembly code for bubble sort but not quite sure how mov edx,0 and mov ebp, 0 contributes to my program, I just randomly test and it works but idk why, hope you guys can help

.686
.model flat

.code

_sort PROC

    push ebp
    mov ebp,esp             ; stack pointer to ebp
    
    mov ebx, [ebp+8]        ; address of first array element
    mov ecx, [ebp+12]       ; size of the array
    mov edx, [ebp+16]       ; (0 for ascending and 1 for descending)

    dec ecx                 ; ecx = size - 1
    mov eax, 0              ; i = 0
    cmp edx, 0              ; compare type of sort with 0 
    je AscenOuterLoop       ; if it's 0 then do ascending sort
    jg DescenOuterLoop      ; if it's 1 then do descending sort
    
AscenOuterLoop: 
    cmp eax, ecx            ; compare eax and ecx
    je Done                 ; loop till the end of array
    add eax, 1              ; i++
    mov edx, 0               
    mov ebp, 0              

DescenOuterLoop:    
    cmp eax, ecx            ; compare i and size - 1
    je Done                 ; i = size - 1, we finish
    add eax, 1              ; i++
    mov edx, 0              
    mov ebp, 0              

AscenInnerLoop:
    cmp ebp, ecx            ; compare j and size - 1
    je AscenOuterLoop           ; j = size - 1, exit the inner loop
    mov esi,[ebx+edx]       ; esi = a[j]
    cmp esi, [ebx+edx+4]    ; compare a[j] and a[j+1]
    jg AscSwapElements      ; if a[j] > a[j+1], swap a[j], and a[j+1]
    jmp AscIncreaseIndex    ; if a[j] <= a[j+1], do nothing, increase index

AscSwapElements:            
    mov edi, [ebx+edx+4]    ; edi = a[j+1]
    mov [ebx+edx], edi      ; a[j] = edi = a[j+1]
    mov [ebx+edx+4], esi    ; a[j+1] = esi = a[j]       

AscIncreaseIndex:
    inc ebp                 ; j++
    add edx, 4              ; increase the index in byte by 4
    jmp AscInnerLoop        ; continue inner loop


DesInnerLoop:
    cmp ebp, ecx            ; compare j and size - 1
    je DescenOuterLoop          ; j = size - 1, exit the inner loop
    mov esi,[ebx+edx]       ; esi = a[j]
    cmp esi, [ebx+edx+4]    ; compare a[j] and a[j+1]
    jl DesSwapElements      ; if a[j] < a[j+1], swap a[j], and a[j+1]
    jmp DesIncreaseIndex    ; if a[j] >= a[j+1], do nothing, increase index

DesSwapElements:            
    mov edi, [ebx+edx+4]    ; edi = a[j+1]
    mov [ebx+edx], edi      ; a[j] = edi = a[j+1]
    mov [ebx+edx+4], esi    ; a[j+1] = esi = a[j]           

DesIncreaseIndex:
    inc ebp                 ; j++
    add edx, 4              ; increase the index in byte by 4
    jmp DesInnerLoop        ; continue inner loop

Done:   
    pop ebp
    ret

_sort ENDP

END

我认为edx和ebp就像[]一样,是i,j值的地址,但不知道它们有助于使排序工作.

I think that edx and ebp is the address for i,j value just like the [] would do, but don't know they contribute to make the sort work.

推荐答案

我认为 edx ebp 是i,j值的地址,就像[]一样,但不知道它们有助于做出排序工作.

I think that edx and ebp is the address for i,j value just like the [] would do, but don't know they contribute to make the sort work.

OuterLoop计数器 i EAX 寄存器中.
InnerLoop计数器 j EBP 寄存器中.

The OuterLoop counter i is in the EAX register.
The InnerLoop counter j is in the EBP register.

每次InnerLoop启动时,其关联的计数器 j 都将 mov ebp,0 重置.

At every start of the InnerLoop, its associated counter j is reset with mov ebp, 0.

由于该数组包含 dword 个大小的元素,因此引入了一个偏移量变量,以便能够处理该数组的dword元素.此变量还会在InnerLoop的每个开始时使用 mov edx,0 重置.

Because the array holds dword-sized elements, an offset variable was introduced so as to be able to address the dword elements of the array. This variable also gets reset at every start of the InnerLoop with mov edx, 0.

关于x86指令集的一件好事是,您不需要该单独的offset变量即可获取数组元素.如果使用缩放索引寻址模式,则可以直接从循环控制变量( EBP )寻址数组元素.

A nice thing about the x86 instruction set is that you don't need that separate offset variable to fetch the array elements. You can address the array elements right from the loop control variable (EBP) if you use a scaled-index addressing mode.

这是具有标度索引地址模式并且使用 EDX 作为InnerLoop计数器 j 的上升版本:

This is the ascending version with a scaled-indexing address mode and using EDX as the InnerLoop counter j:

AscOuterLoop: 
    cmp eax, ecx            ; compare eax and ecx
    je  Done                ; loop till the end of array
    add eax, 1              ; i++
    mov edx, 0              ; j = 0

AscInnerLoop:
    cmp edx, ecx            ; compare j and size - 1
    je  AscOuterLoop        ; j = size - 1, exit the inner loop
    mov esi,[ebx+edx*4]     ; esi = a[j]
    mov edi, [ebx+edx*4+4]  ; edi = a[j+1]
    cmp esi, edi            ; compare a[j] and a[j+1]
    jng AscIncreaseIndex    ; if a[j] <= a[j+1], do nothing, increase index
AscSwapElements:            ; if a[j] > a[j+1], swap a[j], and a[j+1]
    mov [ebx+edx*4], edi    ; a[j] = edi = a[j+1]
    mov [ebx+edx*4+4], esi  ; a[j+1] = esi = a[j]       
AscIncreaseIndex:
    inc edx                 ; j++
    jmp AscInnerLoop        ; continue inner loop


您的代码有两种破损方式:


Your code is broken in 2 ways:

  • 您需要将升序和降序版本完全分开.您不能让您的两个OuterLoop彼此陷入!
  • 您每次都通过OuterLoop不断比较数组的所有元素.BubbleSort背后的想法是,在OuterLoop的第1次迭代之后,最大/最小的数字在数组的远端,并且您不再需要处理该数组元素.
    随着OuterLoop的每次迭代,InnerLoop中的迭代次数应减少1.

您可以轻松解决这两个问题:

You can easily solve both problems:

    ...
    mov  ecx, [ebp+12]      ; size of the array
    mov  edx, [ebp+16]      ; (0 for ascending and 1 for descending)
    test edx, edx           ; compare type of sort with 0 
    jnz  DesOuterLoop       ; if it's NOT 0 then do descending sort
AscOuterLoop: 
    sub  ecx, 1             ; size - 1
    jbe  Done               ; loop till the end of array
    xor  edx, edx           ; j = 0
AscInnerLoop:
    ...

DesOuterLoop:
    sub  ecx, 1             ; size - 1
    jbe  Done               ; loop till the end of array
    xor  edx, edx           ; j = 0
DecInnerLoop:
    ...

这篇关于(x86 MASM Intel)需要在我的汇编代码中解释气泡排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆