Guest User

Untitled

a guest
Apr 21st, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.52 KB | None | 0 0
  1. ; ***********************************************************************
  2. ; * File : nebowman_lab10.asm
  3. ; * Author : Nathan Bowman
  4. ; * Description: Sorts an array of 10 numbers
  5. ; * Register use:
  6. ; * eax: holds the address of the first element in the array
  7. ; * ebx: holds the number of elements to be processed in the array
  8. ; ***********************************************************************
  9. .386
  10. .MODEL FLAT
  11. ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD
  12. PUBLIC _start ; make procedure _start public
  13. NUMELEMENTS EQU 10
  14. ; ***********************************************************************
  15. ; * Data Segment
  16. ; ***********************************************************************
  17. .DATA
  18. n DWORD 10 ; the number of elements in the array
  19. numArray DWORD NUMELEMENTS DUP ( ? ) ; 10 element array
  20.  
  21. ; ***********************************************************************
  22. ; * Stack Segment
  23. ; ***********************************************************************
  24. .STACK 4096
  25. ; ***********************************************************************
  26. ; * Code Segment
  27. ; ***********************************************************************
  28. .CODE
  29. _start PROC NEAR32 ; start procedure called _start. Use flat, 32-bit address memory model
  30.  
  31. lea eax, numArray ; eax = address of numArray
  32. push eax ; push 2 parameters: array address, n
  33. push n
  34. call mySort
  35. add esp, 8 ; clean the stack
  36.  
  37. exit: EVEN ; Make rest of code aligned on an even-addressed byte
  38. INVOKE ExitProcess, 0 ; like return( 0 ); in C
  39. _start ENDP ; end procedure _start
  40.  
  41.  
  42. ; ***********************************************************************
  43. ; * mySort: sorts an array of n elements
  44. ; * Parameters: Address of the array, number of elements to be sorted
  45. ; * Returns: nothing
  46. ; * Register usage: eax: holds the address of the first element in the array
  47. ; * ebx: holds the number of elements to be processed in the array
  48. ; * ecx: holds the reference number of the top of the array with respect to
  49. ; * the array pointer
  50. ; * edx: holds the "count-1" term for the top for loop and the "i" term for
  51. ; * nested for loop
  52. ; * esi: holds the contents of the smallest element
  53. ; * edi: holds the contents of the remaining elements to be compared to
  54. ; * the smallest element
  55. ; * ebp: reference point to things on stack
  56. ; ***********************************************************************
  57. mySort PROC NEAR32
  58. push ebp ; save EBP of calling function
  59. mov ebp, esp ; now EBP -> top of stack
  60. pushfd ; push flags register
  61. mov eax, [ebp+12] ; eax = 1st parameter, array address
  62. mov ebx, [ebp+8] ; ebx = numElements (in array)
  63. sub esp, 4 ; create a local variable to hold the "top" element index
  64. mov ecx, 0 ; ecx = top = 0
  65. mov edx, ebx ; edx = count
  66. sub edx, 1 ; edx = numElements-1
  67.  
  68. topLoop: cmp ecx, edx ; for ( top < count)
  69. jge return
  70. push edx ; push "count-1" onto the stack to be used later
  71. mov [ebp-8], ecx ; push "top" onto the stack to be used later
  72. mov edx, ecx ; edx = minIndex = ecx
  73. inc edx ; minIndex+1
  74. minLoop: cmp edx, ebx ; for( i=minindex+1; i < count; )
  75. jge swap
  76. sal ecx, 2 ; ecx = ecx*4
  77. sal edx, 2 ; edx = edx*4
  78. mov esi, [eax+ecx] ; esi = a[minIndex]
  79. mov edi, [eax+edx] ; edi = a[i]
  80. cmp edi, esi ; done if (a[i] < a[minIndex])
  81. jge finMin
  82. mov ecx, edx ; ecx = edx (minIndex = i)
  83. finMin: sar ecx, 2 ; ecx = ecx/4
  84. sar edx, 2 ; edx = edx/4
  85. inc edx ; edx++
  86. jmp minLoop
  87.  
  88. swap: sal ecx, 2 ; ecx = ecx*4
  89. mov edx, ecx ; edx =ecx
  90. mov ecx, [ebp-8] ; restore "top"
  91. sal ecx, 2 ; ecx = ecx*4 (for array traversal)
  92.  
  93. push eax ; push 3 parameters
  94. push ecx
  95. push edx
  96. call mySwap ; call mySwap function
  97. pop edx ;clean the stack
  98. pop ecx
  99. pop eax
  100.  
  101. sar ecx, 2 ; ecx=ecx/4
  102. pop edx ; recall the count-1
  103. inc ecx ; ecx++
  104. jmp topLoop
  105.  
  106. return: add esp, 4 ; clean the stack
  107. popfd
  108. pop ebp ; restore calling function EBP value
  109. ret
  110. mySort ENDP
  111.  
  112.  
  113. ; ****************************************************************
  114. ; * myFac: computes n! of a DWORD
  115. ; * Parameters: 2 DWORDs to add
  116. ; * Returns: nothing
  117. ; * Register usage: eax: gets n!
  118. ;* ebx: gets number (to be decremented) that n is mulitiplied by
  119. ; * e
  120. ; *****************************************************************
  121. mySwap PROC NEAR32
  122. push ebp ; save EBP of calling function
  123. mov ebp, esp ; now EBP -> top of stack
  124. pushfd ; push flags register
  125.  
  126. mov eax, [ebp+16] ; eax = address of the array
  127. mov ecx, [ebp+12] ; ecx = first element to be swapped
  128. mov edx, [ebp+8] ; edx = second element to be swapped
  129.  
  130.  
  131. mov edi, [eax+ecx] ; edi = a[min]
  132. mov esi, [eax+edx] ; esi = a[top]
  133. mov [eax+ecx], esi ; a[top] = a[min]
  134. mov [eax+edx], edi ; a[min] = a[top]
  135.  
  136. return: popfd
  137. pop ebp ; restore calling function EBP value
  138. ret
  139. mySwap ENDP
  140.  
  141.  
  142. END
Add Comment
Please, Sign In to add comment