I'm required to do a 2-D bubble sort for an assignment in an asm course, but for whatever reason I cannot make it work. I can write one in 3 minutes in C++, but ASM just kicks my ass.
In this instance I'm using a C++ file to make the function call, and just using my .asm file to declare the function. I've got public _Sort and all that, that's not the problem. The C++ call is:
Sort(Names, LetterGrades, Averages, NumStudents-1);
... simple.
Right now, I just wrote the code to sort the names in the 2-D array. I'll work out sorting the other two arrays to synch up later... when I figure THIS out:
"Unhandled exception at 0x0041198f in Project.exe: 0xC0000005: Access violation reading location 0x00133006." <-- which occurs on this line: movzx edx, BYTE PTR[ebx + edi]. I've marked it in the code.
I have a feeling it's because I'm moving the offset down 11, and not down 11 BYTES, to get to the next row... or is there a difference?
I actually don't want a massive re-write (unless it saves much time/sanity) or a solution worked out for me, I'm [I]really[/I] attempting to learn this. I've just hit a brick wall and any sort of help would be greatly appreciated! (yes I know ebp referencing is lame, but... work with me it's what we're being taught and what we have to use).
_Sort proc ;STACK ARCHITECTURE
push ebp ;[ebp+24] NumStudents-1
mov ebp, esp ;[ebp+20] Averages[20]
;[ebp+16] LetterGrades[20]
push edi ;[ebp+8] Names[20][11]
push esi
mov ecx, [ebp+24]
xor ebx, ebx
;OUTER LOOP [execute NumStudents-1 times]:
OL:
xor edi, edi ;edi (column index) = 0
xor esi, esi ;esi (row index) = 0
;ROW OFFSETS LOOP [execute NumStudents-1 times]
RO:
mov eax, esi ;eax = row index
mov edx, 11 ;edx = 11
mul edx ;eax = row index * row size (1 * 11) = row offset
mov ebx, [ebp+8] ;ebx = Name offset
add ebx, eax ;ebx = Name offset + row offset
mov eax, ebx ;eax = row 1
add ebx, 11 ;ebx = row 2
;COLUMN OFFSETS LOOP
CO:
push ecx
movzx ecx, BYTE PTR[eax + edi] ;eax = row 1 value
movzx edx, BYTE PTR[ebx + edi] ;ecx = row 2 value
cmp ecx, edx ;cmp value 1 to value 2
pop ecx
jg SLSO ;value1 > value2, go to swap loop stopover
jl RI ;value1 < value2, go to row incriment loop
cmp edi, 11 ;check if row end has been reached...
je RI ;... if so jump to row incriment loop
inc edi ;if column is not at row end, and value1 = value2, ...
jmp CO ;... incriment column index, restart loop
;SWAP LOOP STOPOVER
SLSO:
xor edi, edi ;zeroes out edi for new column index
;SWAP LOOP
SL:
push ecx
push edx
movzx ecx, BYTE PTR[eax + edi] ;get a value from row 1
movzx edx, BYTE PTR[ebx + edi] ;get a value from row 2
xchg ecx, edx ;exchange values
pop ecx
pop edx
cmp edi, 11
je RI ;if column end is reached, jump to return loop
inc edi ;otherwise go to next column...
jmp SL ;... and restart swap loop
;ROW INCRIMENT LOOP
RI:
cmp esi, [ebp+24]
je EC
inc esi
jmp RO
;END CHECK
EC:
loop OL ;if OL has executed NumStudents-1 times, end
;END OF FUNCTION
pop esi
pop edi
pop ebp
ret
_Sort endp