What does ADD al, '0' do, and why use it before printing an integer digit?

耗尽温柔 提交于 2020-05-24 03:45:14

问题


I am a novice in assembly language programming
I searched for binary search program and found this and I tried understand the program. It's working fine but I couldn't understand the success part of the code:

what is ADD al,'0' and what is 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

回答1:


add al, '0' converts some 1 digit number to the corresponding ASCII code in order to display it at the screen as a result value on success.

mov res, al stores the value of the register AL in the first byte of the variable RES. As long as RES points to a string 'POSITION',13,10,'$', the value of AL will be stored on top of the character 'P' of this string and if, for example AL=33h, the string becomes '3OSITION',13,10,'$'.




回答2:


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


来源:https://stackoverflow.com/questions/19548760/what-does-add-al-0-do-and-why-use-it-before-printing-an-integer-digit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!