Question

The problem with segmentation fault has been solved, however there still remains a question of faulty algorithm itself which sorts numbers correctly but puts some of the biggest ones on the top of the file. For example numbers:

 600196455
1215535209
1560271953
1491899466
2093817641
3618330810
 519782898
3779504611
1656881276
 670566484

after sorting become:

3618330810
3779504611
 519782898
 600196455
 670566484
1215535209
1491899466
1560271953
1656881276
2093817641

OLD I am trying to implement an assembler program to read 32bit numbers from a file(maximum 32768 numbers), sort them using selection sort and save them to a specified file. So far I have successfully written parts that read and write data however there is an error in my sorting algorithm which results in a segmentation fault.

I am using 32bit Fedora, i386 processor, NASM assembler.

Thanks

My code looks as following

section .text
global _start

_start:
    ; read from input
    mov eax, 3
    mov ebx, 0
    mov ecx, array
    mov edx, 4*32768
    int 80h

    mov [fileLength], eax
    shr eax, 2
    mov [numbersCount], eax
    mov [numbersCount1], eax
    dec dword [numbersCount1]

    ; sort
    mov ebp, array
    xor ecx, ecx
    for1:
            mov dword [min], ecx
            xor edx, edx
            mov edx, ecx
            inc edx
            for2:
                    xor esi, esi
                    mov esi, dword [min]
                    mov eax, dword [ebp+4*edx]
                    mov ebx, dword [ebp+4*esi]
                    cmp eax, ebx
                    jge if1
                    mov dword [min], edx
                    if1:
                            inc edx
                            cmp edx, [numbersCount]
                            jl for2
                    xor ebx, ebx
                    xor edx, edx
                    mov ebx, dword [min]

                    mov eax, dword [ebp+4*ebx]
                    mov edx, dword [ebp+4*ecx]
                    mov dword [ebp+4*ebx], edx
                    mov dword [ebp+4*ecx], eax

                    inc ecx
                    cmp ecx, [numbersCount1]
                    jl for1
                    ret
    ; write to output
    mov eax, 4
    mov ebx, 1
    mov ecx, array
    mov edx, [fileLength]
    int 80h

    ; exit to linux
    mov     eax,1
    mov     ebx,0
    int     80h

; initialized data section
; use directives DB (byte), DW (word), DD (doubleword), DQ (quadword)
section .data

; uninitialized data section
; use directives RESB (byte), RESW (word), RESD (doubleword), RESQ (quadword)
section .bss
    fileLength resd 1
    numbersCount resd 1
    numbersCount1 resd 1
    min resd 1
    array resd 32768
    item resd 1
Was it helpful?

Solution

Your using ret without having used call. call is used to push the current location to the stack. ret basically just goes back to the thing on the top of the stack, as there is no call, the top of the stack is whatever you last put on it, so it will jump to a weird location where as I assume your OS is reasonable, it segfaults before the program can do any damage.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top