Assembly language procedure to take 2-digit input

2019-09-06 12:51发布

问题:

I have a procedure which takes a 2-digit input from the user but I don't understand why a jump is made to JUMP01 if AL <= 9 and otherwise 7 is subtracted

INPUT PROC NEAR

    MOV AH,01
    INT 21H
    SUB AL,30H
    CMP AL,09
    JBE JUMP01
    SUB AL,07H
JUMP01:

    MOV DL,AL
    SHL DX,4
    MOV AH,01
    INT 21H
    SUB AL,30H
    CMP AL,09
    JBE JUMP02
    SUB AL,07H
JUMP02: 

    ADD DL,AL
    RET

INPUT ENDP

回答1:

This code is reading a 2 digit number in hexadecimal. The reason for the subtraction is that the letters A through F should be mapped to the range 10 through 15 (decimal). The ascii code of A is 65 which is reduced by 48 (the sub al, 30h) leaving 17, and to get to 10 from there it has to be reduced by a further 7. This works for the other letters too.



回答2:

You don't need to worry, this code just reads hex,assumes capital Letters and does not check about any other chars typed.

It first subtracts '0', so in case of a digit you will have a result le 9.

If not, it assumes you typed 'A'..'F', (0x41..0x46),and therefore subtracts 7 to get the correct result 0x0a..0x0f