UNIVERSITY OF THE PUNJAB
Third Semester 2016
Examination: B.S. 4 Years Programme
Paper: Computer Organization and Assembly Language
Course Code: IT-203 / 21402
Question # 5:
Write a program that alphabetically sorts an array of ten characters.
Solution:
INCLUDE Irvine32.inc
.data
array DWORD 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P'
.code
main PROC
PUSH LENGTHOF array
PUSH OFFSET array
call BubbleSort
call WaitMsg
exit
main ENDP
;--------------------------- BubbleSort ---------------------------
;Sort an array of 32-bit signed integers in ascending order, using the bubble sort algorithm.
;Receives: pointer to array, array size
; --------------------------- Returns: nothing ---------------------------
BubbleSort PROC USES eax ecx esi, pArray:PTR DWORD, Count:DWORD
mov ecx,Count
dec ecx ; decrement count by 1
L1:
push ecx ; save outer loop count
mov esi,pArray ; point to first value
L2:
mov eax,[esi] ; get array value
cmp [esi+4],eax ; compare a pair of values
jg L3 ; if [ESI] <= [ESI+4], no exchange
xchg eax,[esi+4] ; exchange the pair
mov [esi],eax
L3: add esi,4 ; move both pointers forward
loop L2 ; inner loop
pop ecx ; retrieve outer loop count
loop L1 ; else repeat outer loop
L4: ret
BubbleSort ENDP
END main
.data
array DWORD 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P'
.code
main PROC
PUSH LENGTHOF array
PUSH OFFSET array
call BubbleSort
call WaitMsg
exit
main ENDP
;--------------------------- BubbleSort ---------------------------
;Sort an array of 32-bit signed integers in ascending order, using the bubble sort algorithm.
;Receives: pointer to array, array size
; --------------------------- Returns: nothing ---------------------------
BubbleSort PROC USES eax ecx esi, pArray:PTR DWORD, Count:DWORD
mov ecx,Count
dec ecx ; decrement count by 1
L1:
push ecx ; save outer loop count
mov esi,pArray ; point to first value
L2:
mov eax,[esi] ; get array value
cmp [esi+4],eax ; compare a pair of values
jg L3 ; if [ESI] <= [ESI+4], no exchange
xchg eax,[esi+4] ; exchange the pair
mov [esi],eax
L3: add esi,4 ; move both pointers forward
loop L2 ; inner loop
pop ecx ; retrieve outer loop count
loop L1 ; else repeat outer loop
L4: ret
BubbleSort ENDP
END main
Let me know in the comment section if you have any question.
Previous Post:
Program that reads two numbers from keyboard 'n' and 'p' calculates the n raise to power p.
This the best site for assembly language programming.
ReplyDelete