In this program we will see how to count number of 1's in an 8-bit number.
Write 8085 Assembly language program to count number of 1s in 8-bit number stored atlocation 8000H.
In this program we areusing the rotate operation to count the number of 1's. As there are8 different bits in 8-bit number, then we are rotating the numbereight times. we can use RRC or RLC. Here we have used the RRCinstruction. This instruction sends the LSb to MSb also to carryflag. So after each iteration we can check the carry status to getthe count of 1s.
If the number is DA (11011010) then the answer will be 5, as there are five 1s in the number.
Address | Data |
---|---|
. . . | . . . |
8000 | DA |
. . . | . . . |
Address | HEX Codes | Labels | Mnemonics | Comments |
---|---|---|---|---|
F000 | 37 | STC | Set Carry | |
F001 | 3F | CMC | ComplementCarry | |
F002 | 3A, 00, 80 | LDA 8000H | Load the number into A | |
F005 | 0E, 08 | MVI C,08H | Initialize the counter to 08H | |
F007 | 06, 00 | MVI B,00H | Clear B register | |
F009 | 0F | LOOP | RRC | Rotate right |
F00A | D2, 0E, F0 | JNC SKIP | If CY = 0,skip the next step | |
F00D | 04 | INR B | If CY = 1,increase B | |
F00E | 0D | SKIP | DCR C | Decrease C by1 |
F00F | C2, 09, F0 | JNZ LOOP | If Z = 0,jump to loop | |
F012 | 78 | MOV A,B | Load the count to A | |
F013 | 32, 50, 80 | STA 8050H | Store the result at 8050H | |
F016 | 76 | HLT | Terminate the program |
Address | Data |
---|---|
. . . | . . . |
8050 | 05 |
. . . | . . . |