fork download
  1. .intel_syntax noprefix
  2.  
  3. # emulate irvine stuff
  4. .equ TAB, '\t'
  5.  
  6. Randomize:
  7. ret
  8.  
  9. # uses eax as range
  10. RandomRange:
  11. pusha
  12. mov ebx, eax
  13. call rand
  14. xor edx, edx
  15. div ebx
  16. mov [esp+28], edx
  17. popa
  18. ret
  19.  
  20. CrLf:
  21. pusha
  22. push [stdout]
  23. push '\n'
  24. call putc
  25. add esp, 8
  26. popa
  27. ret
  28.  
  29. ReadInt:
  30. pusha
  31. lea eax, [esp+28]
  32. push eax
  33. push offset DecFmt
  34. call scanf
  35. add esp, 8
  36. popa
  37. ret
  38.  
  39. WriteChar:
  40. pusha
  41. push [stdout]
  42. push eax
  43. call putc
  44. add esp, 8
  45. popa
  46. ret
  47.  
  48. # apparently this uses edx
  49. WriteString:
  50. pusha
  51. push [stdout]
  52. push edx
  53. call fputs
  54. add esp, 8
  55. popa
  56. ret
  57.  
  58. WriteDec:
  59. pusha
  60. push eax
  61. push offset DecFmt
  62. call printf
  63. add esp, 8
  64. popa
  65. ret
  66.  
  67. .data
  68. DecFmt: .asciz "%d"
  69.  
  70. # end irvine
  71.  
  72. .equ MIN, 10 #lower range limit
  73. .equ MAX, 200 #upper range limit
  74. .equ LRANGE, 100
  75. .equ HRANGE, 999
  76.  
  77. .data
  78.  
  79. #title, intro, and prompts
  80. title_1: .asciz "PROGRAMMING ASSIGNMENT 5: RANDOM GEN/SORT\n"
  81.  
  82. intro_1: .ascii "This program generates random numbers in the range (100 - 999),\n"
  83. .ascii "displays the original list, sorts the list, and calculates the median value.\n"
  84. .asciz "Finally, it displays the sorted list in descending order.\n"
  85.  
  86. prompt_1: .asciz "How many numbers should be generated? (10 - 200): "
  87.  
  88. error_1: .asciz "Out of range.\n"
  89.  
  90. display_1: .asciz "List of random numbers:\n"
  91. display_2: .asciz "The median is: "
  92. display_3: .asciz "The sorted list:\n"
  93.  
  94. #placeholders for user entries and calculated data
  95. .lcomm randArray, 4*MAX
  96. .lcomm userNum, 4 #integer to be entered by user
  97.  
  98. #strings for posting results
  99. goodBye_1: .asciz "Thank you for using the Gen/sort-ulator! Good-bye!"
  100.  
  101.  
  102. .text
  103. .globl main
  104. main:
  105. call Randomize
  106.  
  107. #Title Screen
  108. push OFFSET title_1
  109. push OFFSET intro_1
  110. call Intro
  111.  
  112. #Get and validate user numbers
  113. push OFFSET error_1
  114. push OFFSET prompt_1
  115. push OFFSET userNum
  116. call GetData
  117.  
  118. #Fill Array with random numbers
  119. push OFFSET randArray
  120. push userNum
  121. call FillArray
  122.  
  123. #display unsorted results
  124. push OFFSET randArray
  125. push userNum
  126. push OFFSET display_1
  127. call DisplayList
  128.  
  129. #sort the results
  130. push OFFSET randArray
  131. push userNum
  132. call sortList
  133.  
  134. #display the median
  135. push OFFSET randArray
  136. push userNum
  137. push OFFSET display_2
  138. call median
  139.  
  140. #display sorted results
  141. push OFFSET randArray
  142. push userNum
  143. push OFFSET display_3
  144. call DisplayList
  145.  
  146. #Say "goodbye"
  147. push OFFSET goodBye_1
  148. call Goodbye
  149.  
  150. xor eax, eax
  151. ret # exit to operating system
  152.  
  153. #-------------------------------------------------------
  154. #Gives an Intro to the program
  155. # Receives parameters on the system stack (in the order pushed):
  156. # Address of the title
  157. # Address of the intro
  158. #post: intro displayed
  159. #registers: none
  160. #-------------------------------------------------------
  161. Intro:
  162. pushad
  163. mov ebp, esp
  164. mov edx, [ebp+40]
  165. call WriteString
  166. call CrLf
  167. mov edx, [ebp+36]
  168. call WriteString
  169. call CrLf
  170. popad
  171. ret 8
  172.  
  173. #-------------------------------------------------------
  174. #Prompts user for an integer, int stores in userNum
  175. # Receives parameters on the system stack (in the order pushed):
  176. # Address of the error message
  177. # Address of the prompt
  178. # Address of return value
  179. #Post: userNum
  180. #registers: none
  181. #-------------------------------------------------------
  182. GetData:
  183. pushad
  184.  
  185. #setup stack and prompt for entry
  186. mov ebp, esp
  187. reenter:
  188. mov edx, [ebp+40]
  189. mov ebx, [ebp+36]
  190. call WriteString
  191. call ReadInt
  192.  
  193. #validate entry
  194. cmp eax, MIN #if eax < LOWER
  195. jl badEntry #jump to summary
  196. cmp eax, MAX #if eax > UPPER
  197. jg badEntry #reprompt
  198. jmp goodEntry #else jump to end, we have a good value
  199.  
  200. #bad entry reprompt
  201. badEntry:
  202. mov edx, [ebp+44]
  203. call WriteString
  204. jmp reenter
  205.  
  206. goodEntry:
  207. call CrLf
  208. mov [ebx], eax
  209. popad
  210. ret 12
  211.  
  212. #-------------------------------------------------------
  213. #Fills array with a number of random integers within RANGE
  214. #Recieves parameters on the system stack (in order pushed)
  215. # array
  216. # userNum
  217. #Post: array is filled with userNum number of randoms
  218. #Registers used: none
  219. #-------------------------------------------------------
  220. FillArray :
  221. pushad
  222. mov ebp, esp
  223. mov ecx, [ebp+36] #initialize loop counter with user entry
  224. mov edi, [ebp+40] #setup array offset
  225. fillLoop:
  226. call nextRand
  227. add edi, 4
  228. loop fillLoop
  229. popad
  230. ret 8
  231.  
  232. #-------------------------------------------------------
  233. #:edure nextRand
  234. # adapted from check lecture 20 solutions
  235. #:edure to get the next random number in the range specified by the user.
  236. # Preconditions: LRANGE < HRANGE
  237. # Registers used: eax, edi
  238. #-------------------------------------------------------
  239. nextRand:
  240. mov eax, HRANGE
  241. sub eax, LRANGE
  242. inc eax #add 1 to get the number of integers in range
  243. call RandomRange
  244. add eax, LRANGE #eax has value in [LOW - HIGH]
  245. mov [edi],eax
  246. ret
  247.  
  248. #-------------------------------------------------------
  249. #Sorts the contents of an integer array
  250. # Receives parameters on the system stack (in order pushed)
  251. # Array
  252. # Array Size
  253. #registers: none
  254. #-------------------------------------------------------
  255. sortList:
  256. pushad
  257. mov ebp, esp
  258. mov ecx, [ebp+36]
  259. mov edi, [ebp+40]
  260. dec ecx #ecx < request-1
  261. mov ebx, 0 #ebx=k
  262.  
  263. #for(k=0# k<request-1# k++)
  264. outerLoop:
  265. mov eax, ebx #eax=i=k
  266. mov edx, eax
  267. inc edx #edx=j=k+1
  268. push ecx
  269. mov ecx, [ebp+36] #ecx < request
  270.  
  271. #for(j=k+1# j<request# j++)
  272. innerLoop:
  273. mov esi, [edi+edx*4]
  274. cmp esi, [edi+eax*4]
  275. jle skip
  276. mov eax, edx
  277. skip:
  278. inc edx
  279. loop innerLoop
  280.  
  281. #swap elements
  282. lea esi, [edi+ebx*4]
  283. push esi
  284. lea esi, [edi+eax*4]
  285. push esi
  286. call exchange
  287. pop ecx
  288. inc ebx
  289. loop outerLoop
  290.  
  291. popad
  292. ret 8
  293.  
  294. #-------------------------------------------------------
  295. # Exchange k and i
  296. # Receives parameters on the system stack (in order pushed)
  297. # array[k]
  298. # array[i]
  299. #registers: none
  300. #-------------------------------------------------------
  301. exchange:
  302. pushad
  303. mov ebp,esp
  304. mov eax, [ebp+40] #array[k] low number
  305. mov ecx, [eax]
  306. mov ebx, [ebp+36] #array[i] high number
  307. mov edx, [ebx]
  308. mov [eax], edx
  309. mov [ebx], ecx
  310. popad
  311. ret 8
  312.  
  313.  
  314. #-------------------------------------------------------
  315. #Displays the median of an integer array
  316. # Receives parameters on the system stack (in order pushed)
  317. # Array
  318. # Array Size
  319. # display string
  320. #registers: none
  321. #-------------------------------------------------------
  322. median:
  323. pushad
  324. mov ebp, esp
  325. mov edi, [ebp+44]
  326. #display string
  327. mov edx, [ebp+36]
  328. call WriteString
  329.  
  330. #calculate median element
  331. mov eax, [ebp+40]
  332. cdq
  333. mov ebx, 2
  334. div ebx
  335. shl eax, 2
  336. add edi, eax
  337. cmp edx, 0
  338. je isEven
  339. #Array size is odd, so display the middle value
  340. mov eax, [edi]
  341. call WriteDec
  342. call CrLf
  343. call CrLf
  344. jmp endMedian
  345. isEven:
  346. #Array size is even so average the two middle values
  347. mov eax, [edi]
  348. add eax, [edi-4]
  349. cdq
  350. mov ebx, 2
  351. div ebx
  352. call WriteDec
  353. call CrLf
  354. call CrLf
  355. endMedian:
  356. popad
  357. ret 12
  358.  
  359. #-------------------------------------------------------
  360. #Displays the contents of an integer array, 10 per row
  361. # Receives parameters on the system stack (in order pushed)
  362. # Array
  363. # Array Size
  364. # display string
  365. #registers: none
  366. #-------------------------------------------------------
  367. DisplayList:
  368. pushad
  369. mov ebp, esp
  370.  
  371. #display string
  372. mov edx, [ebp+36]
  373. call WriteString
  374. call CrLf
  375. mov ecx, [ebp+40]
  376. mov edi, [ebp+44]
  377. mov ebx, 0
  378.  
  379. #display array contents
  380. listloop:
  381. inc ebx #counter for 10 items per row
  382. mov eax, [edi]
  383. call WriteDec
  384. add edi, 4
  385. cmp ebx, 10
  386. jne noReturn #jump if 10 items are not yet printed
  387. call CrLf
  388. mov ebx, 0
  389. jmp noTab #this skips adding a tab on a new row
  390. noReturn:
  391. mov al, TAB
  392. call WriteChar
  393. noTab:
  394. loop listloop
  395. call CrLf
  396. popad
  397. ret 12
  398.  
  399. #-------------------------------------------------------
  400. #Says good-bye to the user
  401. # Receives parameters on the system stack:
  402. # Address of string
  403. #registers: none
  404. #-------------------------------------------------------
  405. Goodbye:
  406. pushad
  407. mov ebp, esp
  408. mov edx, [ebp+36]
  409. call WriteString
  410. call CrLf
  411. popad
  412. ret 4
  413.  
