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 TableNVSetRecNum 0NVSetRecLen 5LoopNVPushByte 0 ;Get the byteCompareR ;Compare with X (now in Y)Pop ;clear off the byteBranchRTarget Equal ;Byte = original XTarget Bigger ;Byte > original XTarget Smaller ;Byte < original XSmallerNVIncRecNum ;to the next recordGoTo Loop;FoundEqualBiggerNVfReadW 1 ;Offset by 1 to skip the byte value;..... Done .....NVEM0 ;========= End of code, start of NVEM0 dataTable:NV0Byte 5 ;1st recordNV0fNum 12.0NV0Byte 89 ;2nd recordNV0fNum 78.9NV0Byte 127 ;n'th recordNV0fNum -98NV0Byte 255 ;Sentinel recordNV0fNum 0.0