- Asked 2/19
- 2078 Poush · 4 marks
Count number of 0s in ROM data
Short answer
Read the byte at ROM 0075H using MOVC. Rotate left eight times, incrementing a counter whenever carry = 0. Store the count in RAM 45H. Example: B7H gives 02H.
Write an assembly program for 8051 to count number of 0's in an 8-bit data stored in ROM at 75H and store result in RAM at 45H.
The byte at ROM address 0075H is read using MOVC, rotated left eight times through the carry, and a counter is incremented whenever the carry is 0. The count is stored in RAM location 45H.
ORG 0000H
MOV DPTR,#0075H ; DPTR points to the data byte in ROM
CLR A ; offset = 0
MOVC A,@A+DPTR ; A = byte stored at 0075H
MOV R0,#8 ; 8 bits to test
MOV R1,#0 ; R1 = count of 0s
NEXT: RLC A ; move MSB into carry
JC SKIP ; carry = 1 -> bit was 1, skip
INC R1 ; carry = 0 -> count the zero
SKIP: DJNZ R0,NEXT ; repeat for all 8 bits
MOV 45H,R1 ; store result in RAM 45H
HERE: SJMP HERE
ORG 0075H
DB 0B7H ; example data 10110111B
END
How it works:
MOVC A,@A+DPTRwith A = 0 reads the byte at 0075H.RLC Amoves each bit into carry;JC SKIPskips the counter when the bit is 1, so only zero bits are counted.- After 8 rotations R1 holds the number of zeros and is saved in 45H.
Example: B7H = 10110111B has two 0s, so 45H = 02H (checked in a simulator). Alternatively, the number of zeros equals 8 minus the number of ones.
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Count number of 1s in ROM data · Embedded System, chapter 9 · asked 2×
- C program: sum of two 8-bit BCD numbers · Embedded System, chapter 9