Example: Scan table for >= byte match, get floating point number

This example is a little more interesting. Instead of using X to directly select a table record (row), it scans the table for a "greater than or equals" match with X and then fetches the corresponding floating point number. Here is the data table in "spreadsheet" form, one row per record.

5 12.0
89 78.9
127 -98
255 0.0

The left hand column contains byte entries which will be matched for >= X. The right hand column contains the corresponding floating point "answers". The object of the exercise is to scan down the lefthand column for the first entry that is greater than or equal to X, then return the floating point number next to it.

You will notice that the last entry has 255 as its "X" value. That is the largest number that can ever be contained in X, and so our scan of the lefthand column is guaranteed to terminate on that entry, if not before.

The code for this example is a bit more elaborate than the previous examples. It initializes the NVEM registers, scans the table in a loop and finally retrieves the resulting floating point number.

The first 3 instructions initialize the NVEM registers. This includes setting the record number to 0, because we are going to scan the table from the top.

Within the loop we first access the byte entry from the record number currently selected by the record number register. Pushing the table byte into X pushes the original X value down the stack into Y. We then compare the 2 numbers in X and Y using a CompareR, leaving a result in the R (for Result) register. The BranchR instruction gives a (in this case) 3-way branch (split) on the contents of R, with the 3 Target lines nominating where to Branch to for each of the 3 possible outcomes.

If the table byte is smaller than the original X value, we wind up at the program line Smaller. There we increment the record number and go back to Loop to try the next record.

If the table byte is greater than or equal to the original X value, we wind up at the program line Equal or Bigger. There we read the floating point number from the record into W.

Code:
    NVSetPtr       Table
    NVSetRecNum    0
    NVSetRecLen    5
Loop
    NVPushByte     0       ;Get the byte
    CompareR               ;Compare with X (now in Y)
    Pop                    ;clear off the byte
    BranchR
    Target         Equal   ;Byte = original X
    Target         Bigger  ;Byte > original X
    Target         Smaller ;Byte < original X
Smaller
    NVIncRecNum            ;to the next record
    GoTo           Loop

;Found
Equal
Bigger
    NVfReadW       1       ;Offset by 1 to skip the byte value
;..... Done .....

    NVEM0  ;=========  End of code, start of NVEM0 data

Table:
    NV0Byte         5       ;1st record
    NV0fNum         12.0

    NV0Byte         89      ;2nd record
    NV0fNum         78.9

    NV0Byte         127     ;n'th record
    NV0fNum         -98

    NV0Byte         255     ;Sentinel record
    NV0fNum         0.0