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 NVEM0NVSetPtr Table ;Get to start of tableNVSetRecLen 1 ;Needed so offset is calculated correctlyNVPopRecNum ;Set the record number from XNVPushByte 0 ;Get the result to X.... continue ... NVEM0 ;===== End of program code, start of NVEM0 data tablesTable:NV0Byte 1NV0Byte 3NV0Byte 5NV0Byte 7NV0Byte 11NV0Byte 13NV0Byte 17NV0Byte 19NV0Byte 23NV0Byte 31NV0Byte 37NV0Byte 41NV0Byte 43NV0Byte 47