Advertisement
Lawnknome

Untitled

Jul 28th, 2015
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.64 KB | None | 0 0
  1. TITLE Array Sorting (Program4.asm)
  2. ;Author: Jared Hughes
  3. ;Email: hughesja@onid.oregonstate.edu
  4. ;Class#: CS271-400
  5. ;Program Assignment 4
  6. ;Due: 8/2/2015
  7. ;Description: This program will generate random numbers in a range
  8. ; display them to the user, sort them, and calculate the median.
  9. ; It will then display the sorted list in descending order.
  10.  
  11. INCLUDE Irvine32.inc
  12.  
  13. MIN = 10
  14. MAX = 200
  15. LO = 100
  16. HI = 999
  17.  
  18. .data
  19.  
  20. myName BYTE "Array Sorting by Jared Hughes", 0
  21. explain BYTE "This program will generate random numbers in a range (10-200).", 0
  22. explain2 BYTE "It will display the array, sort it, calculate the median, and display it again.", 0
  23. getUserNum BYTE "How many numbers should be generated for the array? (10-200): ", 0
  24. errorM BYTE "ERROR: Out of allowable specified range. Please enter again.", 0
  25. unsorted BYTE "The unsorted array: ", 0
  26. sorted BYTE "The sorted array in descending order: ", 0
  27. median BYTE "The median is ", 0
  28. spaces BYTE " "
  29. ending BYTE "These results provided by J. Hughes. Goodbye.", 0
  30. request DWORD ?
  31. myArray DWORD MAX DUP(?)
  32. count DWORD 0
  33. medianValue DWORD ?
  34.  
  35.  
  36. .code
  37.  
  38. main PROC
  39.  
  40. call Randomize ;set up seed for randomization
  41.  
  42. call introduction
  43. push OFFSET request ;pushes the location of request onto stack to pass to getData
  44. call getData
  45.  
  46. COMMENT @
  47. push OFFSET myArray
  48. push request
  49. call fillArray
  50.  
  51. push OFFSET unsorted
  52. push OFFSET myArray
  53. push request
  54. call displayArray
  55.  
  56. push OFFSET myArray
  57. push request
  58. call sortArray
  59.  
  60. push OFFSET count
  61. push OFFSET medianValue
  62. push OFFSET myArray
  63. push request
  64. call medianArray
  65.  
  66. push OFFSET sorted
  67. push OFFSET myArray
  68. push request
  69. call displayArray
  70. @
  71. call farewell
  72.  
  73. exit
  74. main ENDP
  75.  
  76. ;-------------------------------------------------------------------
  77. ; introduction
  78. ;
  79. ; Introduces the programmer to the user as well as explains the
  80. ; details of the program. Instructs user on how to proceed to
  81. ; attain desired results.
  82. ;
  83. ; Receives: NONE
  84. ; Returns: NONE
  85. ; Regs Changed: EDX
  86. ;-------------------------------------------------------------------
  87. introduction PROC
  88.  
  89. mov edx, OFFSET myName ;moves myName into the edx register
  90. call WriteString ;prints the string in the edx register to the console
  91. call CrLf ;newline (will not comment these last two lines out if they are repeated)
  92. mov edx, OFFSET explain
  93. call WriteString
  94. call CrLf
  95. mov edx, OFFSET explain2
  96. call WriteString
  97. call CrLf
  98. ret
  99.  
  100. introduction ENDP
  101.  
  102. ;-------------------------------------------------------------------
  103. ; getData
  104. ;
  105. ; gets the range chosen by the user to generate the array
  106. ; validate the number is in the correct range specified.
  107. ;
  108. ; Receives: reference parameter to 'request'
  109. ; Returns: NONE, erases 8 bytes off stack
  110. ; Regs Changed: EAX, EBX, EBP
  111. ;-------------------------------------------------------------------
  112. getData PROC
  113.  
  114. push ebp
  115. mov ebp, esp
  116.  
  117. getAgain:
  118. mov edx, OFFSET getUserNum
  119. call WriteString
  120. call ReadDec
  121. call CrLf
  122. mov [ebp+8], eax
  123. call validate
  124. mov eax, [ebp+8]
  125. mov ebx, 0
  126. cmp eax, ebx
  127. je getAgain ;if request is equal to (0) jump to getAgain-choice was out of range
  128. mov [ebp+8], eax
  129. pop ebp
  130. ret 4
  131.  
  132. getData ENDP
  133.  
  134. ;-------------------------------------------------------------------
  135. ; validate
  136. ;
  137. ; Validate user integer input is between 1 - 400. Produce error
  138. ; if number is outside range and prompt for new entry.
  139. ;
  140. ; Receives: errorTest and request reference parameter
  141. ; Returns: NONE
  142. ; Registers Changed: EAX, EBP, EDX
  143. ; Variables changed: by reference 'request' changed if out of range.
  144. ;-------------------------------------------------------------------
  145. validate PROC
  146.  
  147. push ebp
  148. mov ebp, esp
  149. mov eax, [ebp+16]
  150. cmp eax, MAX ;if higher than upper range move to error message and loop input
  151. jg errorMessage
  152. cmp eax, MIN ;same but if lower
  153. jl errorMessage
  154. jmp toRet ;if number within 10-200, move to return statement
  155.  
  156. errorMessage:
  157. mov edx, OFFSET errorM
  158. call WriteString
  159. call CrLf
  160. mov eax, 0 ;set eax to zero, thus signal main proc to loop again
  161. mov [ebp+16], eax ;mov 0 into address request is located at
  162.  
  163. toRet:
  164. pop ebp
  165. ret
  166. validate ENDP
  167.  
  168. ;-------------------------------------------------------------------
  169. ; fillArray
  170. ;
  171. ; generates a random number and fills the array to the user specified number
  172. ; Receives: reference to array, count(value)
  173. ; Returns: NONE
  174. ; Regs Changed: EAX, ESI, ESP, EBP, EBX
  175. ;-------------------------------------------------------------------
  176. fillArray PROC
  177.  
  178. push ebp
  179. mov ebp, esp
  180. mov edi, [ebp+12] ;sets esi to start of array
  181. mov ecx, [ebp+8] ;set loop counter to quantity of numbers to generate
  182.  
  183. fill:
  184. mov eax, HI ;set up EAX with parameter for RandomRange
  185. sub eax, LO ;sets up range for available numbers to RandomRange
  186. inc eax
  187. call RandomRange
  188. add eax, LO ;gives a "random" number between 100-999
  189. mov [edi], eax
  190. add edi, 4
  191. loop fill
  192.  
  193. pop ebp
  194. ret 8
  195. fillArray ENDP
  196.  
  197. ;-------------------------------------------------------------------
  198. ; displayArray
  199. ;
  200. ; display array to the user through output
  201. ; Receives: reference to array, request(value)
  202. ; Returns: NONE
  203. ; Regs Changed: EAX, ESI, ESP, EBP, EBX
  204. ;-------------------------------------------------------------------
  205. displayArray PROC
  206.  
  207. push ebp
  208. mov ebp, esp
  209. mov edx, [ebp+16]
  210. call WriteString
  211. call CrLf
  212. mov esi, [ebp+12]
  213. mov ecx, [ebp+8]
  214. mov ebx, 0
  215.  
  216. write:
  217. mov eax, [esi] ;set eax to current array location
  218. cmp ebx, 10 ;compares line counter to line limit
  219. jne noNewLine ;if number printed to the screen less than 10 this line, jump
  220. mov ebx, 0 ;if number printed is 10, reset counter to 0
  221. call CrLf ;moves print out of numbers to next line
  222.  
  223. noNewLine:
  224. call WriteDec
  225. mov edx, OFFSET spaces
  226. call WriteString
  227. inc ebx ;increment counter for number of composites per line
  228. add esi, 4 ;mov esi to next array element
  229. loop write ;loop to display next array element
  230.  
  231. pop ebp
  232. ret 12
  233.  
  234. displayArray ENDP
  235.  
  236. ;-------------------------------------------------------------------
  237. ; sortArray
  238. ;
  239. ; bubble sort array from largest to smallest number
  240. ; Receives: reference to array, request(value)
  241. ; Returns: NONE
  242. ; Regs Changed: EAX, ESI, ESP, EBP, EBX
  243. ;-------------------------------------------------------------------
  244. sortArray PROC
  245.  
  246. push ebp
  247. mov ebp, esp
  248. mov ecx, [ebp+8]
  249. dec ecx
  250. outer:
  251. push ecx
  252. mov esi, [ebp+12]
  253. inner:
  254. mov eax, [esi]
  255. cmp [esi+4], eax
  256. jl loop3
  257. xchg eax, [esi+4]
  258. mov [esi], eax
  259. loop3:
  260. add esi, 4
  261. loop inner
  262.  
  263. pop ecx
  264. loop outer
  265.  
  266. pop ebp
  267. ret 8
  268.  
  269. sortArray ENDP
  270.  
  271. ;-------------------------------------------------------------------
  272. ; medianArray
  273. ;
  274. ; calculate median from sorted array
  275. ; Receives: reference myArray, value - request
  276. ; Returns: none
  277. ; Regs Changed: EAX, EBX, ESI, EBP
  278. ; variables changed: median
  279. ;-------------------------------------------------------------------
  280. medianArray PROC
  281.  
  282. push ebp
  283. mov ebp, esp
  284. mov edx, 0
  285. mov eax, [ebp+8]
  286. mov esi, [ebp+12]
  287. mov ebx, 2
  288. div ebx
  289. cmp edx, 1
  290. jne center
  291.  
  292. center:
  293.  
  294. ret
  295.  
  296. medianArray ENDP
  297.  
  298. ;-------------------------------------------------------------------
  299. ; farewell
  300. ;
  301. ; Statement to end the program and say goodbye to user;
  302. ; Receives: NONE
  303. ; Returns: NONE
  304. ; Regs Changed: EDX
  305. ;-------------------------------------------------------------------
  306. farewell PROC
  307.  
  308. call CrLf ;say goodbye to user
  309. mov edx, OFFSET ending
  310. call WriteString
  311. call CrLf
  312. ret
  313.  
  314. farewell ENDP
  315.  
  316. END main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement