Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- TITLE Array Sorting (Program4.asm)
- ;Author: Jared Hughes
- ;Email: hughesja@onid.oregonstate.edu
- ;Class#: CS271-400
- ;Program Assignment 4
- ;Due: 8/2/2015
- ;Description: This program will generate random numbers in a range
- ; display them to the user, sort them, and calculate the median.
- ; It will then display the sorted list in descending order.
- INCLUDE Irvine32.inc
- MIN = 10
- MAX = 200
- LO = 100
- HI = 999
- .data
- myName BYTE "Array Sorting by Jared Hughes", 0
- explain BYTE "This program will generate random numbers in a range (10-200).", 0
- explain2 BYTE "It will display the array, sort it, calculate the median, and display it again.", 0
- getUserNum BYTE "How many numbers should be generated for the array? (10-200): ", 0
- errorM BYTE "ERROR: Out of allowable specified range. Please enter again.", 0
- unsorted BYTE "The unsorted array: ", 0
- sorted BYTE "The sorted array in descending order: ", 0
- median BYTE "The median is ", 0
- spaces BYTE " "
- ending BYTE "These results provided by J. Hughes. Goodbye.", 0
- request DWORD ?
- myArray DWORD MAX DUP(?)
- count DWORD 0
- medianValue DWORD ?
- .code
- main PROC
- call Randomize ;set up seed for randomization
- call introduction
- push OFFSET request ;pushes the location of request onto stack to pass to getData
- call getData
- COMMENT @
- push OFFSET myArray
- push request
- call fillArray
- push OFFSET unsorted
- push OFFSET myArray
- push request
- call displayArray
- push OFFSET myArray
- push request
- call sortArray
- push OFFSET count
- push OFFSET medianValue
- push OFFSET myArray
- push request
- call medianArray
- push OFFSET sorted
- push OFFSET myArray
- push request
- call displayArray
- @
- call farewell
- exit
- main ENDP
- ;-------------------------------------------------------------------
- ; introduction
- ;
- ; Introduces the programmer to the user as well as explains the
- ; details of the program. Instructs user on how to proceed to
- ; attain desired results.
- ;
- ; Receives: NONE
- ; Returns: NONE
- ; Regs Changed: EDX
- ;-------------------------------------------------------------------
- introduction PROC
- mov edx, OFFSET myName ;moves myName into the edx register
- call WriteString ;prints the string in the edx register to the console
- call CrLf ;newline (will not comment these last two lines out if they are repeated)
- mov edx, OFFSET explain
- call WriteString
- call CrLf
- mov edx, OFFSET explain2
- call WriteString
- call CrLf
- ret
- introduction ENDP
- ;-------------------------------------------------------------------
- ; getData
- ;
- ; gets the range chosen by the user to generate the array
- ; validate the number is in the correct range specified.
- ;
- ; Receives: reference parameter to 'request'
- ; Returns: NONE, erases 8 bytes off stack
- ; Regs Changed: EAX, EBX, EBP
- ;-------------------------------------------------------------------
- getData PROC
- push ebp
- mov ebp, esp
- getAgain:
- mov edx, OFFSET getUserNum
- call WriteString
- call ReadDec
- call CrLf
- mov [ebp+8], eax
- call validate
- mov eax, [ebp+8]
- mov ebx, 0
- cmp eax, ebx
- je getAgain ;if request is equal to (0) jump to getAgain-choice was out of range
- mov [ebp+8], eax
- pop ebp
- ret 4
- getData ENDP
- ;-------------------------------------------------------------------
- ; validate
- ;
- ; Validate user integer input is between 1 - 400. Produce error
- ; if number is outside range and prompt for new entry.
- ;
- ; Receives: errorTest and request reference parameter
- ; Returns: NONE
- ; Registers Changed: EAX, EBP, EDX
- ; Variables changed: by reference 'request' changed if out of range.
- ;-------------------------------------------------------------------
- validate PROC
- push ebp
- mov ebp, esp
- mov eax, [ebp+16]
- cmp eax, MAX ;if higher than upper range move to error message and loop input
- jg errorMessage
- cmp eax, MIN ;same but if lower
- jl errorMessage
- jmp toRet ;if number within 10-200, move to return statement
- errorMessage:
- mov edx, OFFSET errorM
- call WriteString
- call CrLf
- mov eax, 0 ;set eax to zero, thus signal main proc to loop again
- mov [ebp+16], eax ;mov 0 into address request is located at
- toRet:
- pop ebp
- ret
- validate ENDP
- ;-------------------------------------------------------------------
- ; fillArray
- ;
- ; generates a random number and fills the array to the user specified number
- ; Receives: reference to array, count(value)
- ; Returns: NONE
- ; Regs Changed: EAX, ESI, ESP, EBP, EBX
- ;-------------------------------------------------------------------
- fillArray PROC
- push ebp
- mov ebp, esp
- mov edi, [ebp+12] ;sets esi to start of array
- mov ecx, [ebp+8] ;set loop counter to quantity of numbers to generate
- fill:
- mov eax, HI ;set up EAX with parameter for RandomRange
- sub eax, LO ;sets up range for available numbers to RandomRange
- inc eax
- call RandomRange
- add eax, LO ;gives a "random" number between 100-999
- mov [edi], eax
- add edi, 4
- loop fill
- pop ebp
- ret 8
- fillArray ENDP
- ;-------------------------------------------------------------------
- ; displayArray
- ;
- ; display array to the user through output
- ; Receives: reference to array, request(value)
- ; Returns: NONE
- ; Regs Changed: EAX, ESI, ESP, EBP, EBX
- ;-------------------------------------------------------------------
- displayArray PROC
- push ebp
- mov ebp, esp
- mov edx, [ebp+16]
- call WriteString
- call CrLf
- mov esi, [ebp+12]
- mov ecx, [ebp+8]
- mov ebx, 0
- write:
- mov eax, [esi] ;set eax to current array location
- cmp ebx, 10 ;compares line counter to line limit
- jne noNewLine ;if number printed to the screen less than 10 this line, jump
- mov ebx, 0 ;if number printed is 10, reset counter to 0
- call CrLf ;moves print out of numbers to next line
- noNewLine:
- call WriteDec
- mov edx, OFFSET spaces
- call WriteString
- inc ebx ;increment counter for number of composites per line
- add esi, 4 ;mov esi to next array element
- loop write ;loop to display next array element
- pop ebp
- ret 12
- displayArray ENDP
- ;-------------------------------------------------------------------
- ; sortArray
- ;
- ; bubble sort array from largest to smallest number
- ; Receives: reference to array, request(value)
- ; Returns: NONE
- ; Regs Changed: EAX, ESI, ESP, EBP, EBX
- ;-------------------------------------------------------------------
- sortArray PROC
- push ebp
- mov ebp, esp
- mov ecx, [ebp+8]
- dec ecx
- outer:
- push ecx
- mov esi, [ebp+12]
- inner:
- mov eax, [esi]
- cmp [esi+4], eax
- jl loop3
- xchg eax, [esi+4]
- mov [esi], eax
- loop3:
- add esi, 4
- loop inner
- pop ecx
- loop outer
- pop ebp
- ret 8
- sortArray ENDP
- ;-------------------------------------------------------------------
- ; medianArray
- ;
- ; calculate median from sorted array
- ; Receives: reference myArray, value - request
- ; Returns: none
- ; Regs Changed: EAX, EBX, ESI, EBP
- ; variables changed: median
- ;-------------------------------------------------------------------
- medianArray PROC
- push ebp
- mov ebp, esp
- mov edx, 0
- mov eax, [ebp+8]
- mov esi, [ebp+12]
- mov ebx, 2
- div ebx
- cmp edx, 1
- jne center
- center:
- ret
- medianArray ENDP
- ;-------------------------------------------------------------------
- ; farewell
- ;
- ; Statement to end the program and say goodbye to user;
- ; Receives: NONE
- ; Returns: NONE
- ; Regs Changed: EDX
- ;-------------------------------------------------------------------
- farewell PROC
- call CrLf ;say goodbye to user
- mov edx, OFFSET ending
- call WriteString
- call CrLf
- ret
- farewell ENDP
- END main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement