



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Arithmetic calculations like addition and subtraction are fundamental operations in many computer programs. Most programming languages support several different data types that can be used to perform arithmetic calculations.
The ATmega128 uses 8-bit registers and has several different instructions to perform basic arithmetic operations on 8-bit operands. Examples of instructions that add or subtract 8-bit registers are:
ADD R0, R1 ; R0 Å R0 + R ADC R0, R1 ; R0 Å R0 + R1 + Carry Bit SUB R0, R1 ; R0 Å R0 – R
If we are required to manipulate data with 16 or more bits with AVR instructions then we need to perform intermediate steps to manipulate 8-bit registers to produce the correct result. The following figure demonstrates the addition of two 16-bit numbers. One of the 16-bit numbers is located in registers R0 and R1 and the other number is in R and R3. The operation result is placed in the registers R4, R5, and R6.
1 Å Possible Carry-In Bit R0 R Possible Carry-Out Bit Æ 1 R2 R
R4 R5 R
As this calculation demonstrates, we need to add R1 to R3 and accommodate for any carry-in bit. The result of this operation is stored in R6. Next, we add R0 to R2 and accommodate for any carry-in bit from the previous operation. If there is a final carry- bit, then it is stored in R4. Note that since we want to reserve the carry out bit from the operation, we actually get a 17-bit result, 16-bit plus the carry. But since the AVR architecture inherently supports 8-bit numbers and we use 3 registers, we treat the result as a 24-bit number, where the most significant Byte has the value of either 1 or 0 depending on the carry out bit.
The AVR instruction set contains a special instruction to perform multiplication, MUL. This instruction multiplies two registers and stores the result in registers R0 and R1. Therefore a multiplication of 2 bytes generates a 2-byte result. This instruction can be used as a fast and efficient way to multiply two 8-bit numbers. Unfortunately, it can be very complicated to use when multiplying 16 or more bit numbers.
The easiest way to understand how to multiply large numbers is to visualize it like using the paper method to multiply, this method is also known as the sum of products technique. The following figure illustrates the typical paper method.
24
24 12- 28-
1824
As you can see, the paper method multiplies small numbers, numbers less than 10, and then adds up all the products to get the final result. We can use this same technique for large binary numbers. Since there is an instruction that supports multiply, MUL, we can multiply two 8-bit numbers at a time and then add up all the products. Below is an example of how to multiple two 16-bit numbers.
A1 A
H22 L H21 L H12 L
P1 P2 P3 P
As you can see, the result of multiplying two 16-bit (or 2 Byte) numbers gives a 32- bit (or 4 Byte) result. In general, when multiplying to binary numbers, the result will be
the inherent properties of a base-2 number system. This technique is known as the shift- and-add multiplier.
Referring back to the basic concepts of mathematics, multiplication has two operands, the multiplicand (or the operand to be multiplied) and the multiplier. When a decimal number is multiplied, it essentially adds the multiplicand to itself for the amount specified by the multiplier. This seems pretty obvious, until you use binary. In binary, the multiplier is used to determine whether the multiplicand is added or not. The figure below will demonstrate both decimal and binary multiplications.
Decimal: 23 Å Multiplicand
138 Å 6 * 32 = 138
368 Å Final Multiplication Product
Now, we apply this same knowledge to a binary system that uses 4-bit words. Note that multiplying two 4-bit words results in a product that consists of two 4-bit words. The basic principle is that we shift through the multiply. When a 1 is received, we add the multiplicand to the low bit of the result, if a zero is received we do nothing. We then shift the entire result one bit to the left, thus essentially multiplying the result by 2 and repeat the process. Below is an example.
Binary(4-bit): 1011 Å Multiplicand
0000 1011 Å Add results together 1 0010 1100 Å 1 * 1011 = 1011, 1011 << 2 = 0010 1100
0011 0111 Å Add results together 1 0101 1000 Å 1 * 1011 = 1011, 1011 << 3 = 0101 1000
1000 1111 Å Add results together to get final product
Proof: Multiplicand Multiplier Product Bin 1011 * 1101 = 1000 1111 Dec 11 * 13 = 143 Å Correct
Although the method above is sound and easily understandable, there is a more efficient method. For the purpose of this example, I will assume that I am running on a 4- bit system where the registers are 4-bits. It would take 4 registers to do the method
above. A better way would be to share the low result registers with the multiplier registers so that you can shift all the registers at once and only use 3 registers. In order to do this, you would shift everything to the right instead of the left and also rotate through the carry. So if the carry bit is set after the shift, then add the multiplicand to the high register of the result and shift again, otherwise, just shift. The following figure demonstrates this:
Multiply 1011 Å Multiplicand
0 1011 0110 Å Result of addition 0 0101 1011 Å Shift right through carry ----- Å Don’t add since carry is 0
0 0101 1011 Å Result thus far 1 0010 1101 Å Shift right through carry 1011 Å Add multiplicand since carry is set
0 1101 1101 Å Result of carry 1 0110 1110 Å Rotate right through carry 1011 Å Add multiplicand since carry is set
1 0001 1110 Å Result of addition 0 1000 1111 Å Result after final shift, note that a ‘1’ was shifted in, because it was the carry that was set from the last addition.
Note that when shifting to the right, you need to rotate through the carry, this means that what ever is in the carry bit will be shifted in the most significant bit and the least significant bit will be shifted out into the carry bit. For example:
Rotate Right Through Carry Bit
As you can see, the shift and add technique can be easily created with a simple for loop that loops for a specific amount depending on the size of the data, for the case above we used 4-bit numbers so we looped 4 times. With the loop there is a simple rotate left and an add depending on the carry bit. This technique can be easily used for any sized number with minimal effort and is used internally for multiplications with most micro architectures.