0

I am implementing Selection Sort on Assembly language using irvine32.inc and I have some how implemented it to the best of my capability can you check my Selection Sort function I tried to debug and it is giving me an extra zero after very array index so the last two sorted elements are not assigning themselves in the array.

Include irvine32.inc

.data
    a3 dword 20,12,10,15,2,62,45,100,300
    temp dword ?

.code
swap proc,val1:PTR dword,val2:PTR dword
    mov eax,0
    mov esi,val1
    mov edi,val2
    mov eax,[esi]
    xchg eax,[edi]
    mov [esi],eax
    ret
swap endp
Selection_Sort proc,A: PTR DWORD,n:DWORD
Local step:DWORD,min_index:DWORD,i:DWORD
    mov eax,0
    mov step,eax
    forin:
        call Display
        mov eax,step
        mov min_index,eax
        inc eax
        mov i,eax
        fori:
            mov esi,[A]
            mov edi,[A]
            imul ebx,i,type A
            add esi,ebx
            imul ebx,min_index,type A
            add edi,ebx

            mov eax,[edi]
            mov ebx,[esi]

            cmp ebx,eax
            jge L1
            mov eax,i
            mov min_index,eax
            L1:
                mov eax,n
                inc i
                cmp i,eax
                jl fori
            mov eax,step
            cmp eax,min_index
            je L2
            mov esi,[A]
            mov edi,[A]
            imul ebx,step,type A
            add esi,ebx
            imul ebx,min_index,type A
            add edi,ebx
            mov eax,[esi]
            mov ebx,[edi]
            mov [esi],ebx
            mov [edi],eax
            L2:
            inc step
            mov eax,n
            dec eax
            cmp step,eax
            jl forin
            ret
Selection_Sort endp
Display proc
    mov ECX,lengthof a3 ;Set loop times
    mov EDX,offset a3
     DisplayLoop:
     mov ax,[EDX]
     add EDX,2
     call writedec
     call crlf
     loop DisplayLoop
     call crlf
     ret
Display endp
main proc
    call display
    INVOKE Selection_Sort,ADDR [a3],lengthof a3
    call Display
exit 
main endp
end main
  • You have defined the array as `dword` (4 bytes each) but your `Display` uses `word` (2 bytes each). – Jester Nov 30 '22 at 22:09

0 Answers0