Add Two 8-bit Numbers
Write an 8085 assembly program to add two 8-bit numbers stored in memory. The result should be stored in a specified memory location.
-
Input:
2500H
= first number2501H
= second number
-
Output:
3000H
= result of the addition
⚡ TL;DR — Final Working Code
Section titled “⚡ TL;DR — Final Working Code” ; Define interface addresses FIRST_INPUT EQU 2500H SECOND_INPUT EQU 2501H RESULT_LOW EQU 2502H RESULT_HIGH EQU 2503H
; Step 1: Load inputs LXI H, FIRST_INPUT MOV A, M ; A ← first number INX H MOV B, M ; B ← second number
; Step 2: Perform addition ADD B ; A ← A + B MOV C, 00H ; Assume no carry JNC SKIP_CARRY INR C ; If carry, C ← 01HSKIP_CARRY:
; Step 3: Store result LXI H, RESULT_LOW MOV M, A ; Store sum INX H MOV M, C ; Store carry HLT
🧱 Step 1: Define the Interface
Section titled “🧱 Step 1: Define the Interface”Before writing the logic, we need to define how this program will communicate with the external world — this is called the interface.
📦 Interface Definition
Section titled “📦 Interface Definition”We’ll keep all inputs and outputs in the same memory page for clarity and ease of testing.
Address | Role |
---|---|
2500H | First 8-bit input |
2501H | Second 8-bit input |
2502H | Output: sum (lower byte) |
2503H | Output: carry (00H or 01H) |
This interface supports both:
- ✅ Simple use case (just 8-bit sum)
- ✅ Advanced case (with carry handling, if added later)
🧠 Why Interface Design Matters
Section titled “🧠 Why Interface Design Matters”An interface defines the contract between your program and everything outside it. Good interface design means:
- Inputs and outputs are clearly separated
- The module can be extended without breaking
- The user knows exactly where to place test values
❌ Bad Interface Examples
Section titled “❌ Bad Interface Examples”- Leaving the result in a register only
- Overwriting one of the input addresses
- Storing the result across non-contiguous memory
🧾 Code (Input Preparation Only)
Section titled “🧾 Code (Input Preparation Only)”Let’s just load the two input numbers from memory to understand the data flow.
LXI H, 2500H ; HL → first inputMOV A, M ; A ← first numberINX H ; HL → second inputMOV B, M ; B ← second numberHLT
🧪 Manual Test
Section titled “🧪 Manual Test”Set memory:
2500H = 0AH2501H = 14H
Expected after halt:
- Register A = 0AH
- Register B = 14H
You’ve now read both inputs into registers. Next, we’ll perform the actual addition.
🧱 Step 2: Perform the Addition
Section titled “🧱 Step 2: Perform the Addition”Now that we’ve loaded the two input numbers into registers, the next step is to perform the addition and preserve the result, including the carry if any.
🧠 What Happens in This Step
Section titled “🧠 What Happens in This Step”We’ll:
- Add the value in register
B
to the value inA
usingADD B
- Check the carry flag (
CY
) to see if the result overflowed 8 bits - Store the lower byte of the result in
A
- Prepare a value (
00H
or01H
) based on whether carry occurred
🧾 Code (Addition Logic Only)
Section titled “🧾 Code (Addition Logic Only)”ADD B ; A ← A + BMOV C, 00H ; Assume no carryJNC SKIP_CARRY ; If no carry, skipINR C ; If carry occurred, C ← 01H
SKIP_CARRY:HLT
- After
ADD B
, the sum is inA
. - If the addition caused a carry, we increment
C
. - Now
C
holds the carry byte (00H
or01H
) to store alongside the result.
🧪 Manual Test
Section titled “🧪 Manual Test”Set:
2500H = 0AH ; A = 102501H = 14H ; B = 20
Expected:
- A =
1EH
(30 in decimal) - C =
00H
(no carry)
Try with:
2500H = FFH2501H = 01H
Expected:
- A =
00H
(overflow) - C =
01H
(carry)
This step sets up both parts of the result: the 8-bit sum and the carry. Next, we’ll store them into memory.
🧱 Step 3: Store the Result in Memory
Section titled “🧱 Step 3: Store the Result in Memory”We now have the result of the addition:
- The sum (lower 8 bits) is in register
A
- The carry (if any) is in register
C
Our task is to store these two values at the predefined output addresses so the result is preserved even after the program halts.
📦 Output Mapping (Reminder)
Section titled “📦 Output Mapping (Reminder)”Address | Purpose |
---|---|
2502H | Sum (lower byte) |
2503H | Carry (00H or 01H) |
We’ll use the HL pair to point to 2502H
and write the results in order.
🧾 Code to Store Result
Section titled “🧾 Code to Store Result”LXI H, 2502H ; HL → first output addressMOV M, A ; Store sum at 2502HINX H ; HL → next addressMOV M, C ; Store carry at 2503HHLT
This makes your output traceable and testable even after execution ends.
🧪 Manual Test
Section titled “🧪 Manual Test”Try:
2500H = FFH2501H = 01H
Expected memory after execution:
2502H = 00H ; Sum = 00H (overflow)2503H = 01H ; Carry = 01H
Try another:
2500H = 0AH2501H = 05H
Expected:
2502H = 0FH ; Sum = 152503H = 00H ; No carry
You’ve now completed the full data flow: load → compute → store.
📚 Summary
Section titled “📚 Summary”This program demonstrates how to add two 8-bit numbers in 8085 assembly while handling potential overflow:
- Inputs are placed in contiguous memory (
2500H
,2501H
) - The sum is calculated and stored in
A
- A carry check is performed using
JNC
and stored in a separate byte - Final results are written to memory for inspection
🔍 Key Learnings
Section titled “🔍 Key Learnings”- Use
ADD
to combine register values and update flags - Use
JNC
to detect overflow via the carry flag - Always design your memory interface clearly when sharing results with the outside world