8051 Program to find number of Odd numbers in a block

8051-programs
8051-programs

8051 Program to find number of Odd numbers in a block:

8051 Program to find number of Odd numbers in a block is as follows:

MOV DPTR,#5000H
MOV R3,#0AH
MOV R2,#00H
CLR C
UP:MOV B,#02H
MOVX A,@DPTR
DIV AB
MOV R0,B
CJNE R0,#00,SKIP
JMP L1
SKIP: INC R2
L1:INC DPTR
DJNZ R3,UP
RET

Logic for 8051 Program to find number of Odd numbers in a block:

Logic for 8051 Program to find odd numbers in a block is as follows:
The idea behind checking odd numbers is division  by 2. If it is divisible by 2 the number is a even number else it is a odd number. If it isn’t divisible by 2 we can conclude that it is an odd number. In above program 10 numbers to be searched are stored from memory location 2500H -2509H. Register R3 is a counter to count 10 numbers from the block. Register R2 is a counter to count even numbers found in the block. Register R2 is initialized with zero count. In this program number 2 will be our divisor, so we will be storing 02 in register B. Then we start fetching values from the block. Numbers are fetched from the block and stored in register A (Accumulator). Arithmetic Division is carried out between A and B. Contents of A are divided by contents of register B. After execution of division, quotient is stored in register A and the remainder goes to register B.

Note: Division can only be performed using Register A and Register B, No other memory location or register can perform division operation in 8051 series of microcontroller.

Then contents of B that is the remainder is stored in Register R0. Register R0 is then compared with 00H. If contents of R0 are 00 then R2 which is our counter for even numbers is incremented by 1. If R0 has some value except zero, increment process is skipped and next number is fetched from the block. This process will continue until the counter register R3 becomes zero. And after successful execution results will be available at memory location 20H.

Some More Similar Programs:
8051 program to find 1’s in a byte.
8051 Program to find largest number in a block.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here