Previous Topic

Next Topic

Book Contents

Book Index

Example (Advanced): Combining semaphores and Branch instructions

This example follows on from the previous one, to give you an interesting way of testing and branching out depending on several semaphores. Imagine you have 3 semaphores, semA, semB and semC. These may represent inputs or the results of previous calculations. You now need to do one of several things depending on the semaphores. The following table summarizes what your program must do:

C

B

A

GoTo ...

N

0

0

0

DoIT

0

0

0

1

CanIt

1

0

1

0

GoAway

2

0

1

1

DoIt

3

1

0

0

GoAway

4

1

0

1

CanIt

5

1

1

0

OhDear

6

1

1

1

Oopsy

7

By careful planning we locate the 3 semaphores in bits 0, 1 and 2 of a single byte MySems. Here are the directives that define them:

MySems    mEQU       25       ;Where-ever in memory you want your semaphores stored
semA      sEQU       0
semB      sEQU       1
semC      sEQU       2

If we interpret those 3 bits as a binary number, they can be taken as a number N between 0 and 7 (i.e. 8 numbers). This represents exactly the 8 possible permutations of 3 semaphores. If you are unfamiliar with binary numbering you can find plenty of information online.

The following code will implement the above table in a compact Branch/Target structure:

          Recall      MySems
          LoadX       %00000111  ;Mask to select bits 0-2
          AndM                   ;Select the 3 semaphores
          Branch
          Target      DoIt       ;N=0
          Target      CanIt      ;N=1
          Target      GoAway     ;N=2
          Target      DoIt       ;N=3
          Target      GoAway     ;N=4
          Target      CanIt      ;N=5
          Target      OhDear     ;N=6
          Target      Oopsy      ;N=7

This code is both compact and very easy to maintain.

Exercise 1: Write the code to perform the same function without using a Branch/Target table.

Exercise 2: Modify the Branch/Target table so the program goes to Oopsy whenever A and B are both on.

Exercise 3: Modify the program in exercise 1 so it goes to Oopsy whenever A and B are both on.

For those of you with a chip level design bent, this method is really the same idea as using a 1-of-8 (74HC138) or a 1-of-16 decoder chip (74HC154) to generate boolean equations.

Previous Topic

Next Topic