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