题目描述:在附加段中,有一个从小到大的无符号数数组,数组的第一个单元存放着数组的长度。在AX中有一个无符号数,在数组中查找(AX),找到,CF=0,找不到,CF=1。
说说思路:
1,判断(AX)和第一个元素的关系,(AX)<=1st,置CF退出,>则继续查找
2,设定dlow,dhigh指示元素,mid=(dlow+dhigh)/2
3, mid?(AX)
< :dlow <- mid+1
> :dhigh <-mid-1
4, dlow ? dhigh
<= :goto 2
> :no match, search over
5, exit
dseg segment
dlow dw ?
dhigh dw ?
mid dw ?
cache dw ?
dseg ends
eseg segment
start_addr dw 7,15,33,34,40,47,52,53
eseg ends
pro segment
main proc far
assume cs:pro,ds:dseg,es:eseg
start:
mov ax,dseg
mov ds,ax
mov ax,eseg
mov es,ax
mov dlow,1
mov bx,es:[di]
mov dhigh,bx ;set dlow,dhigh
mov ax,35 ;set ax
mov cache,ax ;saving ax
lea di,es:[start_addr]
clc ;
cmp ax,es:[di+2];cmpare ax and 1st
ja next ;above
je exit ;find it
stc
;screen test
mov dx,30h
mov ah,02h
int 21h
;screen test end
jmp exit ;ax<1st,no match exit
next:
mov ax,dlow
add ax,dhigh
mov bl,2
div bl
mov ah,0 ;set ax matchs mid, word
mov mid,ax ;savind mid
mul bl
mov di,ax ;count new addr
;and here we can shift instead
mov ax,cache
cmp ax,es:[di];compare ax and mid
ja above
jb below
clc
;screen test
mov dx,31h
mov ah,02h
int 21h
;screen test end
jmp exit ;find it
above: ;set dlow <-mid+1
mov bx,1
add bx,mid
mov dlow,bx
jmp compare
below: ;set dhigh <-mid-1
mov bx,mid
sub bx,1
mov dhigh,bx
compare:
mov bx,dlow
cmp bx,dhigh
jbe next
stc
;screen test
mov dx,32h
mov ah,02h
int 21h
;screen test end
jmp exit ;no macth ,exit
exit:
mov ax,4c00h
int 21h
main endp
pro ends
end start
转载于:https://blog.51cto.com/poollooq/1368159