PRIYANKA CHETTRI
PRIYANKA CHETTRI

Reputation: 21

Printing a sum of three digit number in 8086

  ;program to print the sum of a three digit number 

.model small
.stack 100h
.data

msg1 db 10,13,"Enter a three digit number  $"  
msg2 db 10,13,"The sum of three digits : $"
value db 0
total db 0

.code                             
start:

       MOV AX, @data
       MOV DS, AX
       
       LEA DX, msg1                        
       MOV AH, 09h
       INT 21H
       
       
       
  read:
       
       
       MOV AH, 01
       INT 21H   
               
               
       CMP AL, 13
       JE calculate 
              
       MOV value, AL
       SUB value, 30h
              
              
       MOV AL, total
       MOV BL, 10
       MUL BL
       
       ADD AL, value
       MOV total, AL
       JMP read
                
                
  calculate:
       
       
       MOV AL, total
  
       AAM
      
       MOV CL, AL 
       
       AAM 
       
       ADD AL, AH
       ADD AL, CL
       
       MOV DL, AL  
       
       LEA DX, msg2
       MOV AH, 09h
       INT 21H
       
       MOV AH, 02
       INT 21H  
       
       MOV AH, 4CH
       INT 21H
      
end start

I am a beginner in Assembly Programming in 8086. And I have written a program to print the sum of a three digit number. I think I have successfully taken a three digit number as an input , and also the sum part using AAM seems to be correct but the output is an unwanted character. Could someone tell me where I went wrong?

Upvotes: 0

Views: 2101

Answers (1)

Abdullah Al Masum
Abdullah Al Masum

Reputation: 51

First of all, you have declared total as db(data byte) so total can hold 255(max)
So, this entire code is designed to give results between 0~255. If you enter above 255, then it will overflow when reading input, and operate on the result that's left.

Now, let's talk about the first point:
After pressing enter, control will jump calculate where total is loaded into al.

When aam(bcd adjust after multiply) will execute. here, AH = AL / 10 and AL = remainder
For example, with al=195, after the first aam ah=19 and al=5
When you run mov cl,al, al was 5.

Again aam is executed, So AH=AL / 10 or AH = 0 and AL = remainder or AL = 5
But our desired output was AH = 19/10 or AH=1 and AL=9

Second point is, you write MOV DL, AL but in next line, you write LEA DX, msg2. So, Previously stored value in DL will be lost. Thus,you will not see any output.
here is the corrected code:

  ;program to print the sum of a three digit number 

.model small
.stack 100h
.data

msg1 db 10,13,"Enter a three digit number  $"  
msg2 db 10,13,"The sum of three digits : $"
value db 0
total db 0

.code                             
start:

       MOV AX, @data
       MOV DS, AX
       
       LEA DX, msg1                        
       MOV AH, 09h
       INT 21H    
       
  read:       
       
       MOV AH, 01
       INT 21H   
               
               
       CMP AL, 13
       JE calculate 
              
       MOV value, AL
       SUB value, 30h
              
              
       MOV AL, total
       MOV BL, 10d
       MUL BL
       
       mov ah,0  
       ADD AL, value
       MOV total, AL
    
       JMP read
                                
  calculate:
       
       MOV AL, total
  
       AAM
      
       MOV CL, AL 
       
       mov al,ah    ;edited here
        
       AAM 
       
       ADD AL, AH
       ADD AL, CL
       
       MOV bL, AL   ;edited here
       add bl,30h   ;edited here

       LEA DX, msg2
       MOV AH, 09h
       INT 21H
       
       mov dl,bl    ;edited here
       MOV AH, 02
       INT 21H      ;output will be ascii charachter
       
       MOV AH, 4CH
       INT 21H
      
end start  

I am also a student, so let me know,if any doubt arise or not.

Upvotes: 1

Related Questions