[org 0x100]
;This code is for counting the size
mov di, 0 ; to be used for indexing
mov bp, s_a
mov cx, 0
jmp count
j1: inc cx ; storing the size in cx
count: mov ax, [bp+di]
add di, 2
cmp ax, -1 ;-1 is the ending condition
jne j1
;=================================;
mov si, cx ;Moving the size in si
mov cx, 2 ;using cx for division number
;This code is for finding the centre point by division
j2: mov ax, si
mov bx, 0
mov bx, cx
div bl
;=================================;
inc cx
inc cx
cmp al, 0 ;If al has 0 then the number is not in array
je nfound
mov dl, al
mov di, 0
;Increasing bx to point to the required number
j3: inc di
inc di
dec dx
cmp dx, 0
jne j3
;=================================;
mov ax, [bp+di] ;Moving the centre number in array to ax
mov dx, [b_s] ;The number to be found
cmp dx, ax
je found
cmp dx, ax
jl below
cmp dx, ax
jg above
above: add bp, di
jmp j2
below: jmp j2
found: mov cx, bp
add cx, di
jmp exit
nfound: mov ax, [bp] ;This code is for the first element
mov di, 0
cmp ax, [b_s]
je found
;=================;
mov cx, 0xffff
exit:
mov ax, 4c00h
int 21h
s_a: dw 1, 2, 3, 4, 5, -1
b_s: dw 5
这是我的代码二进制搜索其工作罚款的所有数字在这里除了'1'和'5'。我已经处理了'1'。请建议'5'的解决方案。这意味着最后的号码。 '-1'不被搜索。二进制搜索排序阵列在汇编语言
使用“bp”作为地址寄存器的默认段是堆栈段,但不是数据段。为了修复代码,您可以额外使用DS段覆盖前缀。例如:mov ax,DS:[bp + di] –
为什么在等效的“shr 1”中使用非常昂贵的指令“div”? –
当您的代码跳回到* j2 *标签时,它使用的CX值太高,无法找到中心点*。总是除以2,或者右移一次。 –