Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.80 KB | None | 0 0
  1. TITLE Program5 (Program5.asm)
  2.  
  3. INCLUDE Irvine32.inc
  4.  
  5. ; (insert constant definitions here)
  6. MIN_INPUT = 10
  7. MAX_INPUT = 200
  8. LO_RANDOM = 100
  9. HI_RANDOM = 999
  10.  
  11. .data
  12.  
  13. ; (insert variable definitions here)
  14. intro BYTE "Fun with Arrays! by ", 0
  15. instruction BYTE "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 list sorted in descending order", 0
  16.  
  17. request DWORD 10
  18. ask_user BYTE "How many numbers should be generated? [10 ... 200]: ", 0
  19. error BYTE "Invalid input", 0
  20.  
  21. title_1 BYTE "The unsorted random numbers: ", 0
  22. title_2 BYTE "The sorted list: ", 0
  23. space BYTE " ", 0
  24.  
  25. mult DWORD 0.5
  26.  
  27. temp DWORD 0
  28.  
  29. list DWORD MAX_INPUT DUP(?)
  30. count DWORD 11
  31.  
  32. .code
  33. main PROC
  34.  
  35. ; (insert executable instructions here)
  36. call randomize
  37. call introduction
  38.  
  39. push OFFSET request ;passed by reference
  40. call getData
  41.  
  42. call CrLf
  43.  
  44. push request ; passed by value
  45. push OFFSET list ; passed by reference
  46. call fillArray
  47.  
  48. push OFFSET list
  49. push request
  50. push OFFSET title_1
  51. call displaylist
  52.  
  53. call CrLf
  54.  
  55. push OFFSET list
  56. push request
  57. call sortList
  58.  
  59. call CrLf
  60. ;
  61. push OFFSET list
  62. push request
  63. push OFFSET title_2
  64. call displaylist
  65.  
  66. ;push OFFSET list
  67. ;push request
  68. ;call displayMedian
  69.  
  70. exit ; exit to operating system
  71. main ENDP
  72.  
  73. ; (insert additional procedures here)
  74. introduction PROC
  75.  
  76. mov edx, OFFSET intro
  77. call WriteString
  78. call CrLf
  79. mov edx, OFFSET instruction
  80. call WriteString
  81. call CrLf
  82. ret
  83.  
  84. introduction ENDP
  85.  
  86. getData PROC
  87. ;include parameter - request (reference)
  88.  
  89. push ebp ;Set up stack frame
  90. mov ebp, esp
  91.  
  92. ;get an integer from user
  93. mov ebx, [ebp+8] ;get address of request into ebx
  94.  
  95. L1:
  96. mov edx, OFFSET ask_user
  97. call WriteString
  98. call ReadDec
  99.  
  100. cmp eax, MIN_INPUT
  101. jl errorMessage
  102. cmp eax, MAX_INPUT
  103. jg errorMessage
  104.  
  105. cmp eax, MIN_INPUT
  106. jge endThis
  107. cmp eax, MAX_INPUT
  108. jle endThis
  109.  
  110. errorMessage:
  111. mov edx, OFFSET error
  112. call WriteString
  113. call CrLf
  114. jmp L1
  115.  
  116. endThis:
  117. mov [ebx], eax
  118. pop ebp
  119. ret 4 ; remove four more bytes from the stack (after ret @)
  120. getData ENDP
  121.  
  122. fillArray PROC
  123. ;include parameters - request (value), array (reference)
  124. ; MAJORITY OF THE FOLLOWING CODE WAS EXTRACTED FROM LECTURE 20 SLIDES
  125. push ebp
  126. mov ebp, esp ;[ebp+4]
  127. mov edi, [ebp+8] ; @list in edi
  128. mov ecx, [ebp+12] ; value of request in ecx
  129.  
  130. more:
  131. mov eax, HI_RANDOM
  132. sub eax, LO_RANDOM
  133. inc eax
  134. call RandomRange
  135. add eax, LO_RANDOM
  136.  
  137. mov [edi], eax
  138. add edi, 4
  139. loop more
  140.  
  141. endmore:
  142. pop ebp
  143. ret 8
  144. fillArray ENDP
  145.  
  146. ;----------------------------------------------------------------------------
  147. ;----------------------------------------------------------------------------
  148. ;----------------------------------------------------------------------------
  149. sortList PROC
  150. ;include parameters - array (reference), request (value)
  151. push ebp
  152. mov ebp, esp ;[ebp+4]
  153. mov edi, [ebp+12] ; @list in edi
  154. mov ecx, [ebp+8] ; value of request in ecx
  155.  
  156. dec ecx ; request - 1
  157. mov ebx, 0 ; "k"
  158.  
  159. ;for(k=0; k<request-1; k++) {
  160. ;i = k;
  161. ;for(j=k+1; j<request; j++) {
  162. ;if(array[j] > array[i])
  163. ;i = j;
  164. ;}
  165. ;exchange(array[k], array[i]);
  166. ;}
  167.  
  168. firstLoop:
  169. mov eax, ebx ; "i = k"
  170.  
  171. mov edx, ebx ; "j = k"
  172. inc edx ; "j = k + 1"
  173. push ecx ; pushed the first loop's counter
  174. mov ecx, [ebp+8] ; made the second loop's counter = request
  175.  
  176. secondLoop:
  177. mov esi, [edi + (edx * 4)] ; array[j]
  178. cmp esi, [edi + (eax * 4)] ; compare array[j] and array[i]
  179. jg greater
  180. jle lesser
  181.  
  182. greater:
  183. mov eax, edx
  184. inc edx
  185. loop secondLoop
  186.  
  187. lesser:
  188. inc edx
  189. loop secondLoop
  190.  
  191. push edx
  192. push esi
  193. push [edi + (ebx * 4)] ; array[k]
  194. push [edi + (eax * 4)] ; array[i]
  195. call exchangeElements
  196. pop [edi + (eax * 4)]
  197. pop [edi + (ebx * 4)]
  198. pop esi
  199. pop edx
  200. pop ecx ; set the
  201. inc ebx ; increment k in the first loop
  202. loop firstLoop
  203.  
  204. pop ebp
  205. ret 8
  206.  
  207. sortList ENDP
  208.  
  209. exchangeElements PROC
  210. push ebp
  211. mov ebp, esp
  212. mov esi, [ebp+12] ; array[k]
  213. mov edx, [ebp+8] ; array[i]
  214. mov [ebp+8], esi
  215. mov [ebp+12], edx
  216. pop ebp
  217. ret
  218. exchangeElements ENDP
  219. ;----------------------------------------------------------------------------
  220. ;----------------------------------------------------------------------------
  221. ;----------------------------------------------------------------------------
  222.  
  223. displayMedian PROC
  224. push ebp
  225. mov ebp, esp ;[ebp+4]
  226. mov edi, [ebp+12] ; @list in edi
  227. mov ecx, [ebp+8] ; value of request in ecx
  228.  
  229. mov eax, ecx
  230. mov ebx, 2
  231. cdq
  232. div ebx
  233. cmp edx, 0
  234. je isEven
  235. cmp edx, 1
  236. je isOdd
  237.  
  238. ;def nlogn_median(l):
  239. ;l = sorted(l)
  240. ;if len(l) % 2 == 1:
  241. ;return l[len(l) / 2]
  242. ;else:
  243. ;return 0.5 * (l[len(l) / 2 - 1] + l[len(l) / 2])
  244.  
  245. isEven:
  246. mov esi, [edi + (eax - 1)]
  247. add esi, [edi + (eax)]
  248. mov eax, esi
  249. mov ebx, 2
  250. cdq
  251. div ebx
  252. call WriteDec
  253.  
  254. isOdd:
  255. mov eax, [edi + (eax*4)]
  256. call WriteDec
  257.  
  258. pop ebp
  259. ret
  260. displayMedian ENDP
  261.  
  262. displayList PROC
  263. push ebp
  264. mov ebp, esp ; [ebp+4]
  265. mov ecx, [ebp+12] ; @request
  266. mov edi, [ebp+16] ; @list
  267. mov esi, 10
  268.  
  269. mov edx, [ebp+8] ; @title
  270. call WriteString
  271. call CrLf
  272.  
  273. show:
  274. mov eax, [edi]
  275. call WriteDec
  276. mov edx, OFFSET space
  277. call WriteString
  278. add edi, 4
  279.  
  280. dec esi
  281. cmp esi, 0
  282. je callClear
  283.  
  284. loopAgain:
  285. loop show
  286.  
  287. jmp endshow
  288.  
  289. callClear:
  290. mov esi, 10
  291. call CrLf
  292. jmp loopAgain
  293.  
  294. endshow:
  295. pop ebp
  296. ret 12
  297.  
  298. displayList ENDP
  299.  
  300. END main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement