user3787105
user3787105

Reputation: 11

x86 Assembly: Programming Insertion_sort as a beginner

I have been very much annoyed to complete this .asm code. This result must be printed in order by using Insertion_Sort.

I actually tried Bubble_Sort in case the logic of Algorithm might be wrong. I would like to ask you to read through this code. Any advice would be very welcome. I really want to know what I missed.

Here is the result I got so far: ( the rlt of Bubble sort is also very strange. Since I don't know how to debug in asm, I can't help just reading through the code again and again. Desperate in help. )

1 3 5 2 7 4 6 8 9 10

 %include "asm_io.inc"
 section .data
   array dd 3,1,5,7,2,8,4,9,6,10
   size dd 10
   segment .bss
   next resw 1
 segment .text
 global main
 main:
   enter 0,0
   pusha
   ; Insertion Sort Start from here 
   mov eax,[array+4*ecx]
   mov [next], eax
   ; j = i-1
   mov ebx, ecx
   dec ebx
 while2:
   cmp ebx,0
   jl EndLoop2
   mov eax, [next]
   cmp [array+4*ebx],eax
   jle EndLoop2
   ;array[j+1] = array[j]
   mov eax, [array+4*ebx]
   mov [array+4*(ebx+1)], eax
   ; j--
   dec ebx
   ; Go back to the top of this loop
   jp while2
 EndLoop2:
   mov eax, [next]
   mov [array+4*(ebx+1)],eax
   inc ecx
   jmp while1
 EndLoop1:
   mov ecx,0
 nextInt:
   mov eax, [array+4*ecx]
   call print_int
   call print_nl
   inc ecx
   cmp [size], ecx
   jne nextInt
   popa
   mov eax, 0
   leave
   ret

Upvotes: 1

Views: 902

Answers (2)

jay
jay

Reputation: 55

I have no idea, why I logged in another account and questioned, the above one(user3787105) is me, I just want to say that I solved the problem. and the errors made me crazy was caused by unnecessarily used "DEC" and "INC". thanks, guys. and sorry for not giving you any advantage for the reason I forgot the password on user3787105 account.

Upvotes: 0

Sep Roland
Sep Roland

Reputation: 39166

Errors include:

  • You use the ECX register in mov eax,[array+4*ecx] but fail to set it up beforehand.

  • With mov [next], eax you write a dword in a variable that was defined as a word.

  • Where does this jmp while1 go? There's no such label!

  • Not sure if the assembler can understand this addressing: mov [array+4*(ebx+1)], eax. Perhaps change it to: mov [array+4*ebx+4], eax

  • jp while2 Shouldn't this be a mere jmp? There are no parities to be checked here.

First solve all of these...

Upvotes: 1

Related Questions