One of the SITCOM training computer prototypes
 SITCOM 85 Training Computer
 
 

Outputting multiple bits

Both previous examples showed you how to change just one bit. If you have indeed done some experimenting of your own, you may have toggled multiple bits at the same time. Toggling a few bits is the easiest part. But what if we have, let's say 3 bits, that must be send to the output without disturbing the rest of the output bits?

In our next example we're going to have a counter that counts binary from 0 to 7, requiring 3 bits. These three bits must be sent to the bits 2, 1 and 0 of output port A, without disturbing the other 5 bits of the output port.

 
   

;------------------------------------------------------------------------
;
; LESSON4C.ASM
;
; Binary counting in bits 2, 1 and 0 of port A only
;
;------------------------------------------------------------------------

            .IN   INIT02           Initialize the assembler

;------------------------------------------------------------------------

            MVI   A,11111111B      Switch all port B LEDs off
            OUT   PORTB
            MVI   A,00110000B      Just a random state of port A
            OUT   PORTA
            MVI   D,0              Clear the binary counter

COUNT       MOV   A,D              Mask only the bits 2, 1 and 0 of the
            ANI   00000111B         binary counter
            XRI   00000111B        Invert the bits (LED is on if bit = 0)
            MOV   B,A
            IN    PORTA            Mask all other bits of rest of the
            ANI   11111000B         output port
            ORA   B                Combine both bytes
            OUT   PORTA             and send it to the output
            MVI   B,200            Wait about one second
            CALL  DELAY
            INR   D                Increment binary counter
            JMP   COUNT            Repeat endlessly
 
 

So what's new in this program? First of all I show a different way of keeping a copy of the output port. Until now we have kept a copy of the output port in a processor register. In this example we keep a copy of the output port in the output port itself!
This can be done with the 8255 because reading the port will return a copy of the output latch which holds the actual output pattern. The IN PORTA instruction makes a copy of the output latch in the Accu.
Be careful though! The 8255 is a rather simple I/O device, very suitable for a novice assembly programmer. More sophisticated I/O devices can mix inputs and outputs on a single output port. On those devices it is not recommended to use this technique of keeping a copy of the output port, because that may disrupt the input function of the port.

Here's how this program works:
First both ports are initialized with an arbitrary output pattern. Any pattern will do, all unaffected bits will not change by the actions of this program.
Then we arrive at the line containing the label COUNT. There we make a copy of the binary counter into the Accu, after which this copy is truncated to 3 bits by the ANI 00000111B instruction. No matter how many bits are actually counting internally, we will only show the 3 least significant bits. Then the XRI 00000111B instruction inverts the 3 remaining bits, to adjust for the fact that the LEDs only light when a "0" is output. The result is then temporarily stored in register B.
Then we read the present contents of the output port using the IN PORTA instruction. This pattern is masked with the bits that have to remain unchanged, leaving the 3 least significant bits "0". A simple ORA B instruction will combine both bytes. ORA again is the OR function, just like ORI, but this time the second operand is held in a processor register instead of being an immediate value. Now it's time to send the combined byte to the output again.
The DELAY routine is nothing new. It is followed by the INR B instruction, which adds one to the binary counter. This completes the program, and we can loop back to the label COUNT.

 
 

Same goal, different bits

In our previous program we were lucky that we had to change the 3 least significant bits, because the neatly aligned with the 3 bits of the counter. But what if we had to output the counter in bits 6, 5 and 4 instead?
Here's the program that does just that:

 
   

;------------------------------------------------------------------------
;
; LESSON4D.ASM
;
; Binary counting in bits 6, 5 and 4 of port A only
;
;------------------------------------------------------------------------

            .IN   INIT02           Initialize the assembler

;------------------------------------------------------------------------

            MVI   A,11111111B      Switch all port B LEDs off
            OUT   PORTB
            MVI   A,00000011B      Just a random state of port A
            OUT   PORTA
            MVI   D,0              Clear the binary counter

COUNT       MOV   A,D              Roll bits 2, 1 and 0 into place
            RLC
            RLC
            RLC
            RLC
            ANI   01110000B        Mask the required bits
            XRI   01110000B        Invert them
            MOV   B,A
            IN    PORTA            Mask all other bits of rest of the
            ANI   10001111B         output port
            ORA   B                Combine both bytes
            OUT   PORTA             and send it to the output
            MVI   B,200            Wait about one second
            CALL  DELAY
            INR   D                Increment binary counter
            JMP   COUNT            Repeat endlessly
 
 

Only minor changes had to be applied to the previous program to get to this one. Mainly the bit masks were different, and we had to roll the 3 bits from 2,1,0 to 6,5,4. The 4 RLC instructions just do that.
Please note that it is best to roll the bits in place first, ignoring all bits that will be erased later anyway. That way you don't have to bother about what bits roll over to the other side, and what happens with the Carry.
After rolling the bits in place we mask the significant bits again, invert them and save the byte for later when it is combined with the masked current output pattern of the output port.

Hopefully this example teaches you that you should design your hardware carefully. Planning your output lines ahead will possibly reduce the program overhead of moving the bits in place.

 
  Continue To Lesson 5 - Introducing Inputs
 
  [Home] [Latest News] [Essentials] [Hardware] [The Build] [Programs] [Projects] [Downloads]
 
  Made in the UK