Close
0%
0%

Hardware Calculator From Scratch

DIY floating-point calculator based on AVR ATmega328P MCU and written in pure assembly

Public Chat
Similar projects worth following
It is based on a homebrew IEEE 754 binary floating point emulation library which has been reinvented from the ground up.

There is also a tutorial that explains all the theory behind the calculator. Look at Files section on this site or go to the GitHub repo.

Overview

The calculator is built upon two custom libraries:

  • float32avr.asm - Software emulation of floating-point arithmetic.
  • lcd1602.asm - Library for interfacing with the LCD1602 based on the HD44780 controller.

The float32avr.asm repository with documentation can be found here.

The lcd1602.asm repository is located here.

Architecture and Operation

A bird's-eye block diagram is provided below:

Input of Numbers

An encoder scans key presses and outputs a 4-bit code ranging from 0x0 to 0xF. An ASCII code table corresponding to the keyboard keys is stored in RAM. The table is arranged so that the lower nibble of the address equals zero. When any key is pressed, its 4-bit code is used to access the table and retrieve its ASCII code.

To prevent the input of invalid numeric strings, a simple finite state machine is implemented to parse key presses. The state diagram is presented below:

The current state is stored in the stack as the address of the label to which the program should jump during the next keyboard interrupt.

Operand Conversion

After the first operand is entered and the operator key is pressed, the decimal numeric string is parsed and converted into a single-precision floating-point format. The operator is stored as the ASCII code of the operator symbol.

Once the second operand is entered, it is also converted to float, the first operand is retrieved, the operator is analyzed, and one of the four subroutines from the floating-point emulation library is called.

The resulting value is pre-normalized to one non-zero decimal digit before the decimal point and converted into a decimal string in exponential format.

Display Output

The lcd1602.asm library implements the simplest mode of interaction with the display controller—an 8-bit bus with synchronous waiting for the busy flag. Additionally, auxiliary subroutines for cursor control and screen clearing are implemented.

Calculation Errors

Unlike commercial calculators, numbers in this calculator are represented in binary floating-point format, not in binary-coded decimal (BCD) format, so the accuracy of the result depends not only on the available precision.

Sources of errors include:

  • Characteristics of the binary floating-point format:
    • Not all decimal numbers can be precisely represented in binary form, even with an infinite precision grid. A classic example is the number 0.1. Simply put, if the original decimal number can be expressed as a sum of powers of two, and the binary precision grid is large enough, then such a decimal number will be represented exactly (assuming the conversion algorithm provides the best approximation). In other cases, the conversion will yield only an approximation of the original decimal number.
    • During calculations, due to the limited precision grid, the result is inevitably rounded (by default to the nearest, with ties going to the even number).
  • Conversion of a decimal string to binary floating-point format: Since the conversion is based on a naive scheme that does not use arbitrary-precision arithmetic but relies on the same single-precision floating-point emulation used for calculations, rounding of intermediate values can occur during conversion, leading to distortions in the original decimal value entered by the user (even if the original decimal number can be exactly represented in binary).
  • Conversion of a floating-point number to a decimal string: Although any binary number in float format can have its exact decimal representation derived, in this calculator, as with the string-to-float conversion, a naive scheme based on single-precision float is used, so rounding of intermediate values can occur during conversion, ultimately distorting the entire value and specific decimal digits.

Schematic

Float32AVR Flowcharts

FADD32 (floating-point addition)

FMUL32 (floating-point multiplication)

FDIV32 (floating-point division)

FTOAN (converts decimal normalized float to ASCII)

... Read more »

Division algorithm with immovable divisor. Proof.pdf

Shows how we can build division with immovable divisor from the basic and naive scheme and why it works.

Adobe Portable Document Format - 430.44 kB - 04/25/2025 at 13:09

Preview

Hardware Calculator From Scratch.pdf

A tutorial that covers all the theory behind the calculator.

Adobe Portable Document Format - 11.80 MB - 04/04/2025 at 15:13

Preview

Float32AVRTest-en.zip

Auxiliary repo that contains desktop floating-point reference tests for test dataset in Float32AVR repo.

x-zip-compressed - 16.98 kB - 04/02/2025 at 20:48

Download

Float32AVR-en.zip

Software floating-point emulation library

x-zip-compressed - 1.09 MB - 04/02/2025 at 20:47

Download

HardwareCalculatorFromScratch-en.zip

The whole firmware source code

x-zip-compressed - 12.96 MB - 04/02/2025 at 14:51

Download

View all 6 files

  • Computer arithmetic

    igor.24034008/02/2025 at 13:04 0 comments

    The Hardware Calculator From Scratch is a minimalistic project that utilizes only binary floating-point arithmetic for doing math. It does not support function evaluation.

    However, for the sake of completeness, I've implemented a few side projects, that cover these additional topics separately. These are written in C++ instead of assembly, as we already know how to implement software floating-point emulation in assembly — so using assembly here would bring no additional educational value.

    So, if you understand binary floating point and the above topics, you’ll be able to implement an industrial-grade hardware calculator that uses BCD floating-point and can evaluate elementary functions — all on your own, without looking anything up.

    You'll be able to implement even a graphing calculator! By the way, I've also written a simple software rasterizer which you can find and study here: https://github.com/igor-240340/SoftwareRenderer.

    If you know how to juggle numbers and draw graphics on the low level, you can do some fun stuff.

View project log

Enjoy this project?

Share

Discussions

Similar Projects

Does this project spark your interest?

Become a member to follow this project and never miss any updates