Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8880599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:11:39+00:00 2026-06-14T20:11:39+00:00

The problem with segmentation fault has been solved, however there still remains a question

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T20:11:40+00:00Added an answer on June 14, 2026 at 8:11 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've a 'C' program which has encountered a strange problem.. I'm getting segmentation fault
I wrote a segmentation fault handler but the problem is, the instruction at which
I have segmentation fault problem with my multiple class files project. Without making empty
I have a segmentation fault problem occurring at the very end of my program.
I'm sure this question has already been asked in one form or another, but
I get segmentation fault accessing an object which looks valid and fully accessible in
The segmentation fault is caused in the scanner code. The Problem: Using GDB to
I'll start with my problem: My problem is that I'm getting a segmentation fault
Problem: Been struggling to get my code to load external shaders and it is
The following code triggers a segmentation fault at exit. It seems to happen only

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.