To count the number of 1's in a byte[8 bits] the technique is as follows:-
We use RAR instruction to rotate the D0 bit via CARRY flag and check the status of carry flag for 1. And we count the number of times the CARRY flag is set[ie equal to 1].
Code-
MVI A,FFH ; Load A with Immediate data as FF
MVI B,08 ; Initialize B with 08 for counting
LABEL2: RAR ; Rotate Right through CARRY
JC LABEL1 ; Check for CARRY flag
DCR B ; Decrement counter
JNZ LABEL2 ; Check for number of counting
LABEL1: INR C ; Increment C for number of 1's
DCR B ;
JNZ LABEL2 ;
HLT
We use RAR instruction to rotate the D0 bit via CARRY flag and check the status of carry flag for 1. And we count the number of times the CARRY flag is set[ie equal to 1].
Code-
MVI A,FFH ; Load A with Immediate data as FF
MVI B,08 ; Initialize B with 08 for counting
LABEL2: RAR ; Rotate Right through CARRY
JC LABEL1 ; Check for CARRY flag
DCR B ; Decrement counter
JNZ LABEL2 ; Check for number of counting
LABEL1: INR C ; Increment C for number of 1's
DCR B ;
JNZ LABEL2 ;
HLT
No comments:
Post a Comment