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

  • SEARCH
  • Home
  • 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 8820513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:37:37+00:00 2026-06-14T05:37:37+00:00

I have to do insertion sort of numbers in NASM. I have a file

  • 0

I have to do insertion sort of numbers in NASM. I have a file which generates random numbers and generate output file with numbers in binary form. My program loads this as input file and should give output with numbers sorted using insertion sort, also in binary form.

My code:

; Template assembler source file

section .text
global _start

_start:
    ; put your code here
    mov eax, 3
    mov ebx, 0
    mov ecx, array
    mov edx, 4*32768
    int 80h
    mov [fileLength], eax   
    shr eax,2
    dec eax
    mov [number], eax



    mov ebx, 1 
    outerloop:
        mov ecx,[array + 4*ebx]
        mov [item],ecx 

        mov ecx,ebx 
        interloop:
        mov edx,ecx 
        dec edx
        mov esi, [array + 4*edx]
        cmp esi,[array + 4*ecx]     
        jb koniec 
            mov eax,[array + 4*edx]
            mov [array + 4*ecx],eax 
        loop interloop
        koniec:

    mov edx,[item]
    mov [array + 4*ecx],edx 

    inc ebx
    cmp ebx,[number] 
    jne outerloop


    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   
    number resd 1   

    array resd 32768
    item resd 1

Pseudo code that I used to write insertion sort:

for i ← 1 to i ← length(A)-1
   {
     // A[ i ] is added in the sorted sequence A[0, .. i-1]
     // save A[i] to make a hole at index iHole
     item ← A[i]
     iHole ← i
     // keep moving the hole to next smaller index until A[iHole - 1] is <= item
     while iHole > 0 and A[iHole - 1] > item
       {
         // move hole to next smaller index
         A[iHole] ← A[iHole - 1]
         iHole ← iHole - 1
       }
     // put item in the hole
     A[iHole] ← item
   }

source: http://en.wikipedia.org/wiki/Insertion_sort

My knowledge about it is pretty small and I don’t know what goes wrong. It sometimes sorts first few numbers but the rest of them is not sorted correctly.

  • 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-14T05:37:38+00:00Added an answer on June 14, 2026 at 5:37 am

    You have to change the line

    cmp esi, [array + 4*edx]
    

    to

    cmp esi, [item]
    

    and remove dec eax before your outerloop. It should work.

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

Sidebar

Related Questions

I have to sort an array of 10 numbers using insertion sort and display
This program sorts the first n words from a file using insertion sort .
I would like to have a field which is updated on every change(insertion, modification),
I have implemented an insertion sort in a double link list (highest to lowest)
I need to build 3 funcs. the 1st is insertion sort, 2nd is generate
In my book they have calculated the running time of insertion sort on an
I have a content model and a comment model. On insertion of comment I
I'm new to CodeIgniter and have come into an issue with the insertion of
I need help regarding array insertion into mysql table. I have an array when
I am attempting to automate the insertion of JPEG images into Powerpoint. I have

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.