Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- title Insert Sort (sort.asm); just the title, doesn't effect anything
- include console.inc; out favourite MACRO command library
- N equ 10; switching EQU with = won't make a difference
- CR equ 0Dh; ASCII for Carriage Return
- LF equ 0Ah; ASCII for new Line Feed
- .data?; store array in RAM, not executable
- X word N dup(?); DW is outdated
- .data; stored in executable
- copyright byte 'COPYRIGHT Egor Feklisov 113',CR,LF,0
- prompt_part1 byte '> Please enter ',0
- prompt_part2 byte ' elements of an array',CR,LF,0
- arrow byte CR,LF,'---> ',0
- status byte CR,LF,'> The program will now sort the array via "insertion method"',CR,LF,0
- .code
- start:
- ;---------------------------------------------------
- ;----------------------get-input--------------------
- outstr offset copyright
- outstr offset prompt_part1
- outint N
- outstr offset prompt_part2
- outstr offset arrow
- mov ecx,N; explicitly neaded for LOOP
- sub esi,esi; a way of resettinng the register
- loop_input:
- inint X[esi * type X]
- inc esi
- loop loop_input
- ;---------------------------------------------------
- ;---------------------------------------------------
- ;--------------------process-array------------------
- outstr offset status
- sub esi,esi
- mov esi,N - 1; marker
- mov ebx,N; counter
- loop_iterate:; iterates from end to start
- mov ax,X[esi * type X]; copy of the marker
- mov ecx,0; pointer to the element being compared with the marker
- loop_check:; iterates from start to marker from the outer loop
- cmp ax,X[ecx * type X]
- jl init_tranformation; insert value if needed
- inc ecx
- cmp ecx,esi
- jb loop_check; jump if below
- jmp skip_transformation
- init_tranformation:; shift all values to the right
- xchg ax, X[ecx * type X]; shifted the first one
- mov edx,ecx
- inc edx
- ; shift the rest without breaking ecx
- loop_replace:
- xchg ax,X[edx * type X]
- inc edx
- cmp edx,esi
- jbe loop_replace
- ja loop_iterate; jump if above
- skip_transformation:; modify both marker and counter
- ; if counter runs out - stop processing
- dec ebx
- cmp ebx,0
- jbe end_iterate; jump if below or equals
- ; shift marker to the left
- dec esi
- cmp esi,0
- jae loop_iterate; jump if above
- ;---------------------------------------------------
- ;---------------------------------------------------
- ;------------------exit-processing------------------
- end_iterate:; just the exit label
- ;---------------------------------------------------
- ;---------------------------------------------------
- ;-------------------print-result--------------------
- mov ecx,N
- sub esi,esi
- outstr offset arrow
- loop_output:
- outint X[esi * type X]
- outchar ' '
- inc esi
- loop loop_output
- ;---------------------------------------------------
- exit; invoke ExitProcess,0
- end start
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement