0

I had posed this question earlier and it was flagged as a duplicate, however I feel my code is different. I have modified my code to add a procedure for the bubble sort. My program correctly takes the user input into the array but generates and error when it attempts to compare indexes after the procedure call. Now I'm stuck. The breakpoint error is thrown within the "mainloop" in the bubble sort procedure..

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

Visual studio throws this exception. 0x00411BF7, access violation reading location 0x0000000004. Is this because I'm not pointing at the beginning of the array?

.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
Chris Snook
  • 19
  • 1
  • 6
  • 1
    Stack Overflow isn't a free code translation service. Right now your question is far too broad. If you have a *specific* question, please update your question to highlight that. – Jonathon Reinhart Nov 29 '17 at 04:21
  • *for bubble sort:* https://stackoverflow.com/questions/26318043/assembly-bubble-sort-for-sorting-string/26324630#26324630 and *for selection sort:* https://stackoverflow.com/questions/43293643/sort-an-array-with-emu8086-assembler/47426690#47426690 – Ahtisham Nov 29 '17 at 04:29
  • Your previous question didn't have any code for bubblesort, just a commented out `;call bubble_sort` and an empty `bubble_sort PROC` / `bubble_sort ENDP`. So you were exactly asking for an implementation of Bubble Sort, which the answer to https://stackoverflow.com/questions/17802947/bubble-sort-in-x86-masm32-the-sort-i-wrote-doesnt-work provides, so it's a duplicate because that answer exactly answers your previous question. – Peter Cordes Nov 29 '17 at 05:18
  • @Ahtisham: Those are x86-16 implementations for sorting 8-bit integers. The one I linked is 32-bit code for sorting 32-bit integers, which the OP could copy/paste and use. – Peter Cordes Nov 29 '17 at 05:19
  • 1
    This one is a debugging question because you've now attempted an implementation, so that's on topic (and not necessarily a duplicate unless your bugs turn out to be common ones). But it's not a [mcve]: add more details about how it fails, and what you see when you single-step through it in a debugger. e.g. where does it stop doing what you expect? – Peter Cordes Nov 29 '17 at 05:22
  • The cmp and xchng functions aren't exchanging the values, in fact no values are moved at all. The output displayed before and after the bubble sort loop has the same data. The program runs but the output is not correct. – Chris Snook Nov 29 '17 at 06:05
  • @PeterCordes If, you would review my new code I would greatly appreciate it. – Chris Snook Nov 29 '17 at 19:27

1 Answers1

0
mov ecx, nbrElts                ;move counter to ecx for loop

forCount2:                      ;outer loop
    cmp ecx, nbrElts
    je end_loop

This is your (partial *) answer, just read it properly. The je end_loop is always true, immediately at the first test.

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

Starts with value 0, and does not compare against == n, but < n-1.

You have to decide, whether you are doing count-down type of loop with ecx = number of loop, going like 5, 4, 3, 2, 1 (and 0 loops is impossible without special test at start of loop), or if you are going with index from zero upward, comparing against max value.

*) I didn't read your code, there may be more problems, this was just sort of first 5 lines I read.

Ped7g
  • 16,236
  • 3
  • 26
  • 63