Search code examples
assemblyx86-16tasm

Nothing happens when I enter 2nd digit of 2nd numbers. What is wrong with the code?


DATA SEGMENT
MSG1 DB 10,13,"ENTER THE FIRST NUMBER: $"
MSG2 DB 10,13,"ENTER THE SECOND NUMBER: $"
MSG3 DB 10,13,"ANSWER: $"
ENDS

CODE SEGMENT
ASSUME DS:DATA,CS:CODE

START:
MOV AX,DATA
MOV DS,AX

LEA DX,MSG1
MOV AH,09H
INT 21H

MOV AH,01H
INT 21H
CALL INPUT8

MOV CL,AL
ROL CL,04H

MOV AH,01H
INT 21H
CALL INPUT8

ADD CL,AL

LEA DX,MSG2
MOV AH,09H
INT 21H

MOV AH,01H
INT 21H
CALL INPUT8

MOV BL,AL
ROL BL,04H

MOV AH,01H
INT 21H
CALL INPUT8
ADD BL,AL

ADD CL,BL

MOV BL,CL
ROL CL,04H
AND CL,0FH

LEA DX,MSG3
MOV AH,09
INT 21H

CALL OUTPUT8

MOV CL,BL
AND CL,0FH
CALL OUTPUT8



INPUT8 PROC

SUB AL,30H
RET
ENDP

OUTPUT8 PROC

ADD CL,30H
MOV DL,CL
MOV AH,02
INT 21H

RET
ENDP

MOV AH,4CH
INT 21H

ENDS
END START

Output in command prompt:

Output in command prompt


Solution

  • You capture the characters from keyboard and convert them into hex numbers in BL and CL, this happens because you use the higher 4 bits of BL to hold one digit and the lower 4 bits to hold the second digit, with decimal numbers this techinque would not work. Then you execute an addition, but, both numbers are hex so the result could contain letters (A..F). In that case, the procedure OUTPUT8 needs to check if the number in CL is a digit (0..9) or a letter (A..F). Also the interrupt to finish the program was not at the right place. Here are both fixes:

    DATA SEGMENT
    MSG1 DB 10,13,"ENTER THE FIRST NUMBER: $"
    MSG2 DB 10,13,"ENTER THE SECOND NUMBER: $"
    MSG3 DB 10,13,"ANSWER: $"
    ENDS
    
    CODE SEGMENT
    ASSUME DS:DATA,CS:CODE
    
    START:
    MOV AX,DATA
    MOV DS,AX
    
    LEA DX,MSG1
    MOV AH,09H
    INT 21H
    
    MOV AH,01H
    INT 21H
    CALL INPUT8
    
    MOV CL,AL
    ROL CL,04H
    
    MOV AH,01H
    INT 21H
    CALL INPUT8
    
    ADD CL,AL
    
    LEA DX,MSG2
    MOV AH,09H
    INT 21H
    
    MOV AH,01H
    INT 21H
    CALL INPUT8
    
    MOV BL,AL
    ROL BL,04H
    
    MOV AH,01H
    INT 21H
    CALL INPUT8
    ADD BL,AL
    
    ADD CL,BL
    
    MOV BL,CL
    ROL CL,04H
    AND CL,0FH
    
    LEA DX,MSG3
    MOV AH,09
    INT 21H
    
    CALL OUTPUT8
    
    MOV CL,BL
    AND CL,0FH
    CALL OUTPUT8
    
    MOV AH,4CH             ;◄■■ THIS CODE COMES FROM
    INT 21H                ;◄■■ 26 LINES BELOW.
    
    INPUT8 PROC
    
    SUB AL,30H
    RET
    ENDP
    
    OUTPUT8 PROC
    cmp cl,9                    ;◄■■ IF CL <= 9
    jbe digit                   ;◄■■ JUMP TO "DIGIT".
                                ;◄■■ NO JUMP IF CL > 9.
    ;IF NUMBER IS LETTER (A..F). ◄■■
    add cl, 57h                 ;◄■■ CONVERT 10..15 TO 'A'..'F'.
    jmp output8display          ;◄■■ SKIP THE "DIGIT" BLOCK.
    
    ;IF NUMBER IS DIGIT (0..9). ;◄■■
    digit:                      ;◄■■
    ADD CL,30H
    
    output8display:             ;◄■■ DISPLAY CL (DIGIT OR LETTER).
    MOV DL,CL
    MOV AH,02
    INT 21H
    output8end:
    RET
    ENDP
    
    ;MOV AH,4CH             ;◄■■ THIS CODE SHOULD
    ;INT 21H                ;◄■■ BE 26 LINES ABOVE.
    
    ENDS
    END START