【问题标题】:Sorting in assembly, bubble在装配中分拣,冒泡
【发布时间】:2018-05-12 16:49:15
【问题描述】:

我之前提出过这个问题,它被标记为重复,但我觉得我的代码不同。我已经修改了我的代码以添加一个冒泡排序的过程。我的程序正确地将用户输入输入到数组中,但在过程调用后尝试比较索引时会生成错误。现在我被困住了。冒泡排序过程中的“mainloop”内抛出断点错误..

mov EBX, [ESI]      ;EBX = array[i]   ;;BREAKPOINT ERROR IS THROWN HERE

Visual Studio 抛出此异常。 0x00411BF7,访问冲突读取位置 0x0000000004。这是因为我没有指向数组的开头吗?

.586
.MODEL FLAT

INCLUDE io.h

.STACK 4096

.DATA
user_array    DWORD 10 DUP (?)
nbrElts        DWORD 10

welcomeLbl BYTE    "Array Bubble Sorting Program ", 0
wel_message BYTE "Please enter 10 unique integers", 0
prompt1 BYTE    "Please enter a number: ", 0
string  BYTE    40 DUP (?)

array_contentLbl BYTE "The values you entered: ", 0
array_val DWORD ?, 0ah, 0dh

resultLbl BYTE "Program will now sort the numbers", 0
waitMsg     BYTE "Please wait.....",0

.CODE

_MainProc PROC
output welcomeLbl, wel_message
lea ebx, user_array
mov ecx, nbrElts

forCount1:
    input   prompt1, string, 40        ; prompt user for a number
    atod    string                    ;convert to integer
    mov [ebx], eax
    add ebx, 4
    loop forCount1

lea ebx, user_array                ;ptr to array, index 0
mov ecx, nbrElts                ;move counter to ecx for loop

forCount2:                    ;loop to output array contents
    mov eax, [ebx]
    mov array_val, eax
    dtoa array_val, eax
    output array_contentLbl, array_val      
    add ebx, 4
    loop forCount2

output resultLbl, waitMsg
push ebx
push ecx
call bubble_sort

lea ebx, user_array                ;ptr to array, index 0
mov ecx, nbrElts                ;move counter to ecx for loop

forCount3:
    mov eax, [ebx]
    mov array_val, eax
    dtoa array_val, eax
    output array_contentLbl, array_val      
    add ebx, 4
    loop forCount3

    quit:        mov eax, 0            ;clear memory
                mov ebx, 0
                mov ecx, 0
                mov edx, 0
    ret
_MainProc ENDP

Bubble_sort PROC

;These registers must be restored at the end
push EBP
mov  EBP, ESP
push EBX
push ESI
push EDI

;EBP + 8    is the array
;EBP + 12   is the number of items in the array

mov ESI, [EBP+8]    ;ESI is the array
mov ECX, [EBP+12]   ;setting ECX to the number of items
xor EAX, EAX        ;Setting EAX to 0, it'll be our iterator

MainLoop:
    ;If 'i' >= the number of items, exit the loop
    cmp EAX, ECX
    jge EndLoop     

    ;If 'i' == 0, move to the next element
    cmp EAX, 0
    je IncreaseCounter

    ;If array[i-1] <= array[i], it means they are
    ;sorted, so move to the next element
    mov EBX, [ESI]      ;EBX = array[i]   ;;BREAKPOINT ERROR IS THROWN HERE
    mov EDX, [ESI-4]    ;EDX = array[i-1]
    cmp EDX, EBX
    jle IncreaseCounter

    ;else, swap array[i-1] with array[i]
    push [ESI]
    push [ESI-4]

    pop [ESI]
    pop [ESI-4]

    ;Move to the previous element in the array
    ;and decrease 'i'
    sub ESI, 4
    dec EAX

    ;Loop back to the top
    BackToMainLoop:
    jmp MainLoop

    ;Moving to the next element in the array
    ;and increasing 'i'
IncreaseCounter:
    inc EAX
    add ESI, 4
    jmp BackToMainLoop

EndLoop:

;Restoring the registers
pop EDI
pop ESI
pop EBX
pop EBP

RET
bubble_sort ENDP
END

【问题讨论】:

  • Stack Overflow 不是免费的代码翻译服务。现在你的问题太广泛了。如果您有具体问题,请更新您的问题以突出显示。
  • 您之前的问题没有任何冒泡排序代码,只是注释掉了;call bubble_sort 和一个空的bubble_sort PROC / bubble_sort ENDP。因此,您确切地要求实现冒泡排序,stackoverflow.com/questions/17802947/… 的答案提供了它,所以它是重复的,因为该答案完全回答了您之前的问题。
  • @Ahtisham:这些是用于对 8 位整数进行排序的 x86-16 实现。我链接的是用于对 32 位整数进行排序的 32 位代码,OP 可以复制/粘贴并使用它。
  • 这是一个调试问题,因为您现在已经尝试了一个实现,所以这是一个主题(不一定是重复的,除非您的错误被证明是常见的)。但它不是minimal reproducible example:添加更多关于它如何失败的详细信息,以及在调试器中单步执行它时看到的内容。例如它在哪里停止做你期望的事情?

标签: assembly


【解决方案1】:
mov ecx, nbrElts                ;move counter to ecx for loop

forCount2:                      ;outer loop
    cmp ecx, nbrElts
    je end_loop

这是您的(部分 *)答案,请正确阅读。 je end_loop 在第一次测试时始终为真。

for (i = 0; i < n-1; i++)    

从值 0 开始,不与 == n 比较,而是与 &lt; n-1 比较。

您必须决定,是否使用ecx = number of loop 进行倒计时类型的循环,例如 5、4、3、2、1(如果在循环开始时没有特殊测试,0 循环是不可能的),或者如果您使用从零向上的索引,则与最大值进行比较。

*) 我没有阅读您的代码,可能还有更多问题,这只是我阅读的前 5 行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多