Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- TITLE Random Number Array Sort (P5_ARAUJOJ.asm)
- ; Author: Jenise Araujo
- ; Course / Project ID CS 271 Date: 02/20/2015
- ; Description: This program will receive a number from the user (limited by a range)
- ; Then the program will generate random numbers UP to the amount specified by the user (range limited)
- ; The numbers will be put into AN array, then sorted IN descending order AND then the median
- ; will be calculated AND the list displayed again, 10 numbers per line.
- INCLUDE Irvine32.inc
- MAX = 200
- MIN = 10
- LO = 100
- HI = 999
- .data
- intro BYTE "This is the Random Number Array Sort program made by Jenise Araujo",0
- intro2 BYTE "In this program, I will generate a user specified number of random integers",0
- intro3 BYTE "Then I will sort the random numbers into an array and display the values.",0
- intro4 BYTE "Afterwards, I will sort the values in descending order",0
- intro5 BYTE "and then calculate the median",0
- intro6 BYTE "Lastly I will show you the sorted list.",0
- getReq BYTE "How many values do you want to display? [10...200] ",0
- space BYTE " ",0
- invalid BYTE "Invalid input",0
- unsort BYTE "Here are the values for the unsorted list",0
- sorted BYTE "Here are the values for the sorted list",0
- request DWORD ?
- Array DWORD MAX DUP(?)
- DisMedian BYTE "The median number is ",0
- EndGame BYTE "Thanks for playing! Goodbye now",0
- .code
- main PROC
- call Randomize ;only call this once
- ;introduce the game
- call Introduction ;introduce the game AND display the instruction
- ;get the user's number
- push OFFSET request
- call getData ;get the number OF integers from the user
- ;generate the random numbers AND store IN AN array
- push OFFSET Array
- push request
- call fillArray ;generate random numbers AND store them IN AN array
- ;Display the list numbers
- push OFFSET unsort
- push OFFSET Array
- push request
- call DisplayList ;display the list OF numbers
- ;Sort the array into descending order
- push OFFSET Array
- push request
- call Sort ;sort the array IN descending order
- ;Display the median number
- ;push OFFSET Array
- ;push request
- ;call DisplayMedian ;calculate AND display the median OF the array
- ;Display the list numbers
- push OFFSET sorted
- push OFFSET Array
- push request
- call DisplayList ;display the list OF numbers
- ;Say goodbye to the user
- call Goodbye
- exit ; exit to operating system
- main ENDP
- ;---------------------------------------------------------------
- ;Introduction Procedure - States the program AND the author AND
- ; displays the instructions for the program
- ;Receives: No parameters
- ;Returns: None
- ;Preconditions: None
- ;---------------------------------------------------------------
- ;introduction
- Introduction PROC
- mov edx, OFFSET intro ; State the name OF the game AND the author
- call WriteString
- call CrLf
- mov edx, OFFSET intro2
- call WriteString
- call CrLf
- mov edx, OFFSET intro3
- call WriteString
- call CrLf
- mov edx, OFFSET intro4
- call WriteString
- call CrLf
- mov edx, OFFSET intro5
- call WriteString
- call CrLf
- mov edx, OFFSET intro6
- call WriteString
- call CrLf
- RET
- Introduction ENDP
- ;---------------------------------------------------------------
- ;Get Data Procedure - Asks AND receives the user's amount OF
- ;numbers for the program
- ;Receives: No parameters
- ;Returns: The user's number is stored ON the stack
- ;Preconditions: None
- ;---------------------------------------------------------------
- ;get data
- getData PROC
- push ebp
- mov ebp,esp
- CollectNum:
- mov edx, OFFSET getReq ;prompt user
- call WriteString
- call ReadDec ; get the user's number
- ;validate the user's number
- cmp eax,MIN
- jl invalidNum ;if the number is less THAN 10
- cmp eax, MAX ;if the number is greater THAN 200
- jg invalidNum
- jmp continue
- invalidNum:
- mov edx, OFFSET invalid ;invalid input
- call WriteString
- call CrLf
- jmp CollectNum
- continue:
- mov ebx, [ebp + 8] ; address OF request IN ebx
- mov [ebx], eax ;store as a global variable
- pop ebp
- RET 4
- getData ENDP
- ;---------------------------------------------------------------
- ;Fill Array Procedure - This will generate the user's specified
- ;number OF random integers AND the put them IN AN array
- ;Receives: Address OF the array AND the user's request
- ;Returns: AN array filled WITH integers
- ;Preconditions: The user must have provided a number OF integers
- ;---------------------------------------------------------------
- ;fill array
- fillArray PROC
- push ebp
- mov ebp,esp
- mov edi, [ebp + 12] ;address OF edi IN array
- mov ecx, [ebp + 8] ;set the counter to request
- RandNum:
- mov eax,HI ;set UP the max range for the random number
- sub eax,LO ;set UP the smallest range for the random number
- inc eax ;generates a number between 0 AND HI
- call RandomRange
- add eax, LO ;produces a number between LO AND HI
- mov [edi],eax ;store the number IN the array
- add edi, 4
- LOOP RandNum
- pop ebp
- RET 8
- fillArray ENDP
- ;---------------------------------------------------------------
- ;Sort Procedure - This will sort the array into descending order
- ;Receives: Address OF the array AND the user's request
- ;Returns: AN array sorted IN descending order
- ;Preconditions: The user must have provided a number OF integers
- ; AND the array must be filled WITH valid integers
- ;---------------------------------------------------------------s
- ;sort list
- Sort PROC
- push ebp
- mov ebp, esp
- mov edi, [ebp + 12]
- mov ecx, [ebp + 8]
- dec ecx ;outer LOOP is set to one less THAN request
- mov ebx, 0 ;k=0
- L1:
- mov eax, ebx ; i = k
- mov edx, eax
- inc edx ; j = k+1
- push ecx ;store the value OF the outer LOOP
- mov ecx, [ebp + 8] ; inner LOOP is set to request
- L2:
- cmp ecx,0
- je exchangeNext
- mov esi, [edi + edx*4] ; store element j OF the array
- cmp esi, [edi + eax*4] ;compare to element i
- jg greater
- inc edx
- LOOP L2
- greater:
- cmp ecx,0
- je exchangeNext
- mov eax, edx ; i = j
- inc edx ;increase j for the next iteration OF the for LOOP
- LOOP L2
- exchangeNext:
- lea esi, [edi+ebx*4] ;saved to work LIKE a temp variable
- push esi ;saved to work LIKE a temp variable
- lea esi, [edi+eax*4] ; swapping array[k] AND array [i]
- push esi
- call exchange
- pop ecx ;restore outer LOOP value
- inc ebx ;move forward through the outer LOOP
- LOOP L1
- pop ebp
- RET 8
- Sort ENDP
- exchange PROC
- pushad
- mov edx, [edi+eax*4] ; store the value OF array[i]
- mov esi, [edi+ebx*4] ; store the value OF array[k]
- mov [edi+eax*4], esi ; switch the values
- mov [edi+ebx*4], edx
- popad
- RET 8
- exchange ENDP
- ;---------------------------------------------------------------
- ;Display Median- This will fine the median OF the array
- ; AND then display it
- ;Receives: Address OF the array AND the user's request
- ;Returns: AN the median
- ;Preconditions: The user must have provided a number OF integers
- ; AND the array must be filled WITH valid integers
- ;---------------------------------------------------------------s
- ;Calculate AND display median
- DisplayMedian PROC
- push ebp
- mov ebp,esp
- mov edi, [ebp + 12]
- mov eax, [ebp + 8]
- mov edx, OFFSET DisMedian
- call WriteString
- ;Determine if the the array size is even or odd
- mov edx, 0
- mov ebx, 2
- div ebx
- cmp edx, 0
- je IsEven ; if request is even the find median by taking the average OF two middle numbers
- jmp IsOdd ; if odd then find the value located IN the middle
- IsEven:
- mov eax, [edi]
- add eax, [edi - 4] ;add the two adjacent elements
- mov edx, 0
- mov ebx, 2
- div ebx ;take the average OF those two numbers
- call WriteDec
- call CrLf
- jmp FoundAvg
- IsOdd:
- mov eax, [edi]
- call WriteDec
- call CrLf
- jmp FoundAvg
- FoundAvg:
- pop ebp
- RET 8
- DisplayMedian ENDP
- ;---------------------------------------------------------------
- ;DisplayList Procedure - This will display the values IN the array
- ;Receives: Address OF the array, the user's request AND address OF a title string
- ;Returns: Nothing
- ;Preconditions: The user must have provided a number OF integers
- ; AND the array must be filled WITH valid integers AND the address
- ; OF a string
- ;---------------------------------------------------------------s
- ;display list
- DisplayList PROC
- ;Set UP the stack
- push ebp
- mov ebp,esp
- mov edx, [ebp + 16] ;address OF the title string
- mov esi, [ebp + 12] ;address OF the array
- mov ecx, [ebp + 8] ; set request as the LOOP control
- mov ebx, 0 ;keep count OF how many numbers are printed
- ;Display the title
- call WriteString
- call CrLf
- ;Display the numbers
- Show:
- mov eax, [esi]
- call WriteDec
- mov edx, OFFSET space
- call WriteString
- add esi, 4 ;Move to the next element IN the array
- inc ebx ;Counter
- cmp ebx, 10
- je printLine
- LOOP Show
- cmp ecx, 0
- jmp endShow
- printLine:
- call CrLf ;Print to the next line
- mov ebx, 0 ;reset ebx
- LOOP show
- endShow:
- call CrLf
- pop ebp
- RET 12
- DisplayList ENDP
- ;---------------------------------------------------------------
- ;Goodbye Procedure - Say goodbye AND end the gae
- ;Receives: No parameters
- ;Returns: None
- ;Preconditions: None
- ;---------------------------------------------------------------
- ;End the game AND say goodbye
- Goodbye PROC
- mov edx, OFFSET EndGame
- call WriteString
- call CrLf
- RET
- Goodbye ENDP
- END main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement