SDPL – Algorithms

Contents

Algorithms

In mathematics and computer science, an algorithm is a finite sequence of well-defined instructions, typically used to solve a class of specific problems or to perform a computation.

ALGORITHM Operation

Some operations require complexity and speed that is well outside the scope of the SDPL language itself, but are trivial to implement in c-like languages, so a facility is provided for performing various hard-coded operations on arrays.

Algorithms are accessed with the ALGORITHM operation.  Wow, that’s simple.

The algorithm command works on the provided data, running one or more algorithms in order as parameters, and returns the result separately.  The original data is never modified.  The ALGORITHM operation never modifies the status register.

Multiple Algorithm Chains

Multiple algorithms can be chained together in a simple ALGORITHM command as well, for simplicity and improved execution speed.

They are run in the order that they are specified.

Multi-Input Algorithms

Some algorithms operate on input pairs, which you should separate using a | symbol.  The ALGORITHM command will zero fill the LEFT hand side of a smaller array (for example AA and BB CC will result in 00 AA and BB CC), if you do not want this behavior, you should ensure the input arrays match.  All algorithms taking an input pair will output a single array.

Using input pairs on algorithms not designed for it will have undefined results.  Since all algorithms operating on input pairs would output a single array, chaining more than one multiple input algorithm together would be a very bad idea.

Available Algorithms

New algorithms will be added to the language as requirements for them increase.

  • SUM8, SUM16, SUM32 – Sum the input into 8, 16, or 32 bytes.
  • INVERT – Perform a binary NOT on each bit in the array
  • REVERSE – Reverse the order of the bytes in the array
  • TOASCII, FROMASCII – Convert the array to/from the ascii representation of the array in hex.
  • OR, AND, XOR – Perform the bitwise operation on a pair of arrays.

Examples

This example runs the SUM8 algorithm and, (because no return array is specified), places the result in the system return variable $*.  Print the result too.

ALGORITHM /SUM8 AA BB CC
PRINT /HEX $*

A return array can also be specified as the first term…

ALGORITHM %RESULT /SUM8 AA BB CC
PRINT /HEX *RESULT

You can leverage this to modify the original data, this example inverts the first two bytes of $X

ALGORITHM %X[0-1] /INVERT $X[0-1]

This example uses two algorithms changed together, and generates an inverted checksum (which is what the OBDXPro serial interface uses as a checksum….) and appends it to the array.

ALGORITHM /SUM8 /INVERT $DATA
APPEND %DATA $*