汇编语言的二进制搜索

时间:2013-10-23 17:45:26

标签: assembly binary-search x86-16

我是汇编语言编程的新手 我搜索了二进制搜索程序并找到了这个并且我尝试了解该程序,它的工作正常,但我无法理解代码的成功部分:
什么是ADD al,'0'和什么是mov res,al ??

.model small
.stack 100h
.data
    ARR DW 1000H,2000H,3000H,4000H,5000H,6000H
    LEN DW ($-ARR)/2
    KEY EQU 2000H
    SUC DB "KEY IS FOUND AT$"
    FAILURE DB "KEY IS NOT FOUND$"
    RES DB "POSITION",13,10,"$"
.CODE
    START:
        MOV AX,@data
        MOV DS,AX
        MOV BX,00           ;LOW
        MOV DX,LEN          ;HIGH
        MOV CX,KEY          ;KEY
    AGAIN:
        CMP BX,DX
        JA FAIL
        MOV AX,BX
        ADD AX,DX
        SHR AX,1
        MOV SI,AX
        ADD SI,SI
        CMP CX,ARR[SI]
        JAE BIG             
        DEC AX          
        MOV DX,AX
        JMP AGAIN
    BIG:
        JE SUCCESS      
        INC AX          
        MOV BX,AX
    JMP AGAIN
    SUCCESS:
        ADD AL,01
        ADD AL,'0'
        MOV RES,AL
        LEA DX SUC
        JMP DISP
    FAIL:
        LEA DX,FAILURE
    DISP:
        MOV AH,09H
        INT 21H
        MOV AH,4CH
        INT 21H
    END START

2 个答案:

答案 0 :(得分:2)

add al, '0'将1位数字转换为相应的ASCII码,以便在屏幕上显示为成功时的结果值。

mov res, al将寄存器AL的值存储在变量RES的第一个字节中。只要RES指向字符串'POSITION',13,10,'$',AL的值将存储在此字符串的字符“P”之上,如果AL = 33h,则字符串变为'3OSITION',13,10,'$'

答案 1 :(得分:0)

bsearch proc term:DWORD,array:DWORD,asize:DWORD

    mov eax,array
    mov ecx,array
    add ecx,asize
    @@:
    cmp eax,ecx
    jg not_found
    mov edx,eax
    add edx,ecx
    shr edx,1
    xchg DWORD PTR [edx],eax
    cmp eax,term
    xchg DWORD PTR [edx],eax
    jg search_right
    jl search_left
    mov eax,edx
    sub eax,array
    ret
    search_right:
    mov ecx,edx
    jmp @B
    search_left:
    mov eax,edx
    jmp @B
    not_found:
    mov eax,-1
    ret
bsearch endp