title: NASM assembly Insertion Sort
body: <p>I'm new to assembly language but I'm still stuck on this assignment. I need help with Insertion Sort assembly language. I don't get the part <code>array[j+1] := array[j]</code> in assembly code.<br />
  My assignment is:</p>
  <blockquote>
  <p>Write an assembly language program to sort an array (a) of byte (a = {7, 5, 2, 3, 6}) using the Insertion Sort algorithm. Please allocate the array size as size = 5 in memory.<br />
  The basic principle behind the insertion sort is simple: insert a new number into the sorted array in its proper place. To apply this algorithm, we start with an empty array. Then insert the first number. Now the array is in sorted order with just one element. Next insert the second number in its proper place. This results in a sorted array of size two. Repeat this process until all the numbers are inserted. The pseudocode for this algorithm, shown below, assumes that the array index starts with 0:</p>
  </blockquote>
  <pre><code>Insertion Sort (array, size)
  for (i = 1; i &lt; size −1; i + +) do
  temp := array[i]
  j := i −1
  while ((temp &lt; array[j]) and (j ≥0)) do
  array[j + 1] := array[j]
  j := j −1
  end while
  array[j + 1] := temp
  end for
  </code></pre>
  <p>Here, index i points to the number to be inserted. The array to the left of i is in sorted order. The numbers to be inserted are the ones located at or to the right of index i. The next number to be inserted is at i.</p>
  <p>Here is what I got:</p>
  <pre><code> segment .data
  array    db   7,5,2,3,6
  size     db   5
  Sorted   times 5 db 0
  
  
  
              segment .text
              global main
  main:   
          mov rsi, [Sorted]
        ;  mov edx,[size]
          sub edx,4                    ; size -1 
          mov ecx,1                    ;for (i = 1;
          
          for: cmp edx,ecx              ; i&lt; size - 1; i++)
               je  finish               ; i = 0 then exit
               movsx rbx, byte[array + rcx] ; temp = array[i]
               mov rax, rcx          ; j = i - 1           
               sub rax,1  
             while:                  ;while ( temp &lt; array[j] &amp;&amp; j &gt;= 0)
               cmp rax,0             ; j &gt;= 0?   
               jl end_while     
               cmp rbx,[array + rax] ; temp &lt; array[j]?
               jae end_while
             next:                     
               mov  r8, [array + rax + 1] ;????
               mov  r9, [array + rax]     ;????
               mov  r8,r9          ; array[j+1] := array[j]
               mov rsi,r8
               dec rax             ; j = j -1 
               inc rcx
               jmp 
               
              end_while:
               mov [Sorted + rax -1] ,rbx  ; array[j + 1] = temp
               inc rcx         
               jmp for 
               
            
         
     finish:
  </code></pre>