TITLE sample of bubble sort for asmebly language
INCLUDE Irvine32.inc
.data
array DWORD 3,1,7,5,2,9,4,3
arraySize = ($ - array) / TYPE DWORD
before BYTE "before calling bubble sort, the array is: ",0
after BYTE "after calling bubble sort, the array is: ",0
BubbleSort PROTO pArray:PTR DWORD, Count:DWORD
DisplayArray PROTO pArray:PTR DWORD, Count:DWORD
.code
main PROC
; print original array
mov edx,OFFSET before
call WriteString
INVOKE DisplayArray, ADDR(array),arraySize
INVOKE BubbleSort,ADDR(array),arraySize
; print array after calling bubble sort
mov edx,OFFSET after
call WriteString
INVOKE DisplayArray, ADDR(array),arraySize
exit
main ENDP
;--------------------------------------------------------------
BubbleSort PROC USES eax ecx esi,
pArray:PTR DWORD, ; pointer to array
Count:DWORD ; array size
;
; Sort an array of 32-bit signed integers in ascending
; order, using the bubble sort algorithm.
; Receives: pointer to array, array size
; Returns: nothing
;--------------------------------------------------------------
mov ecx,Count
L1: push ecx
mov esi,pArray
L2:
mov eax,[esi]
cmp [esi+TYPE DWORD],eax
jg L3
xchg eax,[esi+TYPE DWORD]
mov [esi],eax
L3:
add esi,TYPE DWORD
loop L2
pop ecx
loop L1
ret
BubbleSort ENDP
;---------------------------------------------------------------
DisplayArray PROC USES esi ecx eax,
pArray:PTR DWORD,
Count:DWORD
;
; Display an array of 32-bit signed integers in console
; Receives: pointer to array, array size
; Returns: nothing
;---------------------------------------------------------------
mov ecx,Count
mov esi,pArray
L1: mov eax,[esi]
call WriteDec
mov al,' '
call WriteChar
add esi,Type pArray
loop L1
call Crlf
ret
DisplayArray ENDP
END main
原来汇编也可以写得很漂亮,赞一个