The MASM Forum

General => The Campus => Topic started by: bonbonbon on January 22, 2016, 02:36:14 AM

Title: Help making selection sort work
Post by: bonbonbon on January 22, 2016, 02:36:14 AM
Hey,

I'm quite new to programming and most certainly new to Assembler. I'd like to request some help from you.

I'm writing a program that does a selection sort on a text file containing numers (up to 1000 numbers).
It's not working as intended.

What I want the program to do: open the file "liczby.txt", read it, do a selection sort on it, display sorted data in console and finally save sorted data into new file called "wynik.txt"
What it actually does: opens the file "liczby.txt", reads it, doesn't do selection sort and just displays content as it was in the file "liczby.txt", saves unsorted data in "wynik.txt".

I'm attaching program code so you can look at it. I'm also attaching a sample input and output files "liczby.txt", "wynik.txt". I'm sorry if anything is hard to understand (comments aren't in English).

Some additional info:
"SortowaniePoprzezWybieranie" = "SelectionSort"
"Wyswietlenie" = "Display"


Any help appreciated.

bonbonbon
Title: Re: Help making selection sort work
Post by: jj2007 on January 22, 2016, 02:44:17 AM
First things first: Welcome to the forum :icon14:

I won't have the time to dig deep into the code, and non-English comments and variable names are a bit difficult to understand. Your program builds fine but crashes in line 199, where edi is zero:
         
mov eax, DWORD PTR [edi]    ; £adowanie do EAX wskaŸnik aktualnie przetwarzanych liczb
Title: Re: Help making selection sort work
Post by: MichaelW on January 31, 2016, 03:43:20 AM
I extracted the sort code and wrapped it in a small test app, and it appears to work as expected.

include \masm32\include\masm32rt.inc
.686
.data
    array_size  DD  10
    array       DD  4,7,1,6,9,8,0,2,5,3   
.code
Sort PROC
    mov ECX, array_size
    dec ECX
    xor ESI, ESI
  sort:   
    cmp ESI, array_size
    jge done
    mov EAX, array[ESI*4]
    mov EDX, ESI

    mov ECX, EAX
    mov EDI, ESI
    inc EDI
    mov EDX,ESI
  find_min:
    cmp EDI, array_size
    jge exchange
    mov EAX , array[EDI*4]
    cmp EAX, ECX
    jge new_min
    mov ECX, array[EDI*4]
    mov EDX, EDI
  new_min:
    inc EDI
    jmp find_min             
  exchange:
    mov EAX, array[ESI*4]
    mov array[EDX*4], EAX
    mov array[ESI*4], ECX
    inc ESI
    jmp sort       
  done:
    ret
Sort ENDP
start:   
    xor   esi, esi
    REPEAT 10
        printf("%d\t", array[esi])
        add   esi, 4 
    ENDM
    printf("\n")   
    call Sort   
    xor   esi, esi
    REPEAT 10
        printf("%d\t", array[esi])
        add   esi, 4 
    ENDM
    printf("\n")   
    inkey
    exit
end start 


4       7       1       6       9       8       0       2       5       3

0       1       2       3       4       5       6       7       8       9


I didn't analyze the code, but the multiple references to rozmiar_tablicy (array_size in my translation), and several other details, suggest that it's not very efficient. Also, for this code you should probably be using unsigned comparisons, for example jae instead of jge.  Also, if the value of rozmiar_tablicy is zero, the sort procedure will not sort.