Example: Simple byte lookup using NVEM0

This example is for a simple lookup table with 1-byte entries. Given a number in X, we want to read out the X'th byte from the table (which just happens to be a table of prime numbers). Because all addressing starts at 0, X=0 will access the first value in the table, i.e. the number 1, X=1, will access the 2nd entry (3) etc. (which rather neatly side-steps the argument over whether or not 1 is a prime!)

The program fragment is separated into a code part and a table part, with labels Code: and Table:. Notice the NVEM0 directive that marks the end of the code segment and the start of the table (NVEM0) segment.

The table is made up of one line per entry. In this case it's all single byte entries, so we use the NV0Byte directive to define them.

The code is merely a fragment that would exist somewhere in the middle of a larger program, perhaps as a subroutine. The first instruction selects page 0, which is NVEM0. This is needed only if you are switching between different NVEM types, and is included here mainly for show.

The second instruction sets the NVEM pointer to the start of the table. Imagine a program that contains several tables. Setting the pointer ensures that the code is working on the correct table.

The third instruction sets the NVEM record length register. All NVEM read instructions compute the target address from Pointer, Record Length and Record Number, so these must all be correctly set.

The fourth instruction is a little trickier. Remember that the number of the record we want is in X. We need to get that number set in the NVEM Record Number register. The NVPopRecNum instruction achieves just that by copying X to the Record Number register then popping the stack (the register stack is described fully in the SPLat/PC help file).

Finally we use a NVPushByte to read out the data from the NVEM and Push it onto the register stack, i.e. into X. The net effect is that the number in X is replaced by the corresponding prime number. Notice that the NVPushByte has a zero argument. That argument is used to pick out different bytes from multi-byte records.

Code:
    NVSetPage      0       ;Select NVEM0
    NVSetPtr       Table   ;Get to start of table
    NVSetRecLen    1       ;Needed so offset is calculated correctly
    NVPopRecNum            ;Set the record number from X
    NVPushByte     0       ;Get the result to X

     .... continue ...

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

Table:
    NV0Byte         1
    NV0Byte         3
    NV0Byte         5
    NV0Byte         7
    NV0Byte         11
    NV0Byte         13
    NV0Byte         17
    NV0Byte         19
    NV0Byte         23
    NV0Byte         31
    NV0Byte         37
    NV0Byte         41
    NV0Byte         43
    NV0Byte         47