Hardware Reference
In-Depth Information
inOrder
equ
2
; distance of local variable inOrder from stack top
inner
equ
1
; distance of local variable inner from stack top
iteration equ
0
; distance of local variable iteration from stack top
true
equ
1
false
equ
0
n
equ
30
; array count
local
equ
4
; number of bytes used by local variables
org
$1000
arrayX
dc.b
3,29,10,98,54,9,100,104,200,92,87,48,27,22,71
dc.b
1,62,67,83,89,101,190,187,167,134,121,20,31,34,54
org
$1500
start
lds
#$1500
; initialize stack pointer
ldx
#arrayX
pshx
ldaa
#n
psha
jsr bubble
leas 3,sp ; deallocate space used by outgoing parameters
; bra $ ; uncomment this instruction under CodeWarrior
swi ; break to D-Bug12 monitor
; *********************************************************************************
; The following subroutine implements the bubble sort with the array address and array count
; pushed into the stack.
; *********************************************************************************
bubble
pshd
pshy
pshx
leas
-local,sp
; allocate space for local variables
ldaa
arcnt,sp
; compute the number of iterations to be performed
deca
; ''
staa
iteration,sp
; ''
ploop
ldaa
#true
; set array inOrder flag to true before any iteration
staa
inOrder,sp
; ''
ldx
arr,sp
; use index register X as the array pointer
ldaa
iteration,sp
; initialize inner loop count for each iteration
staa
inner,sp
; ''
cloop
ldaa
0,x
; compare two adjacent elements
cmpa
1,x
; ''
bls looptst
; the following five instructions swap the two adjacent elements
staa
buf,sp
; swap two adjacent elements
ldaa
1,x
; ''
staa
0,x
; ''
ldaa
buf,sp
; ''
staa
1,x
; ''
ldaa
#false
; reset the inOrder flag
staa
inOrder,sp
; ''
looptst
inx
dec
inner,sp
Search WWH ::




Custom Search