Vasily S. answered 3d
Top 1% Teacher, Targeted Strategies for 20+ Point Gains—Book Now
Initialization:
MOV AL, CW_VALUE: The 8086 CPU loads the control word 90H into its AL register.
OUT CWR, AL: The CPU sends this value from AL to the 8255's Control Word Register at address 63H. This one-time command configures Port A as an input port and Port B as an output port.
Main Loop Start:
MAIN_LOOP:: This is a label marking the beginning of the infinite loop. The program will run from this point forward.
Read Switch Inputs:
IN AL, PORT_A: The CPU reads one byte (8 bits) of data from Port A (address 60H). The state of the two switches (S0 at bit 0, S1 at bit 1) is captured in the AL register. The other 6 bits are ignored.
Example: If S1 is ON (1) and S0 is OFF (0), AL might contain 1101 0010B. We only care about the last two bits.
Isolate Switch 0 (PA0):
MOV BL, AL: A copy of the input byte is placed in the BL register for temporary storage.
AND BL, 01H: The BL register is bitwise ANDed with 0000 0001B. This mask zeroes out all bits except bit 0. BL now holds 01H if S0 was on, or 00H if S0 was off.
Isolate Switch 1 (PA1):
AND AL, 02H: The AL register is bitwise ANDed with 0000 0010B. This mask zeroes out all bits except bit 1. AL now holds 02H if S1 was on, or 00H if S1 was off.
SHR AL, 1: The AL register is shifted one position to the right. This moves the S1 bit from position 1 to position 0. AL now holds 01H if S1 was on, or 00H if S1 was off.
Perform Logical AND:
AND AL, BL: This is the core logic. The CPU performs a bitwise AND between AL (holding S1's value) and BL (holding S0's value).
The result in AL will be 01H only if both AL and BL were 01H.
In all other cases (0 AND 0, 0 AND 1, 1 AND 0), the result will be 00H.
Format for Output:
SHL AL, 2: The LED is connected to bit 2 (PB2). This instruction shifts the 1-bit result (01H or 00H) two positions to the left.
If the result was 01H (ON), AL becomes 0000 0100B, which is 04H.
If the result was 00H (OFF), AL remains 00H.
Write to LED:
OUT PORT_B, AL: The CPU sends the value in AL (either 04H or 00H) to Port B at address 61H.
If 04H is sent, bit 2 becomes high, and the LED turns ON.
If 00H is sent, bit 2 becomes low, and the LED turns OFF.
Loop:
JMP MAIN_LOOP: This instruction tells the CPU to jump back to the MAIN_LOOP label. This ensures the switches are read and the logic is re-calculated continuously, making the system responsive in real-time.