8051 code to find the sum of negative numbers stored in an array.

Program to find the sum of all negative numbers stored in an array.


This program finds the negative numbers in an array and adds them.Basically negative number is one whose carry bit is 1.

Algorithm

1)Initializing register R0 with the array address and register R1 with counter.
2)Storing the first value of array in accumalator.
3)Rotating it left to check whether the carry bit is 1 or 0.
4)If carry bit is 1 adding and if the carry bit 0 then repeating the above steps.
5)Repeating all the above steps till the counter is 0.

Code goes here:


ORG 0000h
LJMP MAIN
ORG 40h
MAIN:  MOV R0,#0XA0   ; the address of the stored array
 MOV R1,#10     ;the counter which counts the numbers stored in array
 MOV R2,#00
LOOP: MOV A,@R0
 RLC A        ; rotating right to check whether the carry bit is 1
 JNC LABEL1     ; if the carry bit is 0 then it is positive so it should not be added and jump to label1
 MOV A,@R0
 ADD A,R2       ;adding if the number is negative
 MOV R2,A
LABEL1: INC R0
 DJNZ R1,LOOP   ;checking the counter
 END

No comments:

Powered by Blogger.