Skip to main content

8085 ALP: transfer data with more than 3 ones

Short answer

Count the 1s in each byte by rotating right eight times, incrementing a counter when carry is set. If the count is 4 or greater, copy the byte to table2; otherwise store FFH. For instance, 0FH is copied, 07H becomes FFH.

Write an assembly language program for 8085. Table1 contains 16 no. of 8 bit data, transfer data which have number of 1s greater than 3, from table1 to table2, otherwise store FFH in table2.

Assumptions: table 1 is at 2000H (16 bytes) and table 2 at 2100H. Data and FFH are written at the same index in table 2.

Method: the number of 1s is counted by rotating the byte right 8 times and incrementing a counter each time CY = 1. CPI 04H sets CY if the count is less than 4. For 4 or more ones (more than 3) the original byte is stored; otherwise FFH is stored.

        LXI H,2000H     ; HL -> table 1 (16 bytes)
        MVI D,10H       ; 16 numbers
LOOP:   MOV A,M
        MOV B,A         ; keep the data in B
        MVI C,00H       ; C = number of ones
        MVI E,08H       ; 8 bits to test
CNT:    RRC
        JNC NEXT
        INR C           ; bit = 1
NEXT:   DCR E
        JNZ CNT
        MOV A,C
        CPI 04H         ; CY = 1 if ones < 4
        MOV A,B         ; A = data (MOV keeps the flags)
        JNC STORE       ; ones >= 4, i.e. more than 3 -> keep data
        MVI A,0FFH      ; otherwise store FFH
STORE:  INR H           ; table 2 = table 1 + 0100H (2100H)
        MOV M,A
        DCR H
        INX H
        DCR D
        JNZ LOOP
        HLT

Example: byte 0FH has four 1s, so 0FH is copied; byte 07H has three 1s, so FFH is stored.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Microprocessor chapter 2 questions →