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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
MOV CH,05H
MOV SI,2000H
MOV DI,2005H

LOOP: MOV AL , [SI]
MOV BL , [DI]
ADD AL,BL
MOV [SI] , AL
INC SI
INC DI
DEC CH
JNZ LOOP
HLT

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

  1. MOV AL, [SI]: Load element from Array 1
  2. MOV BL, [DI]: Load element from Array 2
  3. ADD AL, BL: Add the two elements
  4. MOV [SI], AL: Store result back to Array 1
  5. INC SI: Move to next element in Array 1
  6. INC DI: Move to next element in Array 2
  7. DEC CH: Decrement counter
  8. 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 = 75H

Important Notes

  1. In-place Operation: Array 1 is overwritten with results
  2. No Overflow Handling: If sum exceeds 255, only lower byte is stored
  3. Array 2 Unchanged: Original values remain in Array 2

Handling Overflow

For 8-bit addition that may overflow:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
LOOP: 
    MOV AL, [SI]
    MOV BL, [DI]
    ADD AL, BL      ; May set carry flag
    MOV [SI], AL    ; Store result
    JNC NO_OVERFLOW ; Jump if no carry
    ; Handle overflow here
NO_OVERFLOW:
    INC SI
    INC DI
    DEC CH
    JNZ LOOP
HLT

16-bit Version

For adding 16-bit arrays:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
MOV CH, 05H
MOV SI, 2000H
MOV DI, 2010H

LOOP:
    MOV AX, [SI]    ; Load 16-bit value
    ADD AX, [DI]    ; Add 16-bit value
    MOV [SI], AX    ; Store 16-bit result
    ADD SI, 2       ; Move to next word
    ADD DI, 2
    DEC CH
    JNZ LOOP
HLT