One of the SITCOM training computer prototypes
 SITCOM 85 Training Computer
 
 

Lesson 4

So 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 bit

In 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).
Port A is initialized to a random pattern, again any pattern will do just fine.

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.
With the OR function the result bit will be "1" if a bit in one operand OR a bit in the other operand (or both) is "1". Remember that the AND function only results in a "1" if the bits in both operands are "1".
So all bits with the value "0" of the immediate operand in our example do not affect the bits already present in the Accu. Those LEDs will not be changed by the ORI instruction. Only bit 4, which contains a "1" in the operand, will be affected and will become "1" no matter what it was before.

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.
But since we are only interested in a flashing LED at this moment, we can do it the clever way. Do you remember how we flashed all LEDs of the outputs in LESSON1C.ASM? We used a CMA instruction, which inverted all the bits of the Accu at the same time.
Wouldn't it be nice if we could do the same on a single bit level? Of course I wouldn't bring this up if we couldn't, so here it is:

 
   

;------------------------------------------------------------------------
;
; 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.
An EXOR function returns a "0" if the bits in both operands are equal. Needless to say that it will return a "1" if both bits differ. I fear that this one needs a bit more explanation as the AND and OR functions.

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:
The mask in our examples affect just one output bit, which was our goal. But would it work with multiple bits too?
Or can you toggle two LEDs of port A (one LED switching on, while the other goes off at the same time) without using the XRI instruction? Using the XRI instruction would simplify this experiment, but it won't teach you anything new.

 
  Continue With Lesson 4 - Selective output.
 
  [Home] [Latest News] [Essentials] [Hardware] [The Build] [Programs] [Projects] [Downloads]
 
  Made in the UK