Advertisement
Guest User

Untitled

a guest
Mar 31st, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. title Insert Sort (sort.asm); just the title, doesn't effect anything
  2.  
  3. include console.inc; out favourite MACRO command library
  4.  
  5. N equ 10; switching EQU with = won't make a difference
  6. CR equ 0Dh; ASCII for Carriage Return
  7. LF equ 0Ah; ASCII for new Line Feed
  8.  
  9. .data?; store array in RAM, not executable
  10.     X word N dup(?); DW is outdated
  11.  
  12. .data; stored in executable
  13.     copyright byte 'COPYRIGHT Egor Feklisov 113',CR,LF,0
  14.     prompt_part1 byte '> Please enter ',0
  15.     prompt_part2 byte ' elements of an array',CR,LF,0
  16.     arrow byte CR,LF,'---> ',0
  17.     status byte CR,LF,'> The program will now sort the array via "insertion method"',CR,LF,0
  18.  
  19. .code
  20.  
  21. start:
  22. ;---------------------------------------------------
  23. ;----------------------get-input--------------------
  24.     outstr offset copyright
  25.     outstr offset prompt_part1
  26.     outint N
  27.     outstr offset prompt_part2
  28.     outstr offset arrow
  29.     mov ecx,N; explicitly neaded for LOOP
  30.     sub esi,esi; a way of resettinng the register
  31.     loop_input:
  32.         inint X[esi * type X]
  33.         inc esi
  34.         loop loop_input
  35. ;---------------------------------------------------
  36.  
  37.  
  38. ;---------------------------------------------------
  39. ;--------------------process-array------------------
  40.     outstr offset status
  41.     sub esi,esi
  42.     mov esi,N - 1; marker
  43.     mov ebx,N; counter
  44.  
  45.     loop_iterate:; iterates from end to start
  46.         mov ax,X[esi * type X]; copy of the marker
  47.         mov ecx,0; pointer to the element being compared with the marker
  48.         loop_check:; iterates from start to marker from the outer loop
  49.             cmp ax,X[ecx * type X]
  50.             jl init_tranformation; insert value if needed
  51.             inc ecx
  52.             cmp ecx,esi
  53.             jb loop_check; jump if below
  54.  
  55.         jmp skip_transformation
  56.         init_tranformation:; shift all values to the right
  57.             xchg ax, X[ecx * type X]; shifted the first one
  58.             mov edx,ecx
  59.             inc edx
  60.             ; shift the rest without breaking ecx
  61.             loop_replace:
  62.                 xchg ax,X[edx * type X]
  63.                 inc edx
  64.                 cmp edx,esi
  65.                 jbe loop_replace
  66.             ja loop_iterate; jump if above
  67.  
  68.         skip_transformation:; modify both marker and counter
  69.             ; if counter runs out - stop processing
  70.             dec ebx
  71.             cmp ebx,0
  72.             jbe end_iterate; jump if below or equals
  73.             ; shift marker to the left
  74.             dec esi
  75.             cmp esi,0
  76.             jae loop_iterate; jump if above
  77. ;---------------------------------------------------
  78.  
  79.  
  80. ;---------------------------------------------------
  81. ;------------------exit-processing------------------
  82.     end_iterate:; just the exit label
  83. ;---------------------------------------------------
  84.  
  85.  
  86. ;---------------------------------------------------
  87. ;-------------------print-result--------------------
  88.     mov ecx,N
  89.     sub esi,esi
  90.     outstr offset arrow
  91.     loop_output:
  92.         outint X[esi * type X]
  93.         outchar ' '
  94.         inc esi
  95.         loop loop_output
  96. ;---------------------------------------------------
  97.  
  98.  
  99.     exit; invoke ExitProcess,0
  100. end start
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement