Email: Password: Remember Me | Create Account (Free)
Introduction to 16-bit Math


The 8051 is an 8-bit microcontroller. This basically means that each machine language opcode in its instruction set consists of a single 8-bit value. This permits a maximum of 256 instruction codes (of which 255 are actually used in the 8051 instruction set).

The 8051 also works almost exclusively with 8-bit values. The Accumulator is an 8-bit value, as is each register in the Register Banks, the Stack Pointer (SP), and each of the many Special Function Registers (SFRs) that exist in the architecture. In reality, the only values that the 8051 handles that are truly 16-bit values are the Program Counter (PC) that internally indicates the next instruction to be executed, and the Data Pointer (DPTR) which the user program may utilize to access external RAM as well as directly access code memory. Other than these two registers, the 8051 works exclusively with 8-bit values.

For example, the ADD instruction will add two 8-bit values to produce a third 8-bit value. The SUBB instruction subtracts an 8-bit value from another 8-bit value and produces a third 8-bit value. The MUL instruction will multiply two 8-bit values and produce a 16-bit value.

Programming Tip: It could be said that the MUL instruction is a 16-bit math instruction since it produces a 16-bit answer. However, its inputs are only 8-bit. The result is 16-bits out of necessity since any multiplication with two operands greater than the number 16 will produce a 16-bit result. Thus, for the MUL operation to have any value at all it was absolutely necessary to produce a 16-bit result.

As we can see, the 8051 provides us with a number of instructions aimed at performing mathematical calculations. Unfortunately, they are all work with 8-bit input values--and we often find ourselves working with values that simply cannot be expressed in 8-bits.

This tutorial will discuss techniques that allow the 8051 developer to work with 16-bit values in the 8051's 8-bit architecture. While we will only discuss 16-bit mathematics, the techniques can be extended to any number of bits (24-bit, 32-bit, 64-bit, etc.). It's just a matter of expanding the code to support the additional bytes. The algorithms remain the same.

These tutorials will explain how to perform 16-bit addition, subtraction, and multiplication with the 8051. For the time being, 16-bit division is outside the scope of this tutorial.

Programming Tip: Compared to addition, subtraction, and multiplication, division is a relatively complicated process. For the time being 16-bit division will not be discussed because the author has not had a need to develop such routines, nor an opportunity to analyze the process in performing the calculation. If you have developed a routine that allows a 16-bit value to be divided by another 16-bit value and would like to contribute the code to, along with a tutorial similar to those found in these sections, please contact us..

How did we learn math in primary school?

Before jumping into multibyte mathematics in machine language, let's quickly review the mathematics we learned as children. For example, we learned to add two numbers, say 156 + 248, as follows:

How do we calculate the above? We start in the 1's column, adding 6 + 8 = 14. Since 14 can't fit in a single column, we leave 4 in the 1's column and carry the 1 to the 10's column. We then add 5 + 4 = 9, add the 1 we carried, to get 10. Again, 10 doesn't fit in a single column. So we leave the 0 in the 10's column and carry the 1 to the 100's column. Finally, we add 1 + 2 = 3, add the 1 we carried to get 4, which is our final answer in the 100's column. The final answer, thus, is 404.

It is important to remember this when working with multibyte math, because the process is going to be the same. Let's start by doing 16-bit addition.

Previous: Tutorial Contents Tutorial Contents Next: 16-Bit Addition