Faster 16bit multiplication algorithm for 8-bit MCU
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Multiplication in microcontroller units (MCUs) is a fundamental operation that underpins many computational tasks, but it can also be a performance bottleneck, especially in lower-end 8-bit MCUs lacking hardware multipliers. A typical challenge is multiplying 16-bit integers efficiently given the limited resources of these devices. In this article, we explore methods and an algorithm to speed up 16-bit multiplication on 8-bit MCUs using software-based techniques.
Basics of 16-bit Multiplication
On an 8-bit microcontroller, such as those from the AVR or PIC families, 16-bit multiplication must often be conducted in software. This typically involves combining four 8-bit multiplications to achieve a single 16-bit multiplication, due to the limited register width. A basic multiplication of two 16-bit numbers, A
and B
, could be represented as follows:
• •
Where , , , and are the high and low bytes of and , respectively. The resulting multiplication operation can be broken down into smaller segments:
Expanding this expression gives:
Each of the terms can be calculated using an 8-bit multiplication.
Optimized Algorithm
To optimize the multiplication, a strategic approach can diminish the total number of operations by reusing partial results and minimizing additions. One such optimized method is the iterative shift-and-add processing which exploits the binary decomposition of the numbers.
Faster 16-bit Multiplication Algorithm
Here is a step-by-step explanation of an efficient method to multiply two 16-bit numbers:
- Initialize Registers: • Load the initial values of A and B into two pairs of 8-bit registers representing their high and low bytes.
- Compute Partial Products: • Compute : This is the simplest multiplication as it directly contributes to the lower byte of the final product. • Compute and : These contribute mainly to the middle segment of the resulting word. Each multiplication here will need to be left-shifted by 8 bits (equivalent to multiplying by 256). • Compute : This contributes to the higher byte and needs to be left-shifted by 16 bits in the final product assembly.
- Aggregate Partial Products with Shifting: • Add the results, as shown in the following combinatorial scheme, ensuring that carries from one byte are considered in the subsequent higher byte:
- Optimize Carries Management: • Use assembly instructions wisely to manage carries that arise when adding 8-bit results. Adding with carry instructions (
ADD/ADCin AVR assembly) efficiently manages the multi-byte aggregation.
Example Code
Below is an illustrative example in pseudocode that translates the above approach into steps suitable for a typical 8-bit AVR type MCU:
• Instruction Cycle Count: Depending on the architecture, these operations (8-bit multiplication, additions, and carries) can be significantly faster when carefully sequenced compared to calling generic multiplication routines. • Memory Use and Efficiency: This implementation is efficient in terms of memory since it mainly uses registers. This is crucial for MCUs with limited RAM. • Scalability: This method scales well for slightly larger bit-widths with the same principle applied to segments of the binary representation.
Related reading
- Faster Algorithm for string comparing in c
- Faster algorithm to find unique element between two arrays?
- faster implementation of sum for Codility test
- Faster kNN Classification Algorithm in Python
- Faster math algorithm sacrificing accuracy
- Faster s3 bucket duplication
- faster string sorting with long common prefix?
- Faster than binary search for ordered list

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.