Q) Read the status of eight input bits from the Logic Controller
Interface and display ‘FF’ if the parity of the input read is even;
otherwise display 00
This program will read an 8 bit number from the logic controller and display ‘FF’ if that number is of even parity. Otherwise displays ‘00’ on logic controller. Also displays the number of ‘1’s present in the input number.
Algorithm:
Step 1: Initialize the control word i.e., 82H of logic controller.
Step 2: Store the control word in the Control register and initialize the Port B address.
Step 3: Read the 8-bit status into AL from the input Port-B of logic controller.
Step 4: Next step is to determine the number of 1’s in AL register value.
Step 5: To check whether AL register contains even or odd number.
Step 6: If AL contains even number, it is even parity and display FF.
Step 7: If AL contains odd number, it is odd parity and display 00.
Step 8: Finally display the number of 1’s that is the value of AL.
Step 9: Stop.
CODE:
Explanation:
Programming model selected contains one data segment(DS) and one code segment (CS).
Our data segment contains three memory locations assigned with port addresses of the logical interface. Port A(PA), Port B(PB) and Control registers(CR) are equated with appropriate port addresses.
Code segment starts with initializing of data segment at the line 3 and 4 in the snippet.
Control register port address (CR) is moved into DX register. Also port address (82H which is port C, which is also called as Control ward) is moved into AL register. Ports are then activated using OUT command (OUT DX,AL)
Next we have to display whether parity is even or odd and display ‘FF’ if even parity. Otherwise display ‘00’.
Next is counting the number of 1s in the inputted 8 bit number
Then move 0 to AL, stating that initially number of 1s is 0. You should increment 1s each time you get a 1 in the input number. CL is assigned to 8, We should run the comparison for 1, 8 times since input is 8 bit. CL acts as a counter.
Now we have the original 8 bit number in BL (remember we had backed up in BL at the beginning). Right rotate it and check the carry flag. (since carry flag contains the rightmost bit after ROR). If carry was set, it means the number is 1, hence count it by increment AL. If carry isn’t set, jump to DOWN1 label for next number.
Each time the number is checked, decrement the loop counter CL, and if it is not equal to 0, it means you still have numbers to be checked. Go to label UP to check for more.
Finally after 8 comparisons, that is when CL becomes 0, the number of 1s will be in AL. Display it using OUT command and terminate.
Code segment ends and so does START label.
Output:
Output for this program will be seen in logic controller.
(see the DELAY and Executing explanations in MP hardware basics page).
This program will read an 8 bit number from the logic controller and display ‘FF’ if that number is of even parity. Otherwise displays ‘00’ on logic controller. Also displays the number of ‘1’s present in the input number.
Algorithm:
Step 1: Initialize the control word i.e., 82H of logic controller.
Step 2: Store the control word in the Control register and initialize the Port B address.
Step 3: Read the 8-bit status into AL from the input Port-B of logic controller.
Step 4: Next step is to determine the number of 1’s in AL register value.
Step 5: To check whether AL register contains even or odd number.
Step 6: If AL contains even number, it is even parity and display FF.
Step 7: If AL contains odd number, it is odd parity and display 00.
Step 8: Finally display the number of 1’s that is the value of AL.
Step 9: Stop.
CODE:
Explanation:
ASSUME CS:CODE,DS:DATA
DATA SEGMENT
CR EQU 0E803H
PA EQU 0E800H
PB EQU 0E801H
DATA ENDS
Programming model selected contains one data segment(DS) and one code segment (CS).
Our data segment contains three memory locations assigned with port addresses of the logical interface. Port A(PA), Port B(PB) and Control registers(CR) are equated with appropriate port addresses.
CODE SEGMENT
START:
MOV AX,DATA
MOV DS,AX
Code segment starts with initializing of data segment at the line 3 and 4 in the snippet.
MOV DX,CRWe are activating ports of the logic controller. That is done in this way.
MOV AL,82H
OUT DX,AL
Control register port address (CR) is moved into DX register. Also port address (82H which is port C, which is also called as Control ward) is moved into AL register. Ports are then activated using OUT command (OUT DX,AL)
MOV DX,PBInput is then taken from Port B using IN instruction(to do this, port b address must be in DX). The value inputted is stored in register AL.
IN AL,DX
Next we have to display whether parity is even or odd and display ‘FF’ if even parity. Otherwise display ‘00’.
MOV DX,PAOutput port address is moved into DX. And AL is backed up into BL.(for checking number of 1s in AL later).
MOV BL,AL
AND AL,ALAL is ANDed with itself to check for parity(AL will remain same after ANDing since we are ANDing with itslef). If the resulting number is of even parity, it means AL is even parity number so jump to label ‘FF’ to display the same and after displaying, jump to label COUNT. Otherwise move ‘0’ to AL and display. For displaying, we need output port address in DX which is done in previous snippet. OUT command is used to display the contents of AL through Port A.
JP FF
MOV AL,0
OUT DX,AL
JMP COUNT
FF:
MOV AL,0FFH
OUT DX,AL
Next is counting the number of 1s in the inputted 8 bit number
COUNT:Call some delay, otherwise you wont be able to see the parity indicator ‘FF’ or ‘00’ in the controller.
CALL DELAY
MOV AL, 0
MOV CL,08
Then move 0 to AL, stating that initially number of 1s is 0. You should increment 1s each time you get a 1 in the input number. CL is assigned to 8, We should run the comparison for 1, 8 times since input is 8 bit. CL acts as a counter.
UP:UP is a label.
ROR BL,01
JNC DOWN1
INC AL
DOWN1:
DEC CL
JNZ UP
OUT DX, AL
MOV AH,4CH
INT 21H
Now we have the original 8 bit number in BL (remember we had backed up in BL at the beginning). Right rotate it and check the carry flag. (since carry flag contains the rightmost bit after ROR). If carry was set, it means the number is 1, hence count it by increment AL. If carry isn’t set, jump to DOWN1 label for next number.
Each time the number is checked, decrement the loop counter CL, and if it is not equal to 0, it means you still have numbers to be checked. Go to label UP to check for more.
Finally after 8 comparisons, that is when CL becomes 0, the number of 1s will be in AL. Display it using OUT command and terminate.
DELAY PROC NEARDelay procedure is same for all programs.
PUSH CX
PUSH BX
MOV CX, 0FFFFH
B1: MOV BX, 0FFFFH
B2: DEC BX
JNZ B2
LOOP B1
POP BX
POP CX
RET
DELAY ENDP
CODE ENDS
END START
Code segment ends and so does START label.
Output:
Output for this program will be seen in logic controller.
(see the DELAY and Executing explanations in MP hardware basics page).
Share your views about this article!