[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,或者右移一次。 –