Assuming we are using a microcontroller with an 8-bit data bus and Port 0 is connected to output pins D0 to D7:
org 0000h ; program starting address
start: mov ah, 00h ; clear the high 8 bits of AX
mov al, 55h ; load AL with an arbitrary number (e.g. 01010101b)
mov bx, 0001b ; set BX to 00000001b for parity checking
check: test al, bx ; test the lowest bit of AL
jz even ; if it's zero, jump to even
mov al, 01h ; if it's one, set AL to 01h (odd parity)
jmp output ; jump to output
even: mov al, 80h ; set AL to 80h (even parity)
output: mov P0, al ; transfer the value of AL to Port 0
ljmp $ ; loop forever
end ; end of program
Explanation:
- We start by loading AH with zero to clear the high 8 bits of AX, then we load AL with an arbitrary number (here, 55h).
- We set BX to 00000001b, which will be used for parity checking. This is because the parity of a number is even if the number of 1 bits in it is even, which means the lowest bit of the number is either 0 or 1. Therefore, we only need to check the lowest bit of AL to determine its parity.
- We use the TEST instruction to bitwise AND AL and BX. This sets the Zero flag (ZF) to 1 if the result is zero, which means the lowest bit of AL is zero (even parity). If the result is not zero, we jump to the "odd" label to set AL to 01h (odd parity).
- If the parity is even, we set AL to 80h (binary 10000000), and if it's odd, we leave AL as it is. We then transfer the value of AL to Port 0 using the MOV instruction.
- Finally, we use the LJMP (long jump) instruction to loop the program back to the beginning (start label).
Note: The actual instructions for transferring the value of AL to Port 0 may vary depending on the microcontroller and its instruction set architecture. In this example, we assume that the value of Port 0 can be accessed directly as a memory address (P0).
Write an assembly program that :
Load AL by an arbitrary number, if there is parity even, transfer 80H to the Port 0, and if there is parity odd, transfer 01H to the Port 0.
1 answer