Write a procedure to sort a list of words in memory using a bubble-sort technique.
Use the attached sample program code as a skeleton for your program. You may simply fill in the code equivalent of the comments shown, or rewrite the algorithm if you so desire.
Use IOASM for integer output procedures.
Submit the source file, executable file and output screen dumps.
The sample program can be downloaded from the following location
:
http://elf.udw.ac.za/cs/sampprog/cs2/asm-1996/prac4.asm
You ought to be submitting at least three files for this practical.
; bubble-sort
DOSSEG
.MODEL SMALL
.STACK 4096
.DATA
anArray dw 100 dup (0) ; data
N dw ? ; size of array
Message1 db 'Enter the size of the list : $'
Message2 db 'Enter number : $'
Message3 db 'The sorted list is : ',13,10,'$'
.CODE
EXTRN readsint:proc,writesint:proc
; use procedures from IOASM
ProgramStart:
mov ax,SEG _DATA ; set up data segment
mov ds,ax
lea dx,Message1 ; output message
mov ah,9
int 21h
call readsint ; get N
mov N,ax
lea ax,anArray ; push address of array onto stack
push ax
push N ; push size of array onto stack
call inputarray ; read in array
lea ax,anArray ; push address of array onto stack
push ax
push N ; push size of array onto stack
call bubblesort ; sort array
lea dx,Message3 ; output message
mov ah,9
int 21h
lea ax,anArray ; push address of array onto stack
push ax
push N ; push size of array onto stack
call outputarray ; output array
mov ah,4ch ; terminate program
int 21h
inputarray PROC ; procedure to input an array
push bp ; save stack position
mov bp,sp
xor si,si ; initialize array index
mov cx,[bp+4] ; get size of array
mov bx,[bp+6] ; get start address
cmp cx,0 ; check for null-array
je inputend
inputstart: ; loop to input array
call readsint ; get integer
mov [bx+si],ax ; store in array
add si,2 ; increment array index
loop inputstart
inputend:
mov sp,bp ; restore stack
pop bp
ret 4 ; pop parameters off stack and return
inputarray ENDP
outputarray PROC ; procedure to output an array
push bp ; save stack position
mov bp,sp
xor si,si ; initialize array index
mov cx,[bp+4] ; get array size
mov bx,[bp+6] ; get start address
cmp cx,0 ; check for null-array
je outputend
outputstart: ; loop to output array
mov ax,[bx+si] ; get array element
call writesint ; output integer
add si,2 ; increment array index
loop outputstart
outputend:
mov sp,bp ; restore stack
pop bp
ret 4 ; pop parameters off stack and return
outputarray ENDP
bubblesort PROC ; procedure to bubble-sort an array
; save stack position
; get array length
; make it zero-based
; check if it is zero
; if true, end procedure
start1: ; start of outer loop
; save outer loop counter
; get array length
; make it zero-based
; if it is zero, continue outer loop
; get array offset
; make inner counter=0
start2: ; start of inner loop
; multiply counter by 2 because of words
; get first word
; get second word
; restore inner counter to previous value
; check if swap necessary
; if true, jump to relevant code
; if false, continue with loop
destbigger: ; swap procedure
; multiply counter by 2 because of words
; store second value into first position
; store first value into second position
; restore inner counter to previous value
sourcebigger:
; increment inner loop counter
; continue inner loop
end2: ; if inner loop counter runs out, ...
; restore outer loop counter
; continue outer loop
end1: ; if outer loop counter runs out, exit from proc
; restore stack
; pop parameters off stack and return
bubblesort ENDP
END ProgramStart