Count Elements in Array – Assembly Program


Write and run a program using 8086 assembly language that counts the number of non-zero numbers stored in a byte array of size five. The array is stored in the memory. The count is left in AL register. For example, if the memory contains the array :
51 00 31 00 25
then the program should output 3 in AL register. [June 2015, Set-2]

DATA SEGMENT
    ARRAY DB 51H, 00H, 31H, 00H, 05H
    COUNT DB ?
DATA ENDS

CODE SEGMENT
START:

    ASSUME CS:CODE, DS:DATA 
    MOV AX, DATA
    MOV DS, AX 
        
    MOV SI, 0000H
    MOV CX, 5H
       
  NEXT: 
    MOV AL, ARRAY[SI]
    
    CMP AL,00H
    JNE ONE
    
    JE TWO
    
    ONE:
    ADD BL, 01H
    
    TWO:
    
    ADD SI,2   
    LOOP NEXT 
    
    MOV COUNT,BL 
    
    MOV AX, 4C00H
    INT 21H  
        
    MOV AX, 4C00H
    INT 21H    
CODE ENDS

END START