用汇编语言编写程序:对一个已知数组折半排序
1个回答
展开全部
dseg segment ;定义数据段
low_idx dw ?
high_idx dw ?
dseg ends
cseg segment ;定义数据段
b_search proc near
assume cs:cseg,ds:dseg,es:dseg
push ds
push ax
mov ax,dseg
mov ds,ax
mov es,ax
pop ax
cmp ax,es:[di+2] ;search value<or=first el.
ja chk_last ;no, go check last el.
lea si,es:[di+2] ;yes,fetch addr or first el.
je exit ;if value=lst el.,exit
stc ;if value<lst el., set CF
jmp exit ;and then exit
chk_last:
mov si,es:[di] ;point to last el.
shl si,1
add si,di
cmp ax,es:[si] ; search value>or=last el.
jb search ;no, go search list
je exit ;yes,exit if value=last el.
stc ;if value>last el.,set CF
jmp exit ;and then exit
search:
mov low_idx,1 ;fetch index
mov bx,es:[di]
mov high_idx,bx
mov bx,di
mid: mov cx,low_idx ;calculate middle point
mov dx,high_idx
cmp cx,dx
ja no_match
add cx,dx
shr cx,1
mov si,cx
shl si,1 ;calculate next search addr
compare: cmp ax,es:[bx+si] ;search value found?
je exit ;if so,exit
ja higher ;otherwise,find correct half
dec cx
mov high_idx,cx
jmp mid
higher: inc cx
mov low_idx,cx
jmp mid
no_match:stc ;set CF
exit: pop ds
ret
b_search endp
cseg ends
end
low_idx dw ?
high_idx dw ?
dseg ends
cseg segment ;定义数据段
b_search proc near
assume cs:cseg,ds:dseg,es:dseg
push ds
push ax
mov ax,dseg
mov ds,ax
mov es,ax
pop ax
cmp ax,es:[di+2] ;search value<or=first el.
ja chk_last ;no, go check last el.
lea si,es:[di+2] ;yes,fetch addr or first el.
je exit ;if value=lst el.,exit
stc ;if value<lst el., set CF
jmp exit ;and then exit
chk_last:
mov si,es:[di] ;point to last el.
shl si,1
add si,di
cmp ax,es:[si] ; search value>or=last el.
jb search ;no, go search list
je exit ;yes,exit if value=last el.
stc ;if value>last el.,set CF
jmp exit ;and then exit
search:
mov low_idx,1 ;fetch index
mov bx,es:[di]
mov high_idx,bx
mov bx,di
mid: mov cx,low_idx ;calculate middle point
mov dx,high_idx
cmp cx,dx
ja no_match
add cx,dx
shr cx,1
mov si,cx
shl si,1 ;calculate next search addr
compare: cmp ax,es:[bx+si] ;search value found?
je exit ;if so,exit
ja higher ;otherwise,find correct half
dec cx
mov high_idx,cx
jmp mid
higher: inc cx
mov low_idx,cx
jmp mid
no_match:stc ;set CF
exit: pop ds
ret
b_search endp
cseg ends
end
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询