Enhanced math for the Basic Stamp 2

An introduction to the Stamp2 BCD & 32-bit routines


The Basic Stamp 2 (tm), although being a very nice little computer to play with, has limited math capabilities. The main limitations are: (To a certain extent, these two limitations are two faces of the same medal. If, for example, I can work with integer numbers the range 0 to 65535, and I am in need to have decimal places, I could decide to store all numbers in memory multiplied by, let's say, 100. So, 1 would be stored as 100 and 2 as 200. If I need to store a fractionary number, say 2.34, I would simply store 234 in my variable. This of course would reduce my working range, as 65535 in this representation means just 655.35. As you see, you earn some decimal places at the expense of some loss in the range of acceptable numbers.

From time to time, one would like to do some "stronger" math. There are indeed ways to try and overcome these limitations (although sometimes at a very high cost in terms of memory!).

The best way, when possible, is to use properly the Stamp's math operations. This is particularly true when, for example, you have to multiply or divide a variable by a constant number, i.e. a number that never changes. This can usually be done with small "tricks". To do a silly exampe, let's say that you where to multiply a variable by a constant fractionary number, like 2.5. Multiplying by 2.5 is equivalent to multplying by 5 and then dividing by 2. See the page about the fractionary approach for more details.

My math routines

When this cannot be done, there are other ways. In the past years, I have developped two set of routines to do higher precision math on the BS2. The first one is a set of BCD math routines (no division yet!) that are able to work on VERY large integer numbers (up to 15 digits in length). The second one is a set of binary math routines (MAT32) that can work on numbers with 24, 32 or 40 bits, and even more.

So, what is the difference between the two? The names say it all. The BCD routines store the numbers in a BCD (binary coded decimal) format, the MAT32 store the numbers in a binary format.

BCD format is a quite funny way to keep numbers, and stores one decimal digit in 4 bits, equivalent to half a byte, and to a nibble. As the BS2 is able to work with nibble variables, this is quite handy. You can declare an array of nibbles and store in each nibble a decimal digit:

     number var nibble (10)    ' This variable can store 10 decimal digits!
Binary format is the more usual way of representing numbers. In the Stamp, you have nibble variables (4 bits, can store numbers 0 to 15), byte variables (8 bits, 0 to 255) and word variables (16 bits, 0 to 65535). On other machines, you also have long variables (32 bits, 0 to 4294967295).

The binary math routines somehow implement 32-bit calculations, at the expense of about one third of the program space and of most of the available RAM (you can anyway re-use a lot of the RAM used by MAT32). This include sum, subtract, multiply and divide.

When should you use BCD and when MAT32?

First of all, the BCD routines don't handle division, and I don't think it is feasable to add it leaving some memory for other uses.

But displaying (through DEBUG, on an LCD etc.) a BCD number is straightforward, while it is quite complex to display a 32-bit binary number. So complex that my routines dont't even attempt to do it, and just display numbers in hexadecimal.

So, if you just need to do calculations, the MAT32 routines are probably the best: more complete, faster, lighter on system resorces etc.

If you also need to display you results in decimal, you should try and play with the BCD routines.

This is only partially, true; for more detail, see the pages about both routines: the BCD routines and the binary routines.