A complete expression calculator built in pure 8051 Assembly Language for the AT89C51 microcontroller. Supports operator precedence, brackets, signed 24-bit arithmetic, memory register, and special math functions.
This project implements a full expression calculator from scratch in
8051 Assembly (MIDE-51). Unlike simple left-to-right calculators, this one
correctly evaluates expressions like 3 + 4 * 2 = 11 respecting operator
precedence — using Dijkstra's Shunting-Yard algorithm implemented entirely
in assembly.
- ✅ Operator precedence (
*/before+-) - ✅ Bracket support with implicit multiplication (
3(4+5)→ 27) - ✅ 24-bit signed arithmetic (range: ±8,388,607)
- ✅ Decimal division (7÷2 = 3.5)
- ✅ Memory register — M+, M−, MR, MC
- ✅ Special functions — x², x³, √ (via MODE layer)
- ✅ Percentage operator
% - ✅ Unary minus (e.g.
-5 * 3 = -15) - ✅ 4 error states — DIV/0, Overflow, 3-digit limit, Bracket mismatch
| Component | Part | Connection |
|---|---|---|
| Microcontroller | AT89C51 (8051) | U1 — 12MHz crystal |
| Display | LM044L 20×4 LCD | Data: P1, RS: P2.1, EN: P2.2 |
| Keypad | 4×4 Matrix | P3.0–P3.7 |
| MODE Button | Tactile Switch | P2.3 (active LOW) |
| M−, MR, MC | Tactile Switches | P2.4–P2.6 |
| Crystal | 12 MHz + 33pF ×2 | XTAL1 / XTAL2 |
| Reset | 10kΩ + 10µF | RST (pin 9) |
[ 7 SQU ][ 8 CUB ][ 9 ( ][ ÷ ]
[ 4 M- ][ 5 % ][ 6 √ ][ × ]
[ 1 MR ][ 2 MC ][ 3 M+ ][ - ]
[ ON/C ][ 0 ) ][ = ][ + ]
Small labels = function in MODE layer. Press MODE first, then the key.
8051-calculator/
├── src/
│ ├── calculator.asm # Full source (1485 lines)
├── simulation/
│ └── calculator8051.pdsprj # Proteus 8 project file
├── hex/
│ └── calculator.hex # Compiled Intel HEX output
└── docs/
├── circuit.png # Schematic screenshot
├── ram-map.md # RAM layout documentation
└── how-it-works.md # Algorithm explanation
- Open
simulation/calculator.pdsprjin Proteus 8 - Load
hex/calculator.hexinto the AT89C51 component - Click Play ▶ and use the virtual keypad
- Open
src/calculator.asmin MIDE-51 - Press F9 (Build) to assemble
- The
.hexfile is generated in the same folder
The calculator uses the Shunting-Yard algorithm to evaluate expressions with correct precedence. Two stacks are maintained in internal RAM:
- Value Stack (40H–5DH) — stores 24-bit operands (10 deep)
- Operator Stack (5EH–67H) — stores operator characters (10 deep)
When an operator is pressed, its precedence is compared to the top of the operator stack. Higher-precedence operators are applied first.
See docs/how-it-works.md for full details.
| Error Message | Trigger |
|---|---|
ERROR: DIV BY 0 |
Divisor is zero |
OVERFLOW! |
Result exceeds ±8,388,607 or stack full |
ERR:3DIG |
Typing a 4th digit (max input = 999) |
BRACKET ERR |
Mismatched ( ) in expression |
- MIDE-51 — 8051 assembler and IDE
- Proteus 8 — Circuit simulation
MIT License — see LICENSE for details.
