# 8051 Program to Add an Array of Numbers

## Program 1: Array from Internal Memory

### Problem

Suppose that array of 10 data bytes is stored in internal memory location 40h. Write a program in assembly language for 8051 microcontroller to add these data bytes and store the result’s lower byte in 4Ah and MSB in 4Bh.

### Algorithm

Step 1: Select Register Bank 0.

Step 2: Initialize Carry Counter.

Step 3: Initialize byte counter.

Step 4: Initialize memory pointer for array.

Step 5: Clear Accumulator which is uses as sum register.

Step 6: Add number from array.

Step 7: If Result < 8 bit, then go to Step 9.

Step 8: Increment Carry counter by 1.

Step 9: Increment memory pointer by 1.

Step 10: Decrement byte counter by 1.

Step 11: If byte counter 1 is not 0, then go to Step 6.

Step 12: Store LSB and MSB (Value of Carry Counter) of result in memory·

Step 13: Stop.

### Program

CLR PSW.3 ; Clear 3rd bit of PSW.

CLR PSW.4 ; Clear 4th bit of PSW .

; Above steps are used to select register bank 0.

CLR PSW.7 ; Clear carry flag.

MOV R3, #00H ; Initialize R3=0 as carry counter.

MOV R0, #0AH ; Initialize R0=10(decimal or A hexadecimal) as byte counter.

MOV R1, #40H ; Initialize R1= 40 as memory pointer.

MOV A, #00H ; Clear accumulator.

UP: ADDC A, @R1 ; Add accumulator with data in memory location indicated by R1 indirectly with carry.

INC R3 ; If carry=1 increment carry counter.

NEXT: INC R1 ; Increment memory pointer.

DJNZ R0, UP ; Decrement and jump to UP if byte counter is not zero

MOV @R1, A ; Store LSB of result to indirect address indicated by R1 indirectly

INC R1 ; Increment memory pointer

MOV A, R3 ; Store MSB of result to accumulator

MOV @R1, A ; Store MSB of result to indirect address indicated by R1 indirectly

LOOP: AJMP LOOP ; Stop.

## Program 2: Array from External Memory

### Problem

Assume the array of ten data bytes is stored in external RAM from memory location 3000H. Assume sum is 16 bit. Write a program in assembly language for 8051 microcontroller to store LSB of result in memory location 300AH and MSB of result in memory location 300BH.

### Algorithm

Step 1: Select Register Bank 0.

Step 2: Initialize Carry Counter.

Step 3: Initialize byte counter.

Step 4: Initialize memory pointer for array.

Step 5: Clear Accumulator which is uses as sum register.

Step 6: Add number from array.

Step7: If Result < 8 bit, then go to Step 9

Step 8: Increment Carry counter by 1

Step 9: Increment memory pointer by 1

Step 10: Decrement byte counter by 1.

Step 11: If byte counter is not 0, then go to Step 6.

Step 12: Store LSB and MSB (Value of Carry Counter) of result in memory.

Step 13: Stop.

### Program

CLR PSW.3 ; Clear 3rd bit of PSW.

CLR PSW.4 ; Clear 4th bit of PSW .

; Above steps are used to select register bank 0.

CLR PSW.7 ; Clear carry flag.

MOV R3, #00H ; Initialize R3=0 as carry counter.

MOV R0, #0AH ; Initialize R0=10(decimal or 0A hexadecimal) as byte counter.

MOV DPTR, 3000h ; Initialize memory pointer.

MOV R2, #00H ; Clear R2.

UP: MOVX A, @DPTR ; Move the content of external memory to accumulator.

MOV R2, A ; Store the result to R2

INC R3 ; If carry=1 increment carry counter.

NEXT: INC DPTR ; Increment memory pointer.

DJNZ R0, UP ; Decrement and jump to UP if byte counter is not zero

MOV @DPTR, A ; Store LSB of result to indirect address indicated by DPTR indirectly

INC DPTR ; Increment memory pointer

MOV A, R1 ; Store MSB of result to accumulator

MOV @DPTR, A ; Store MSB of result to indirect address indicated by DPTR indirectly

LOOP: AJMP LOOP ; Stop.