1

I'm trying to write an assembly code that will ask a user to enter 3 numbers and add them but it seems that there is something wrong with my following code, first of all, it shows me the result in ASCII format and it not giving me the write addition results.

.MODEL SMALL 
.STACK 100H
.DATA
    PROMPT_1 DB 'Enter the First number : $' 
    PROMPT_2 DB 'Enter the Second number : $' 
    PROMPT_3 DB 'Enter the Third number : $' 
    PROMPT_4 DB 'The Sum of three numbers is = $' 
    NUM1 DB ?
    NUM2 DB ?
    NUM3 DB ?
    Sum  DB ?
.CODE

  MAIN PROC 
    MOV AX, @DATA 
    MOV DS, AX ; initialize DS
; display massage and enter the first number and save it in num1 variable
    LEA DX, PROMPT_1 ; load and display the PROMPT_1 
    MOV AH, 9
    INT 21H
    MOV AH, 1 ; read a character
    INT 21H
    SUB AL, 30H ; save First digit in VALUE_1 in ASCII code 
    MOV NUM1,AL
    MOV AH, 2 ; carriage return
    MOV DL, 0DH
    INT 21H
    MOV DL, 0AH ; line feed
    INT 21H
; display massage and enter the Second number and save it in num2 variable
    LEA DX, PROMPT_2 ; load and display the PROMPT_2 
    MOV AH, 9
    INT 21H
    MOV AH, 1 ; read a character
    INT 21H
    SUB AL, 30H ; save First digit in NUM1 in ASCII code 
    MOV NUM2,AL
    MOV AH, 2 ; carriage return
    MOV DL, 0DH
    INT 21H
    MOV DL, 0AH ; line feed 
    INT 21H
; display massage and enter the third number and save it in num3 variable
    LEA DX, PROMPT_3 ; load and display the PROMPT_3
    MOV AH, 9
    INT 21H
    MOV AH, 1 ; read a character
    INT 21H
    SUB AL, 30H ; save First digit in NUM3 in ASCII code 
    MOV NUM3,AL
    MOV AH, 2 ; carriage return
    MOV DL, 0DH
    INT 21H
    MOV DL, 0AH ; line feed
    INT 21H
; Print massage in the prompt_4 on the screen 
    LEA DX, PROMPT_4 ; load and display thePROMPT_4 
    MOV AH, 9 
    INT 21H
; Calculate the sum of NUM1+NUM2+NUM3 
    MOV AL, NUM1
    ADD AL, NUM2
    ADD AL, NUM3
    
    MOV AH,0

    Add AL,30H ; convert ASCII to DECIMAL code 
    MOV AH,2 ; display the character
    MOV DL, AL
    INT 21H
    
    MOV AH,4CH
    INT 21H
  MAIN ENDP 
  END MAIN

if I run this code the results will be The Sum of three numbers is = >

( if I enter 3 4 5 for example )

Aak
  • 11
  • 3
  • 2
    3+4+5 is 12. You can't print 12 using a single character. – Michael Oct 06 '20 at 19:45
  • See e.g. https://stackoverflow.com/questions/15621258/assembly-printing-ascii-number or https://stackoverflow.com/questions/40503119/assembly-8086-sum-of-an-array-printing-multi-digit-numbers – Michael Oct 06 '20 at 19:50

0 Answers0