1 |
2 |
sinclairrf |
<!-- Copyright 2012, Sinclair R.F., Inc. -->
|
2 |
|
|
<html>
|
3 |
|
|
<title>
|
4 |
|
|
9x8 Tips and Tricks
|
5 |
|
|
</title>
|
6 |
|
|
<body>
|
7 |
|
|
Copyright 2012, Sinclair R.F., Inc.<br/><br/>
|
8 |
|
|
This file lists some tips and tricks for efficiently using the 9x8 instructions
|
9 |
|
|
set.<br/><br/>
|
10 |
|
|
<h1>Multi-Byte Numeric Operations</h1>
|
11 |
|
|
The 9x8 core does not have built-in multi-byte instructions, however, common
|
12 |
|
|
operations such as incrementing and decrementing multi-byte counts easily fit
|
13 |
|
|
in the provided instruction set.<br/><br/>
|
14 |
|
|
<h2>Incrementing and Decrementing multi-byte values</h2>
|
15 |
|
|
If the top two bytes of the data stack are
|
16 |
|
|
<tt>( u_16_LSB u_16_MSB )</tt>, then this value can be
|
17 |
|
|
incremented by the following 6 instructions: This increments the LSB
|
18 |
|
|
of the two-byte value and, if the resulting LSB is zero, subtracts
|
19 |
|
|
<tt>-1</tt> from the MSB.<br/><br/>
|
20 |
|
|
<tt> swap 1+ swap over 0= -</tt><br/><br/>
|
21 |
|
|
Incrementing a 3 byte or larger value is similar, except that the MSBs
|
22 |
|
|
must be pushed onto the return stack instead of using the <tt>swap</tt>
|
23 |
|
|
instruction. For example, to increment a 24-bit count, use the following
|
24 |
|
|
11 instructions:<br/><br/>
|
25 |
|
|
<tt> >r swap 1+ swap over 0= - r> over 0= -</tt><br/><br/>
|
26 |
|
|
Adding larger increments can be done similarly. For example to add four to
|
27 |
|
|
a two-byte multiple of four, change the "<tt>1+</tt>" increment operations
|
28 |
|
|
to the two instruction "<tt>4 +</tt> sequence:<br/><br/>
|
29 |
|
|
<tt> swap 4 + swap over 0= -</tt><br/><br/>
|
30 |
|
|
If the count is not guaranteed to be a multiple of four, use the
|
31 |
|
|
following:<br/><br/>
|
32 |
|
|
<tt> swap 4 + swap over 0xFC & 0= -</tt><br/><br/>
|
33 |
|
|
Decrementing multi-byte values is similar. For example, to decrement a
|
34 |
|
|
two-byte count, use the following: This decrements the LSB of the two-byte
|
35 |
|
|
value and, if the resulting LSB is <tt>0xFF</tt>, adds <tt>-1</tt> to the
|
36 |
|
|
MSB.<br/><br/>
|
37 |
|
|
<tt> swap 1- swap over -1= +</tt><br/><br/>
|
38 |
|
|
A function to add a power of two to a two-byte quantity could be
|
39 |
|
|
implemented as follows. This works by adding the increment to the LSB and
|
40 |
|
|
then masking the new LSB with <tt>256-u_increment</tt>. As an
|
41 |
|
|
example, the value <tt>4</tt> can be added to the two-byte value on the
|
42 |
|
|
top of the data stack through
|
43 |
|
|
"<tt>.call(u16_add_power_of_2,4)</tt>".<br/><br/>
|
44 |
|
|
<tt> ; Add a power of 2 to a two-byte value: u_16_new = u_16 + u_increment<br/>
|
45 |
|
|
; ( u_16_LSB u_16_MSB u_increment - u_16_new_LSB u_16_new_MSB )<br/>
|
46 |
|
|
.function u16_add_power_of_2<br/>
|
47 |
|
|
>r swap r@ + swap over 0 r> - & 0=<br/>
|
48 |
|
|
.return(-)</tt><br/><br/>
|
49 |
|
|
</body>
|
50 |
|
|
</html>
|