2

I'm relatively new to 8086. I was making a 4 function calculator using procedures. When I enter the input as:

first number:1

second number:1

choice:1 (1 is for addition)

I get the output as 'b'. Getting to my question, can anyone tell me why this is happening and what changes I should make to get my output as '2'?

DATA SEGMENT
    MSG1 DB 'Enter a number:$'          
    MSG2 DB 'Enter another number:$'    
    MSG3 DB '1 for addition$'
    MSG7 DB 'Enter choice(1-4):$'
DATA ENDS

CODE SEGMENT 
ASSUME CS:CODE,DS:DATA
START:
MOV AX,DATA          ;initialize DS     
MOV DS,AX            

MOV DX,OFFSET MSG1   ;print msg1
MOV AH,09H
INT 21H

MOV AH,01H           ;get user input part 1
INT 21H               

MOV BL,AL            ;store 1st number in BL

PRINTN 

MOV DX,OFFSET MSG2   ;print msg2
MOV AH,09H
INT 21H

MOV AH,01H           ;get user input part 2
INT 21H         

MOV CL,AL            ;store 2nd number in CL 

PRINTN

MOV DX,OFFSET MSG3   ;print msg3
MOV AH,09H
INT 21H           

MOV DX,OFFSET MSG7   ;print msg7
MOV AH,09H
INT 21H

MOV AH,01H           ;get user input part 3 (choice for add,sub etc)
INT 21H     

MOV DL,AL            ;store user's choice in dl

CMP DL,1             ;basically if-else 
JE L1                           

L1:PRINTN
CALL ADDITION        ;call addition proc
HLT


ADDITION PROC
    ADD BL,CL             ;add the numbers and store in bl
    MOV DX,OFFSET MSG8    ;print MSG8
    MOV AH,09H
    INT 21H

    MOV DL,BL             ;mov sum to dl
    MOV AH,02H            ;ah=2 for o/p
    INT 21H               ;interrupt  
    RET                   ;go back to main
ADDITION ENDP

Note: I've added the minimum code necessary, in case you want the entire code, let me know.

U. Watt
  • 533
  • 1
  • 5
  • 27

1 Answers1

1
MOV AH,01H           ;get user input part 1
INT 21H               
MOV BL,AL            ;store 1st number in BL

The comment "store 1st number in BL" is the culpritt here! What you get back from this DOS input function is a character. Best convert the character into the number that it represents. Simply subtract 48.

Shouldn't you verify that the input was indeed one of "0", "1", "2", ... , "9" ?

mov ah, 01h  ;get user input part 1
int 21h
sub al, '0'  ;'0' is the same as 48 (30h)
mov bl, al   ;store 1st number in BL

Do the same for the second number.
Now all calculations can act upon true numbers.
To display the (single character) result you need to convert the number back into a character. Simply add 48.

mov dl, b    ;mov sum to dl
add dl, '0'  ;'0' is the same as 48 (30h)
mov ah, 02h
int 21h
Fifoernik
  • 9,779
  • 1
  • 21
  • 27