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.
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.
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.
文章评论(0条评论)
登录后参与讨论