8051 Program to find even numbers in a block:
8051 Program to find even 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 INC R2 SKIP: INC DPTR DJNZ R3,UP MOV 20H,R2 RET
Logic for 8051 Program to find even numbers in a block:
Logic for 8051 Program to find even numbers in a block is as follows:
The idea behind checking even numbers is to divide a number by 2. If it is divisible by 2 the number is a even number. If it isn’t divisible by 2 we can conclude that it is an odd number. In above program 10 numbers are stored from memory location 2500H -2509H. R3 is a counter to count 10 numbers from the block. R2 is a counter to count even numbers found in the block. 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 fetch values from the block. Numbers from the block are stored in register A (Accumulator). 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.