Tuesday, 31 July 2018

Write a program in Assembly Language that finds the number of 1s in a byte.

.MODEL SMALL
.STACK 100H
.DATA
    DATA1 DB 21H
    COUNT DB ?

.CODE
MAIN PROC
    MOV AX, @DATA
    MOV DS, AX
   
    SUB BL,BL ;clear BL to keep number of 1s
    MOV DL,8 ;rotate total of 8 times
    MOV AL,DATA1
   
    AGAIN:
    ROL AL,1 ;rotate it once
    JNC NEXT ;check for 1
    INC BL ;if CF =1 than increment count
   
    NEXT:
    DEC DL ;go through this 8 times
    JNZ AGAIN ;if not finished go back
    MOV COUNT,BL ;save the number of ones
   
    MOV AH, 2
    ADD BL, 30H
    MOV DL, BL
    INT 21H 
   
    MOV AH, 4CH
    INT 21H
    MAIN ENDP
END MAIN

No comments:

Post a Comment