Hardware Reference
In-Depth Information
;
bra
$
; uncomment this instruction for CodeWarrior
swi
; *********************************************************************************
; The following subroutine uses the binary search algorithm to search an array of
; 8-bit elements to find if there is any element that matches the supplied key. The array
; base address, array count, and key are passed in the stack.
; *********************************************************************************
binSearch
pshx
; save X in the stack
pshb
; save B in the stack
leas
2 locVar,SP
; allocate space for locVar variables
movb
#0,min,SP
; initialize min to 0
ldaa
arrCnt,SP
; initialize max to arCnt 2 1
deca
; ''
staa
max,SP
; ''
ldx
arrBas,SP
; use X as the pointer to the array
loop
ldab
min,SP
; is search over yet?
cmpb
max,SP
; ''
lbhi
notfound
; if min > max, then not found (unsigned comparison)
addb
max,SP
; compute mean
lsrb
; ''
stab
mean,SP
; save mean
ldaa
b,x
; get a copy of the element arr[mean]
cmpa
key,SP
; compare key with array[mean]
beq
found
; found it?
bhi
searchLO
; continue to search in lower half
ldaa
mean,SP
; prepare to search in upper half
inca
; ''
staa
min,SP
; ''
bra
loop
searchLO
ldaa
mean,SP
; set up indices range for searching in the
deca
; lower half
staa
max,SP
; ''
bra
loop
found
ldaa
#1
bra
exit
notfound
ldaa
#0
exit
leas
locVar,SP
pulb
pulx
rts
arr
db
1,3,6,9,11,20,30,45,48,60
db
61,63,64,65,67,69,72,74,76,79
db
80,83,85,88,90,110,113,114,120,123
;
org
$FFFE
; uncomment this line for CodeWarrior
;
dc.w
start
; uncomment this line for CodeWarrior
end
 
Search WWH ::




Custom Search