Explanation
This 8051 assembly program's primary purpose is to read a set of ten numbers stored sequentially in memory, classify each number as either even or odd, and then store these classified numbers into two separate memory locations: one for even numbers and another for odd numbers.
Here's a step-by-step breakdown of its logic:
-
Initialization: The program starts by setting up three pointers and a counter.
R0is initialized to30H, acting as the "input pointer" to the first number in the original set.R1is initialized to40H, serving as the "even numbers pointer" where even numbers will be stored.R2is initialized to50H, serving as the "odd numbers pointer" where odd numbers will be stored.R3is initialized to0AH(decimal 10), which acts as a loop counter, indicating that ten numbers will be processed.
-
Main Loop (
CHECK): The program enters a loop that iterates ten times, once for each number in the input set.- Inside the loop, it first fetches a number from the memory location currently pointed to by
R0and loads it into the Accumulator (A).
- Inside the loop, it first fetches a number from the memory location currently pointed to by
-
Even/Odd Check: This is the core logic for classification.
- The instruction
ANL A, #01Hperforms a bitwise AND operation between the number in the Accumulator and the binary value00000001B. - If the original number was even, its Least Significant Bit (LSB) is 0. The result of
A AND 01Hwill be00H. - If the original number was odd, its LSB is 1. The result of
A AND 01Hwill be01H. - The
JZ EVENinstruction then checks the result in the Accumulator. IfAis00H(meaning the number was even), the program jumps to theEVENsection. IfAis01H(meaning the number was odd), the program continues to the instructions immediately followingJZ EVEN.
- The instruction
-
Handling Odd Numbers: If the number is odd, the following sequence is executed:
- The current value of
R1(the even pointer) is temporarily saved in internal RAM address60H. This is crucial becauseR1will be repurposed for storing the odd number. - The value of
R2(the odd pointer) is loaded into the Accumulator and then transferred toR1. Now,R1temporarily points to the next available location for an odd number. - The original number (which was determined to be odd) is reloaded into the Accumulator from the input memory location (
@R0), as the previousANLoperation modifiedA. - This odd number is then stored in the memory location pointed to by
R1(which is currently the odd numbers storage address). - The actual odd numbers pointer
R2is incremented to prepare for the next odd number. - Finally, the original value of
R1(the even pointer) is restored from60H. - A
SJMP NEXTinstruction then bypasses theEVENsection and jumps to theNEXTlabel.
- The current value of
-
Handling Even Numbers: If the number is even, the program jumps to the
EVENsection:- The original number (which was determined to be even) is reloaded into the Accumulator from the input memory location (
@R0). - This even number is then stored in the memory location pointed to by
R1(which is the even numbers storage address). - The even numbers pointer
R1is incremented to prepare for the next even number.
- The original number (which was determined to be even) is reloaded into the Accumulator from the input memory location (
-
Loop Progression (
NEXT): After processing either an odd or even number:- The input pointer
R0is incremented to move to the next number in the original set. DJNZ R3, CHECKdecrements the loop counterR3. IfR3is not zero, the program jumps back to theCHECKlabel to process the next number.
- The input pointer
-
Termination: When
R3becomes zero, all ten numbers have been processed, and the program terminates (END).