Lesson 4So far we have been using the output ports byte-wide, outputting 8-bits at a time. What if we want to change the state of one output bit only, or maybe just 4 of them? That would require an other approach, which I will try to explain in this lesson. |
Toggling just 1 bitIn this example I'm going to show you how a program can make just one of the 8 LEDs of port A flash, all other LEDs remain unaffected. |
;------------------------------------------------------------------------ ; ; LESSON4A.ASM ; ; Flashing only one LED of port A ; ;------------------------------------------------------------------------ .IN INIT02 Initialize the assembler ;------------------------------------------------------------------------ MVI A,11111111B Switch all port B LEDs off OUT PORTB MVI A,00110101B Just a random state of port A FLASH ORI 00010000B Switch LED on port A4 off OUT PORTA MVI B,100 Wait about half a second CALL DELAY ANI 11101111B Switch LED on port A4 on OUT PORTA MVI B,100 Wait about half a second CALL DELAY JMP FLASH Repeat endlessly |
We don't need port B in this example, therefore we switch off all its LEDs (any other pattern would do too).
The fun really starts on the line containing the label FLASH.
Here we see a new instruction named ORI, which stands for OR immediately.
ORI works quite similar to the ANI instruction we have seen at work in Lesson 3.
Instead of performing the AND function on the bits in both operands (the Accu and the immediate value), it performs the OR function.
After the delay the ANI instruction is used to switch the LED of port A4 back on again, while only bit 4 is cleared by the ANI instruction. All other LEDs are unaffected by the "1" in the operand. By the way: The bit pattern behind the ORI and ANI instructions we see in this example are often called "a mask" or "bit mask", as they are used as mask to cover the bits of the original pattern that have to be changed. |
Doing it the clever way
In our previous example we used two separate instructions to show how to set and clear just a single bit in an output byte.
I deliberately showed it to you this way, because often we need an output bit to become set or cleared on command.
|
;------------------------------------------------------------------------ ; ; LESSON4B.ASM ; ; Flashing only one LED of port A, doing it the clever way. ; ;------------------------------------------------------------------------ .IN INIT02 Initialize the assembler ;------------------------------------------------------------------------ MVI A,11111111B Switch all port B LEDs off OUT PORTB MVI A,00110101B Just a random state of port A FLASH XRI 00010000B Toggle LED on port A4 OUT PORTA MVI B,100 Wait about half a second CALL DELAY JMP FLASH Repeat endlessly |
Obviously the XRI instruction does the trick of toggling bit 4, as the program seems to be working the same as the previous one.
XRI is the third so called logical function of the 8085, ANI and ORI are the others two.
Being a function of the same class, its operation is quite similar to the others.
This time the EXOR function is performed on all the bits of both operands.
Obviously all mask bits with the value "0" don't affect the result.
Let's see why:
If the original bit is "0", they are both "0", so the result will be "0".
Net result, the bit is not changed.
If the original bit is "1", they both differ, so the result will be "1". Net result, the bit is not changed.
All mask bits with the value "1" effectively invert the bit.
Here's why:
If the original bit is "0", they both differ, so the result will be "1".
Net result, the bit is inverted.
If the original bit is "1", they are both "1", so the result will be "0". Net result, the bit is inverted.
Experiment tip: |
Continue With Lesson 4 - Selective output. |
[Home] [Latest News] [Essentials] [Hardware] [The Build] [Programs] [Projects] [Downloads] | |||