Success #stdin #stdout 0.01s 1572KB
stdin
100
stdout
PROGRAMMING ASSIGNMENT 5: RANDOM GEN/SORT

This program generates random numbers in the range (100 - 999),
displays the original list, sorts the list, and calculates the median value.
Finally, it displays the sorted list in descending order.

How many numbers should be generated? (10 - 200): 
List of random numbers:

983	386	577	215	393	935	686	292	349	821
762	527	690	359	663	626	340	226	872	236
711	468	367	529	882	630	162	923	767	335
429	802	622	958	969	967	893	656	311	242
529	973	721	219	384	437	798	624	615	670
813	326	191	180	756	973	762	870	896	581
205	325	384	727	336	405	746	229	113	957
424	595	982	145	714	367	534	564	943	150
287	808	376	378	888	184	403	651	954	299
232	160	576	568	839	812	926	586	994	939

The median is: 583

The sorted list:

994	983	982	973	973	969	967	958	957	954
943	939	935	926	923	896	893	888	882	872
870	839	821	813	812	808	802	798	767	762
762	756	746	727	721	714	711	690	686	670
663	656	651	630	626	624	622	615	595	586
581	577	576	568	564	534	529	529	527	468
437	429	424	405	403	393	386	384	384	378
376	367	367	359	349	340	336	335	326	325
311	299	292	287	242	236	232	229	226	219
215	205	191	184	180	162	160	150	145	113

Thank you for using the Gen/sort-ulator! Good-bye!