Advertisement
Guest User

Array Sort Descending

a guest
Mar 1st, 2015
440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. TITLE Random Number Array Sort    (P5_ARAUJOJ.asm)
  2.  
  3. ; Author: Jenise Araujo
  4. ; Course / Project ID   CS 271              Date: 02/20/2015
  5. ; Description: This program will receive a number from the user (limited by a range)
  6. ; Then the program will generate random numbers UP to the amount specified by the user (range limited)
  7. ; The numbers will be put into AN array, then sorted IN descending order AND then the median
  8. ; will be calculated AND the list displayed again, 10 numbers per line.
  9.  
  10. INCLUDE Irvine32.inc
  11.  
  12. MAX = 200
  13. MIN = 10
  14. LO = 100
  15. HI = 999
  16.  
  17. .data
  18. intro   BYTE    "This is the Random Number Array Sort program made by Jenise Araujo",0
  19. intro2  BYTE    "In this program, I will generate a user specified number of random integers",0
  20. intro3  BYTE    "Then I will sort the random numbers into an array and display the values.",0
  21. intro4  BYTE    "Afterwards, I will sort the values in descending order",0
  22. intro5  BYTE    "and then calculate the median",0
  23. intro6  BYTE    "Lastly I will show you the sorted list.",0
  24. getReq  BYTE    "How many values do you want to display?   [10...200]  ",0
  25. space   BYTE    "  ",0
  26. invalid BYTE    "Invalid input",0
  27. unsort  BYTE    "Here are the values for the unsorted list",0
  28. sorted  BYTE    "Here are the values for the sorted list",0
  29. request DWORD   ?
  30. Array   DWORD   MAX DUP(?)
  31. DisMedian  BYTE "The median number is ",0
  32. EndGame BYTE    "Thanks for playing! Goodbye now",0
  33.  
  34.  
  35. .code
  36. main PROC
  37.     call    Randomize           ;only call this once
  38.  
  39. ;introduce the game
  40.     call    Introduction        ;introduce the game AND display the instruction
  41.  
  42. ;get the user's number
  43.     push    OFFSET  request    
  44.     call    getData             ;get the number OF integers from the user
  45.  
  46. ;generate the random numbers AND store IN AN array             
  47.     push    OFFSET Array       
  48.     push    request
  49.     call    fillArray           ;generate random numbers AND store them IN AN array
  50.    
  51. ;Display the list numbers      
  52.     push    OFFSET  unsort
  53.     push    OFFSET Array
  54.     push    request
  55.     call    DisplayList         ;display the list OF numbers
  56.    
  57. ;Sort the array into descending order  
  58.     push    OFFSET Array
  59.     push    request
  60.     call    Sort                ;sort the array IN descending order
  61.  
  62. ;Display the median number
  63.     ;push   OFFSET Array
  64.     ;push   request
  65.     ;call   DisplayMedian       ;calculate AND display the median OF the array
  66.    
  67. ;Display the list numbers      
  68.     push    OFFSET sorted
  69.     push    OFFSET Array
  70.     push    request
  71.     call    DisplayList         ;display the list OF numbers
  72.  
  73. ;Say goodbye to the user
  74.     call    Goodbye
  75.  
  76.     exit    ; exit to operating system
  77. main ENDP
  78.  
  79. ;---------------------------------------------------------------
  80. ;Introduction Procedure - States the program AND the author AND
  81. ; displays the instructions for the program
  82. ;Receives: No parameters
  83. ;Returns: None
  84. ;Preconditions: None
  85. ;---------------------------------------------------------------
  86. ;introduction
  87.     Introduction    PROC
  88.         mov     edx, OFFSET intro       ; State the name OF the game AND the author
  89.         call    WriteString
  90.         call    CrLf
  91.  
  92.         mov     edx, OFFSET intro2
  93.         call    WriteString
  94.         call    CrLf
  95.        
  96.         mov     edx, OFFSET intro3
  97.         call    WriteString
  98.         call    CrLf
  99.  
  100.         mov     edx, OFFSET intro4
  101.         call    WriteString
  102.         call    CrLf
  103.  
  104.         mov     edx, OFFSET intro5
  105.         call    WriteString
  106.         call    CrLf
  107.  
  108.         mov     edx, OFFSET intro6
  109.         call    WriteString
  110.         call    CrLf
  111.  
  112.         RET
  113.     Introduction    ENDP
  114. ;---------------------------------------------------------------
  115. ;Get Data Procedure - Asks AND receives the user's amount OF
  116. ;numbers for the program
  117. ;Receives: No parameters
  118. ;Returns: The user's number is stored ON the stack
  119. ;Preconditions: None
  120. ;---------------------------------------------------------------
  121. ;get data
  122.     getData         PROC
  123.         push    ebp
  124.         mov     ebp,esp
  125.  
  126.     CollectNum:
  127.         mov     edx, OFFSET getReq   ;prompt user
  128.         call    WriteString
  129.         call    ReadDec              ; get the user's number
  130.  
  131.     ;validate the user's number
  132.         cmp     eax,MIN    
  133.         jl      invalidNum          ;if the number is less THAN 10
  134.         cmp     eax, MAX            ;if the number is greater THAN 200
  135.         jg      invalidNum
  136.         jmp     continue
  137.  
  138.     invalidNum:
  139.         mov     edx, OFFSET invalid     ;invalid input
  140.         call    WriteString
  141.         call    CrLf
  142.         jmp     CollectNum
  143.  
  144.     continue:
  145.         mov     ebx, [ebp + 8]       ; address OF request IN ebx
  146.         mov     [ebx], eax           ;store as a global variable
  147.         pop     ebp
  148.  
  149.         RET     4
  150.     getData         ENDP
  151. ;---------------------------------------------------------------
  152. ;Fill Array Procedure - This will generate the user's specified
  153. ;number OF random integers AND the put them IN AN array
  154. ;Receives: Address OF the array AND the user's request
  155. ;Returns: AN array filled WITH integers
  156. ;Preconditions: The user must have provided a number OF integers
  157. ;---------------------------------------------------------------
  158. ;fill array
  159.     fillArray       PROC
  160.         push    ebp
  161.         mov     ebp,esp
  162.         mov     edi, [ebp + 12]     ;address OF edi IN array
  163.         mov     ecx, [ebp + 8]      ;set the counter to request
  164.        
  165.     RandNum:
  166.         mov     eax,HI              ;set UP the max range for the random number
  167.         sub     eax,LO              ;set UP the smallest range for the random number
  168.         inc     eax                 ;generates a number between 0 AND HI
  169.         call    RandomRange
  170.         add     eax, LO             ;produces a number between LO AND HI
  171.         mov     [edi],eax           ;store the number IN the array
  172.         add     edi, 4
  173.         LOOP    RandNum
  174.  
  175.         pop     ebp
  176.         RET     8
  177.     fillArray       ENDP
  178. ;---------------------------------------------------------------
  179. ;Sort Procedure - This will sort the array into descending order
  180. ;Receives: Address OF the array AND the user's request
  181. ;Returns: AN array sorted IN descending order
  182. ;Preconditions: The user must have provided a number OF integers
  183. ; AND the array must be filled WITH valid integers
  184. ;---------------------------------------------------------------s
  185. ;sort list
  186.     Sort            PROC
  187.         push    ebp
  188.         mov     ebp, esp
  189.         mov     edi, [ebp + 12]
  190.         mov     ecx, [ebp + 8]
  191.         dec     ecx             ;outer LOOP is set to one less THAN request
  192.         mov     ebx, 0          ;k=0
  193.  
  194.     L1:
  195.         mov     eax, ebx    ; i = k
  196.  
  197.         mov     edx, eax   
  198.         inc     edx         ; j = k+1
  199.         push    ecx             ;store the value OF the outer LOOP
  200.         mov     ecx, [ebp + 8]  ; inner LOOP is set to request
  201.  
  202.     L2:
  203.         cmp     ecx,0
  204.         je      exchangeNext
  205.         mov     esi, [edi + edx*4]  ; store element j OF the array
  206.         cmp     esi, [edi + eax*4]  ;compare to element i
  207.         jg      greater            
  208.         inc     edx
  209.         LOOP    L2
  210.                    
  211.  
  212.     greater:
  213.         cmp     ecx,0
  214.         je      exchangeNext
  215.         mov     eax, edx        ; i = j
  216.         inc     edx             ;increase j for the next iteration OF the for LOOP
  217.         LOOP    L2
  218.    
  219.     exchangeNext:
  220.         lea     esi, [edi+ebx*4]                ;saved to work LIKE a temp variable
  221.         push    esi             ;saved to work LIKE a temp variable
  222.         lea     esi, [edi+eax*4]    ; swapping array[k] AND array [i]
  223.         push    esi
  224.         call    exchange
  225.         pop     ecx             ;restore outer LOOP value
  226.         inc     ebx             ;move forward through the outer LOOP
  227.         LOOP    L1
  228.  
  229.         pop     ebp
  230.         RET     8
  231.     Sort        ENDP
  232.        
  233.     exchange    PROC
  234.         pushad
  235.         mov     edx, [edi+eax*4]    ; store the value OF array[i]
  236.         mov     esi, [edi+ebx*4]    ; store the value OF array[k]
  237.         mov     [edi+eax*4], esi    ; switch the values
  238.         mov     [edi+ebx*4], edx
  239.         popad
  240.  
  241.         RET     8
  242.     exchange    ENDP
  243. ;---------------------------------------------------------------
  244. ;Display Median- This will fine the median OF the array
  245. ; AND then display it
  246. ;Receives: Address OF the array AND the user's request
  247. ;Returns: AN the median
  248. ;Preconditions: The user must have provided a number OF integers
  249. ; AND the array must be filled WITH valid integers
  250. ;---------------------------------------------------------------s
  251. ;Calculate AND display median
  252.     DisplayMedian   PROC
  253.         push    ebp
  254.         mov     ebp,esp
  255.         mov     edi, [ebp + 12]
  256.         mov     eax, [ebp + 8] 
  257.        
  258.         mov     edx, OFFSET DisMedian    
  259.         call    WriteString
  260.  
  261.     ;Determine if the the array size is even or odd
  262.         mov     edx, 0
  263.         mov     ebx, 2
  264.         div     ebx
  265.         cmp     edx, 0
  266.         je      IsEven              ; if request is even the find median by taking the average OF two middle numbers
  267.         jmp     IsOdd               ; if odd then find the value located IN the middle
  268.  
  269.     IsEven:
  270.         mov     eax, [edi]
  271.         add     eax, [edi - 4]      ;add the two adjacent elements
  272.         mov     edx, 0
  273.         mov     ebx, 2
  274.         div     ebx                 ;take the average OF those two numbers
  275.         call    WriteDec
  276.         call    CrLf
  277.         jmp     FoundAvg
  278.  
  279.     IsOdd:
  280.         mov     eax, [edi]         
  281.         call    WriteDec
  282.         call    CrLf
  283.         jmp     FoundAvg
  284.  
  285.     FoundAvg:
  286.         pop     ebp
  287.         RET     8
  288.     DisplayMedian   ENDP
  289. ;---------------------------------------------------------------
  290. ;DisplayList Procedure - This will display the values IN the array
  291. ;Receives: Address OF the array, the user's request AND address OF a title string
  292. ;Returns: Nothing
  293. ;Preconditions: The user must have provided a number OF integers
  294. ; AND the array must be filled WITH valid integers AND the address
  295. ; OF a string
  296. ;---------------------------------------------------------------s
  297. ;display list
  298.     DisplayList     PROC
  299.  
  300.     ;Set UP the stack
  301.         push    ebp
  302.         mov     ebp,esp
  303.         mov     edx, [ebp + 16]     ;address OF the title string
  304.         mov     esi, [ebp + 12]     ;address OF the array
  305.         mov     ecx, [ebp + 8]      ; set request as the LOOP control
  306.         mov     ebx, 0              ;keep count OF how many numbers are printed
  307.  
  308. ;Display the title
  309.         call    WriteString        
  310.         call    CrLf
  311.  
  312. ;Display the numbers
  313.     Show:
  314.         mov     eax, [esi]
  315.         call    WriteDec
  316.         mov     edx, OFFSET space
  317.         call    WriteString
  318.         add     esi, 4              ;Move to the next element IN the array
  319.         inc     ebx                 ;Counter
  320.         cmp     ebx, 10
  321.         je      printLine      
  322.         LOOP    Show
  323.         cmp     ecx, 0
  324.         jmp     endShow
  325.  
  326.     printLine:
  327.         call    CrLf            ;Print to the next line
  328.         mov     ebx, 0          ;reset ebx
  329.         LOOP    show   
  330.  
  331.     endShow:
  332.         call    CrLf
  333.         pop     ebp
  334.         RET     12
  335.     DisplayList     ENDP
  336. ;---------------------------------------------------------------
  337. ;Goodbye Procedure - Say goodbye AND end the gae
  338. ;Receives: No parameters
  339. ;Returns: None
  340. ;Preconditions: None
  341. ;---------------------------------------------------------------
  342. ;End the game AND say goodbye
  343.     Goodbye         PROC
  344.         mov     edx, OFFSET EndGame
  345.         call    WriteString
  346.         call    CrLf
  347.  
  348.         RET
  349.     Goodbye         ENDP
  350. END main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement