INCLUDE Irvine32.inc
.data
;statements
intro1 BYTE "Program #4", 0
intro2 BYTE "Programmed by ", 0
intro3 BYTE "This program generates random numbers in the range of 10 to 29.", 0
intro4 BYTE "The list is then displayed and sorted, the median of the list is determined, ", 0
intro5 BYTE "and the list sorted from smallest to highest is displayed.", 0
instruct BYTE "How many numbers would you like to generate? Enter a number between 10 and 29:
", 0
outOfRange BYTE "Integer out of range.", 0
unsorted BYTE "Randoma numbers that are unsorted: ", 0
median BYTE "The median is ", 0
sorted BYTE "The sorted list: ", 0
spaces BYTE " ", 0
bye BYTE "Goodbye, and thanks for using this program!", 0
;variables
request DWORD ?
;constants
USER_MIN = 10
USER_MAX = 29
RAND_MIN = 10
RAND_MAX = 200
MAX_SIZE = 29
;array
list DWORD MAX_SIZE DUP(?)
.code
main PROC
 call introduction
 push OFFSET request
 call getData
 call Randomize
 push OFFSET list
 push request
 call fillArray
 push OFFSET list
 push request
 push OFFSET unsorted
 call displayList
 call CrLf
 push OFFSET list
 push request
 call sortList
 call CrLf
 push OFFSET list
 push request
 call displayMedian
 call CrLf
 push OFFSET list
 push request
 push OFFSET sorted
 call displayList
 call CrLf
 call CrLf
 call goodbye
 call CrLf
 exit
main ENDP
;-------------------------------------------------------------------------------------------;
; Introduction Procedure: ;
; Description: Displays instructions to user ; Lists programmer's name and program title. 
;-------------------------------------------------------------------------------------------;
introduction PROC
; displays programmer's name and program title 
 mov edx, OFFSET intro1
 call WriteString
 call CrLf
 mov edx, OFFSET intro2
 call WriteString
 call CrLf
 call CrLf
; displays instructions
 mov edx, OFFSET intro3
 call WriteString
 call CrLf
 mov edx, OFFSET intro4
 call WriteString
 call CrLf
 mov edx, OFFSET intro5
 call WriteString
 call CrLf
 call CrLf
 ret
introduction ENDP
;-------------------------------------------------------------------------------------------;
; Get Data Procedure: ;
; Description: Gets input and validates that input is in range. ;
;
;-------------------------------------------------------------------------------------------;
getData PROC
 push ebp
 mov ebp, esp
 mov ebx, [ebp + 8]
promptUser:
 mov edx, OFFSET instruct
 call WriteString
 call ReadInt
 mov [ebx], eax
 call CrLf
; check that value is in range
 cmp eax, USER_MAX
 jle checkLowerLimit
 mov edx, OFFSET outOfRange 
 call WriteString
 call CrLf
 jmp promptUser
checkLowerLimit:
 cmp eax, USER_MIN
 jge withinRange
 mov edx, OFFSET outOfRange
 call WriteString
 call CrLf
 jmp promptUser
; if input is valid
withinRange:
 pop ebp
 ret 4
getData ENDP
;-------------------------------------------------------------------------------------------;
; Fill Array Procedure: ;
; Description: Fills the array with requested number of random integers ;
;
;-------------------------------------------------------------------------------------------;
fillArray PROC
 push ebp
 mov ebp, esp
 mov esi, [ebp + 12] 
 
 mov ecx, [ebp + 8]
fillLoop:
 mov eax, RAND_MAX
 sub eax, RAND_MIN
 inc eax
 call RandomRange
 add eax, RAND_MIN
 mov [esi], eax
 add esi, 4
 loop fillLoop
 pop ebp
 ret 8
fillArray ENDP
;-------------------------------------------------------------------------------------------;
; Sort List Procedure: ;
; Description: Sorts array from highest to lowest ;
;-------------------------------------------------------------------------------------------;
sortList PROC
 push ebp
 mov ebp, esp
 mov esi, [ebp + 12]
 mov ecx, [ebp + 8]
 dec ecx
 outerLoop:
 mov eax, [esi]
 mov edx, esi
 push ecx
innerLoop:
 mov ebx, [esi + 4]
 mov eax, [edx]
 cmp eax, ebx
 jge noSwap
 add esi, 4
 push esi
 push edx
 push ecx
 call swapElements
 sub esi, 4
noSwap:
 add esi, 4
 loop innerLoop
 pop ecx
 mov esi, edx
 add esi, 4
 loop outerLoop

 pop ebp
 ret 8
sortList ENDP
;-------------------------------------------------------------------------------------------;
; Swap Elements Procedure: ;
; Description: Swaps elements if necessary ;
;-------------------------------------------------------------------------------------------;
swapElements PROC
 push ebp
 mov ebp, esp
 pushad
 mov eax, [ebp + 16]
 mov ebx, [ebp + 12]
 mov edx, eax
 sub edx, ebx
 ; swap values if needed
 mov esi, ebx
 mov ecx, [ebx]
 mov eax, [eax]
 mov [esi], eax
 add esi, edx
 mov [esi], ecx
 popad
 pop ebp
 ret 12
swapElements ENDP

;-------------------------------------------------------------------------------------------;
; Display Median Procedure: ;
; Description: Finds median of array ;
;-------------------------------------------------------------------------------------------;
displayMedian PROC
 push ebp
 mov ebp, esp
 mov esi, [ebp + 12]
 mov eax, [ebp + 8]
 mov edx, 0
 mov ebx, 2
 div ebx
 mov ecx, eax
getMedian:
 add esi, 4
 loop getMedian
 cmp edx, 0
 jnz oddNumber
 mov eax, [esi - 4]
 add eax, [esi]
 mov edx, 0
 mov ebx, 2
 div ebx
 mov edx, OFFSET median
 call WriteString
 call WriteDec
 call CrLf
 jmp endMedian
oddNumber:
 mov eax, [esi]
 mov edx, OFFSET median
 call WriteString
 call WriteDec
 call CrLf
endMedian:
 pop ebp
 ret 8
displayMedian ENDP
;-------------------------------------------------------------------------------------------;
; Display List Procedure: ;
; Description: Prints the array ;
;-------------------------------------------------------------------------------------------;
displayList PROC
 push ebp
 mov ebp, esp
 mov ebx, 0
 mov esi, [ebp + 16]
 mov ecx, [ebp + 12]
 mov edx, [ebp + 8] 
 call WriteString
 call CrLf
displayLoop:
 mov eax, [esi]
 call WriteDec
 mov edx, OFFSET spaces
 call WriteString
 inc ebx
 cmp ebx, USER_MIN
 jl nextElement
 call CrLf
 mov ebx, 0
nextElement:
 add esi, 4
 loop displayLoop
 pop ebp
 ret 8
displayList ENDP
;-------------------------------------------------------------------------------------------;
; Farewell Procedure: ;
; Description: Says goodbye and exits program ;
;-------------------------------------------------------------------------------------------;
goodbye PROC
 mov edx, OFFSET bye
 call WriteString
 call CrLf
 exit
goodbye ENDP
END main

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: