0

This is my program to print even numbers in 8086 for n>10. My logic seems to be correct, but it's printing 00 50 50... and so on. I am a beginner in 8086 programming a help would be very much appreciated.

When I emulate the code I have seen that right after the AAM line the value of the AL register which was 2 changes to 5.

;Program to print even numbers using loop  for multiple  digit range
                                                      
                                                      
.model small  

.stack 100h 

.data      

n db '?'
msg1 db 10,13,"Enter the range : $"
msg2 db 10,13, "The even numbers are $"  
value db 0
total db 0

.code
begin:
       MOV AX, @data
       MOV DS, AX  
       
       LEA DX, msg1
       MOV AH, 09h
       INT 21H
       
       
       
 read:             ;to read digits more than 1
       
       
       MOV AH, 01
       INT 21H   
               
               
       CMP AL, 13
       JE message 
              
       MOV value, AL
       SUB value, 30h
              
              
       MOV AL, total
       MOV BL, 10
       MUL BL
       
       ADD AL, value
       MOV total, AL
       JMP read
            
            
       MOV AL, total     
            
       MOV n , AL
       
       
            
       
 message:
 
       LEA DX, msg2
       MOV AH, 09h
       INT 21H  
       
                   
                   
            
       ;initialising variables
       
 
       MOV CL, 0 
       MOV BL, 0
        
      
 even:
     
     
        
       MOV AL, BL
       AAM 
  
       ADD AX, 3030h  
       MOV BX, AX
 
       MOV AH, 2
       MOV DL, BH
       INT 21H   
       
     
       MOV AH,2
       MOV DL, BL
       INT 21H
     
            
       ADD BL, 2
       MOV DL, ' '
       INT 21H
       INC CL
       CMP CL, n
       JNE even
      
      
      MOV AH, 4CH
      INT 21H
      
end begin
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • What output do you expect? – ecm May 04 '21 at 13:43
  • for n =10, I expect even numbers from 0 to 18 , mixture of both single and a double digit number – PRIYANKA CHETTRI May 04 '21 at 13:56
  • 1
    You're skipping the line that would initialise `n` so it will loop for 256 iterations rather than `total` iterations. After the `even` label you use `bl`. It was correctly initialised to zero before the loop. However, you're overwriting it with the numeric digits' ASCII codes in `mov bx, ax`. You need to understand that `bl` is a part of `bx`, which you already do use (to get one of the ASCII codes) but you seem to have forgotten that you can't keep your counter in `bl` while using `bx`. For a very simple solution add `push bx` before `mov bx, ax` and `pop bx` before `add bl, 2`. – ecm May 04 '21 at 13:58
  • 2
    [Adding two numbers to make a two digit number](https://stackoverflow.com/q/26678900) uses AAM to print a 2-digit number, and shows how to handle 3-digit numbers. My answer on [Displaying Time in Assembly](https://stackoverflow.com/q/37129794) shows `div` and `aam` ways, printing 1 digit at a time. – Peter Cordes May 04 '21 at 14:21

0 Answers0