Friday, September 2, 2011

Statement: Write an ALP of 8085 to calculate the sum of series of even numbers. Assume that the length of the series is stored at memory location E000 H and the series itself begins at memory location E001 H. Store the result at memory location F000H.

LDA E000H
MOV C,A
MVI B,00H
LXI H,E001H
L1:
MOV A,M
ANI 01H
JNZ L2
MOV A,B
ADD M
MOV B,A
L2:
INX H
DCR C
JNZ L1
MOV A,B
STA F000H
HLT

Explanation:


         In this program we have to find sum of series of even numbers. The length is stored at some memory location in this program it is E000H but it can be any location. The same way starting location of the series in this program is E001H but it can be any.

        So first of all we have copied the length of the series into the accumulator. Then this data is copied to the register C. After that we have intialized register B with 00H (We are going to use it to store the sum.). HL register pair is initialized with E001 H.

'L1' Loop:

       The data stored in memory location (M) pointed by HL pair is copied to accumulator. The data is 'anded' with 01H . ( If the result of ANDing is zero it indicates that the number is even.) If the result of ANDing is not zero then it will jump to L2.

       If the result is zero then the sum (data of register B) is copied to the accumulator and the data stored in memory location is added to the accumulator. (Remember, in the starting of this loop we had just COPIED the data of M to A so data is still stored at M.) The result is again copied at register B using MOV B,A.

'L2' Loop:


      HL pair is incremented by 1. The data in register C (=counter=length )is decremented by 1. If the data in register C is not equal to 00H then it'll jump to L1 loop.
     Otherwise  resultant sum is copied to accumulator then it is stored at memory location given in the statement.

HLT stands for 'stop'.

No comments:

Post a Comment