SDPL – Examples

Contents

Subroutine example

Here are a few subroutine calls that construct “HELLO WORLD” and print it.

A subroutine is a section of code that uses the return operation to jump back to the call point.

SET %X "HELLO"
CALL *SUBROUTINE_A
PRINT $X

*SUBROUTINE_A
APPEND %X " "
CALL *SUBROUTINE_B
RETURN

*SUBROUTINE_B
APPEND %X "WORLD"
RETURN

Incremental loop example

This example uses a loop to build the array 00 01 02 03 04 05 in a variable Y and prints the result.  Each loop, the variable X is compared and incremented.

SET %X 00 # start X at zero
LABEL *LOOP_START
  COMPARE $X | 05
  IF > GOTO *COMPLETE
  APPEND %Y $X
  INC %X
  GOTO *LOOP_START
LABEL *COMPLETE
PRINT "LOOP RESULT:" /HEX $Y

Branching datastream operation

This example repeatedly sends an imaginary device a command AA BB CC.  The device replies with either EE with a suffix of a two byte status code, or FF with a suffix of a one byte status code.  We print the status code.  Bonus points if we examine invalid replies and then fail.

This example uses some language trickery, explained below.

*REPEAT

TX AA BB CC

RX /OR EE %STATUS[2]
RX /OR FF %STATUS[1]
RX /TAIL %UNKNOWN_REPLY

COMPARE $* | 01
IF < PRINT "TWO BYTE CODE: " /HEX $STATUS
IF = PRINT "ONE BYTE CODE: " /HEX $STATUS
IF > PRINT "UNKNOWN REPLY ERROR: " /HEX $UNKNOWN_REPLY
IF > FAIL

GOTO *REPEAT

The first trick is the fact that the PRINT command does not alter the status register, so the value of the COMPARE operation remains in the register and we can run multiple IFs against it without using any pointers or subroutines to branch, in fact we run two conditional > operations to both print and exit the program.

The second trick is that with three replies in an RX /OR list (0, 1, or 2), comparing against 1 allows us to use < for the first reply, = for the second, and > for the third.  More complex lists may require several compares.