Performance

The aim of this page is to discuss performance of embedded systems.

Trick: The Fork (Assembly, Two’s Complement)

This is a trick that I learned in Robin Sharp’s class at DTU many years ago. It is only relevant if you need to save some time in an inner loop, and you are willing to write at least some instructions in assembly language.

Having worked with DSP in bit-slice and assembler, I have actually used this trick many times.

Problem: You need to know whether a number is within a certain range.

y-axis
|
|
|------- Upper Limit A (Not Included) ----------- e.g. 17
|
| 
| Is X in this range?
|
|------- Lower Limit B (Included) ---------------- e.g 10
|
|
|
-----------------------------------------------------> X-axis

In the register where you have X  – e.g. the accumulator – do the following in the relevant assembler language:

Sub A
Add (A-B)
JNC OutOfRange

After each of the first two operations, the carry will be set if the sign changes.
In  other words, in the above figure, crossing the X-axis gives a carry (incl going from negative to 0 or vice-versa).

Now, after the first two operations, the Carry is set if B <= X < A. This means that you get to do two tests with only one possible jump.

Desktop Test:

X SUB 17 ADD 7
-3 -20,Cy=0 -13,Cy=0
9 -8, Cy=1 -1, Cy=0
10 -7, Cy=1 0, Cy=1
13 -4, Cy=1 3, Cy=1
16 -1, Cy=1 6, Cy=1
17 0, Cy=0 7, Cy=0
20 3, Cy=0 10, Cy=0