Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; ***********************************************************************
- ; * File : nebowman_lab10.asm
- ; * Author : Nathan Bowman
- ; * Description: Sorts an array of 10 numbers
- ; * Register use:
- ; * eax: holds the address of the first element in the array
- ; * ebx: holds the number of elements to be processed in the array
- ; ***********************************************************************
- .386
- .MODEL FLAT
- ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD
- PUBLIC _start ; make procedure _start public
- NUMELEMENTS EQU 10
- ; ***********************************************************************
- ; * Data Segment
- ; ***********************************************************************
- .DATA
- n DWORD 10 ; the number of elements in the array
- numArray DWORD NUMELEMENTS DUP ( ? ) ; 10 element array
- ; ***********************************************************************
- ; * Stack Segment
- ; ***********************************************************************
- .STACK 4096
- ; ***********************************************************************
- ; * Code Segment
- ; ***********************************************************************
- .CODE
- _start PROC NEAR32 ; start procedure called _start. Use flat, 32-bit address memory model
- lea eax, numArray ; eax = address of numArray
- push eax ; push 2 parameters: array address, n
- push n
- call mySort
- add esp, 8 ; clean the stack
- exit: EVEN ; Make rest of code aligned on an even-addressed byte
- INVOKE ExitProcess, 0 ; like return( 0 ); in C
- _start ENDP ; end procedure _start
- ; ***********************************************************************
- ; * mySort: sorts an array of n elements
- ; * Parameters: Address of the array, number of elements to be sorted
- ; * Returns: nothing
- ; * Register usage: eax: holds the address of the first element in the array
- ; * ebx: holds the number of elements to be processed in the array
- ; * ecx: holds the reference number of the top of the array with respect to
- ; * the array pointer
- ; * edx: holds the "count-1" term for the top for loop and the "i" term for
- ; * nested for loop
- ; * esi: holds the contents of the smallest element
- ; * edi: holds the contents of the remaining elements to be compared to
- ; * the smallest element
- ; * ebp: reference point to things on stack
- ; ***********************************************************************
- mySort PROC NEAR32
- push ebp ; save EBP of calling function
- mov ebp, esp ; now EBP -> top of stack
- pushfd ; push flags register
- mov eax, [ebp+12] ; eax = 1st parameter, array address
- mov ebx, [ebp+8] ; ebx = numElements (in array)
- sub esp, 4 ; create a local variable to hold the "top" element index
- mov ecx, 0 ; ecx = top = 0
- mov edx, ebx ; edx = count
- sub edx, 1 ; edx = numElements-1
- topLoop: cmp ecx, edx ; for ( top < count)
- jge return
- push edx ; push "count-1" onto the stack to be used later
- mov [ebp-8], ecx ; push "top" onto the stack to be used later
- mov edx, ecx ; edx = minIndex = ecx
- inc edx ; minIndex+1
- minLoop: cmp edx, ebx ; for( i=minindex+1; i < count; )
- jge swap
- sal ecx, 2 ; ecx = ecx*4
- sal edx, 2 ; edx = edx*4
- mov esi, [eax+ecx] ; esi = a[minIndex]
- mov edi, [eax+edx] ; edi = a[i]
- cmp edi, esi ; done if (a[i] < a[minIndex])
- jge finMin
- mov ecx, edx ; ecx = edx (minIndex = i)
- finMin: sar ecx, 2 ; ecx = ecx/4
- sar edx, 2 ; edx = edx/4
- inc edx ; edx++
- jmp minLoop
- swap: sal ecx, 2 ; ecx = ecx*4
- mov edx, ecx ; edx =ecx
- mov ecx, [ebp-8] ; restore "top"
- sal ecx, 2 ; ecx = ecx*4 (for array traversal)
- push eax ; push 3 parameters
- push ecx
- push edx
- call mySwap ; call mySwap function
- pop edx ;clean the stack
- pop ecx
- pop eax
- sar ecx, 2 ; ecx=ecx/4
- pop edx ; recall the count-1
- inc ecx ; ecx++
- jmp topLoop
- return: add esp, 4 ; clean the stack
- popfd
- pop ebp ; restore calling function EBP value
- ret
- mySort ENDP
- ; ****************************************************************
- ; * myFac: computes n! of a DWORD
- ; * Parameters: 2 DWORDs to add
- ; * Returns: nothing
- ; * Register usage: eax: gets n!
- ;* ebx: gets number (to be decremented) that n is mulitiplied by
- ; * e
- ; *****************************************************************
- mySwap PROC NEAR32
- push ebp ; save EBP of calling function
- mov ebp, esp ; now EBP -> top of stack
- pushfd ; push flags register
- mov eax, [ebp+16] ; eax = address of the array
- mov ecx, [ebp+12] ; ecx = first element to be swapped
- mov edx, [ebp+8] ; edx = second element to be swapped
- mov edi, [eax+ecx] ; edi = a[min]
- mov esi, [eax+edx] ; esi = a[top]
- mov [eax+ecx], esi ; a[top] = a[min]
- mov [eax+edx], edi ; a[min] = a[top]
- return: popfd
- pop ebp ; restore calling function EBP value
- ret
- mySwap ENDP
- END
Add Comment
Please, Sign In to add comment