On this page
Sum of Two Arrays
This program adds corresponding elements from two arrays and stores results in the first array.
Description
The program performs element-wise addition of two arrays: Result[i] = Array1[i] + Array2[i]
Memory Layout
- Array 1: Starts at address 2000H (5 elements)
- Array 2: Starts at address 2005H (5 elements)
- Result: Stored back in Array 1 (2000H)
Code
| |
Explanation
Initialization
- CH = 5: Number of elements to add
- SI = 2000H: Pointer to Array 1 (source and destination)
- DI = 2005H: Pointer to Array 2 (source)
Addition Loop
- MOV AL, [SI]: Load element from Array 1
- MOV BL, [DI]: Load element from Array 2
- ADD AL, BL: Add the two elements
- MOV [SI], AL: Store result back to Array 1
- INC SI: Move to next element in Array 1
- INC DI: Move to next element in Array 2
- DEC CH: Decrement counter
- JNZ LOOP: Continue if more elements remain
Example
Before Execution:
Memory Address: 2000H 2001H 2002H 2003H 2004H 2005H 2006H 2007H 2008H 2009H
Initial Value: [10] [20] [30] [40] [50] [05] [10] [15] [20] [25]
└─────── Array 1 ──────┘ └─────── Array 2 ──────┘After Execution:
Memory Address: 2000H 2001H 2002H 2003H 2004H 2005H 2006H 2007H 2008H 2009H
Final Value: [15] [30] [45] [60] [75] [05] [10] [15] [20] [25]
└────── Result Array ─────┘ └─────── Array 2 ──────┘Calculations
2000H: 10H + 05H = 15H
2001H: 20H + 10H = 30H
2002H: 30H + 15H = 45H
2003H: 40H + 20H = 60H
2004H: 50H + 25H = 75HImportant Notes
- In-place Operation: Array 1 is overwritten with results
- No Overflow Handling: If sum exceeds 255, only lower byte is stored
- Array 2 Unchanged: Original values remain in Array 2
Handling Overflow
For 8-bit addition that may overflow:
| |
16-bit Version
For adding 16-bit arrays:
| |