Hello. I would like to ask about adding two-digit numbers. There is a part there where it has to be converted to ascii code, my question is why it has to be converted to ascii code? What will its effect?

Thank you for your help. I really need this problem to be solved to finish this course and graduate. THANKS! :)

.MODEL SMALL
.STACK
.DATA
    MSG1    DB  10,13,  'Enter First Number: $'
    MSG2    DB  10,13,  'Enter Second Number: $'
    MSG3    DB  10,13,  'SUM: $'
    MSG4    DB  10,13,  'DIFFERENCE: $'
    MSG5    DB  10,13,  'PRODUCT: $'
    MSG6    DB  10,13,  'QUOTIENT: $'
    NEGA    DB  '-$'

    NUM1    DB  0
    NUM2    DB  0
    DIG1    DB  0
    DIG2    DB  0
    ANS     DB  0

.CODE

MAIN PROC
    MOV AX,@DATA
    MOV DS,AX

ENT1:
    MOV DX,OFFSET MSG1  ;display prompt for first number
    MOV AH,09H
    int 21h

    MOV AH,01H      ;input first number
    INT 21H

    CMP AL,'0'      ;check if it is in range from 0 - 9
    JB ENT1
    CMP AL,'9'
    JA ENT1

    SUB AL,30H      ;convert to real number entered
    MOV DIG1,AL

    MOV AH,01H      ;input first number
    INT 21H

    CMP AL,'0'      ;check if it is in range from 0 - 9
    JB ENT1
    CMP AL,'9'
    JA ENT1

    SUB AL,30H      ;convert to real number entered
    MOV DIG2,AL

    MOV AL,DIG1     ;convert 1st digit to tens place
    MOV BL,10
    MUL BL

    MOV NUM1,AL     ;add 1st digit to 2nd digit
    MOV AL,DIG2
    ADD NUM1,AL


ENT2:
    MOV DX,OFFSET MSG2  ;display prompt for second number
    MOV AH,09H
    int 21h

    MOV AH,01H      ;input second number
    INT 21H

    CMP AL,'0'      ;check if it is in range from 0 - 9
    JB ENT2
    CMP AL,'9'
    JA ENT2

    SUB AL,30H      ; convert to real number entered
    MOV DIG1,AL

    MOV AH,01H      ;input second number
    INT 21H

    CMP AL,'0'      ;check if it is in range from 0 - 9
    JB ENT2
    CMP AL,'9'
    JA ENT2

    SUB AL,30H      ;convert to real number entered
    MOV DIG2,AL

    MOV AL,DIG1
    MOV BL,10
    MUL BL

    MOV NUM2,AL
    MOV AL,DIG2
    ADD NUM2,AL


ADDITION:
    MOV BL,NUM1
    ADD BL,NUM2

    CALL CHANGE

    MOV DX,OFFSET MSG3
    CALL RESULT

SUBTRACTION:
    MOV BL, NUM1
    CMP BL, NUM2
    JL LESS

    SUB BL, NUM2

    CALL CHANGE

    MOV DX,OFFSET MSG4
    CALL RESULT


LESS:
    MOV BL, NUM2
    SUB BL, NUM1

    CALL CHANGE

    MOV DX, OFFSET MSG4
    MOV AH,09H
    int 21h

    MOV DX, OFFSET NEGA
    CALL RESULT 

    MOV AH,4CH      ; exit to DOS
    INT 21H
MAIN ENDP

CHANGE PROC
    MOV AH,0
    MOV AL,BL

    MOV BL,10
    DIV BL

    MOV BL,AL
    MOV BH,AH

    ADD BH,30H      ; convert to ascii code
    MOV ANS,BH

    MOV AH,0
    MOV AL,BL
    MOV BL,10
    DIV BL

    MOV BL,AL
    MOV BH,AH

    ADD BH,30h      ; convert to ascii code
    ADD BL, 30h     ; covert to ascii code

    RET
CHANGE ENDP

RESULT PROC

    MOV AH,09H
    INT 21H

    MOV DL,BL
    MOV AH,02H
    INT 21H

    MOV DL,BH
    MOV AH,02H
    INT 21H

    MOV DL,ANS
    MOV AH,02H
    INT 21H

    RET
RESULT ENDP


END

Interesting. You wrote a 183 line program, but yet you don't understand how CHANGE works.

Anyway, conversion is required because the digits you seen on the screen do not have the same internal representation as thier binary value, but in the low nibble there is a similarity, meaning;

'0' = 30H = 0011 0000

Therefore SUB AL,30H strips the high nibble to make the binary number and ADD AL,30H the ASCII equivalent again. Problem is, when the result is greater than 9, the units, tens, hundreds and so on must be calculated. This is where lines 140 & 151 come in. The best thing to do would be trace through the CHANGE procedure with DEBUG and watch what happens.

commented: Thank you so much! Actually, we were two, me and my partner made this program. He edited some part where I got some mistakes including converting to ascii code. Btw, thank you so much for your answer. +0

I tried running this, how come the answer sometimes results into having two differences

What did you enter and what were the results?

got it fixed, I put a JMP after 115, that jumps to 131...it doesn't need to enter LESS if it NUM1 is greater than NUM2

what should be done for this to accept 1 digit numbers instead of adding a zero first

e.g. accept 8 instead of 08 for first number and second number

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.