首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在程序集中排序,气泡

在程序集中排序,气泡
EN

Stack Overflow用户
提问于 2017-11-29 12:18:19
回答 1查看 437关注 0票数 0

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

代码语言:javascript
复制
mov EBX, [ESI]      ;EBX = array[i]   ;;BREAKPOINT ERROR IS THROWN HERE

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

代码语言:javascript
复制
.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
EN

回答 1

Stack Overflow用户

发布于 2017-11-29 21:30:42

代码语言:javascript
复制
mov ecx, nbrElts                ;move counter to ecx for loop

forCount2:                      ;outer loop
    cmp ecx, nbrElts
    je end_loop

这是你的(部分*)答案,只需正确阅读。在第一次测试时,je end_loop总是正确的。

代码语言:javascript
复制
for (i = 0; i < n-1; i++)    

从值0开始,不与== n进行比较,而是与< n-1进行比较。

你必须决定,你是用ecx = number of loop做倒计时类型的循环,像5,4,3,2,1(在循环开始时没有特殊的测试是不可能的),还是从零开始索引,与最大值进行比较。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47544970

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档