Copyright 2012, Sinclair R.F., Inc.

This file lists some tips and tricks for efficiently using the 9x8 instructions set.

Multi-Byte Numeric Operations

The 9x8 core does not have built-in multi-byte instructions, however, common operations such as incrementing and decrementing multi-byte counts easily fit in the provided instruction set.

Incrementing and Decrementing multi-byte values

If the top two bytes of the data stack are ( u_16_LSB u_16_MSB ), then this value can be incremented by the following 6 instructions: This increments the LSB of the two-byte value and, if the resulting LSB is zero, subtracts -1 from the MSB.

  swap 1+ swap over 0= -

Incrementing a 3 byte or larger value is similar, except that the MSBs must be pushed onto the return stack instead of using the swap instruction. For example, to increment a 24-bit count, use the following 11 instructions:

  >r swap 1+ swap over 0= - r> over 0= -

Adding larger increments can be done similarly. For example to add four to a two-byte multiple of four, change the "1+" increment operations to the two instruction "4 + sequence:

  swap 4 + swap over 0= -

If the count is not guaranteed to be a multiple of four, use the following:

  swap 4 + swap over 0xFC & 0= -

Decrementing multi-byte values is similar. For example, to decrement a two-byte count, use the following: This decrements the LSB of the two-byte value and, if the resulting LSB is 0xFF, adds -1 to the MSB.

  swap 1- swap over -1= +

A function to add a power of two to a two-byte quantity could be implemented as follows. This works by adding the increment to the LSB and then masking the new LSB with 256-u_increment. As an example, the value 4 can be added to the two-byte value on the top of the data stack through ".call(u16_add_power_of_2,4)".

  ; Add a power of 2 to a two-byte value:  u_16_new = u_16 + u_increment
  ; ( u_16_LSB u_16_MSB u_increment - u_16_new_LSB u_16_new_MSB )
  .function u16_add_power_of_2
    >r swap r@ + swap over 0 r> - & 0=
  .return(-)