Write an assembly language program to count the number of 1’s and 0’s in the binary bit system 11001101 = CDH.

Algorithm

Step 1 : Load register A (accumulator) with the given data

Step 2 : Load register B with 08H to set up a decrement counter

Step 3 : Load register C as counter to count the numbers of 1’s (initial value 00H)

Step 4 : Load register D as counter to count the number of 0’s (initial value 00H)

Step 5 : Rotate the content of Accumulator to left through carry

Step 6 : If on carry from Step 5 then jump directly to Step 9

Step 7 : Else increase counter C by 1

Step 8 : Jump unconditionally to Step 10

Step 9 : Increase counter D by 1

Step 10 : Decrease counter B by 1

Step 11 : Until B is not equal to 0 repeat from Step 5

Assembly Language:

Label Instructions Comments
MVI A, CDH; Loads register A (accumulator) with data stream
MVI B, 08H; Sets up decrement counter
MVI C, 00H; Sets counter to count number of 1’s
MVI D, 00H; Sets counter to count number of 0’s
jump2: RAL; Rotate accumulator left through carry
JNC jump1:; On no carry jumps to jump1:
INR C; Increases counter C by one
JMP jump3:; Jumps unconditionally to jump3:
jump1: INR D; Increases counter D by one
jump3: DCR B; Decreases counter B by one
JNZ jump2:; On B not equal to 0 jumps to jump2:
HLT; Terminates the program