Saturday, September 12, 2009

Calculate the sum of series of numbers(8085)




Statement: Calculate the sum of series of numbers. The length of the series is in memory location 4200H and the series begins from memory location 4201H.
a. Consider the sum to be 8 bit number. So, ignore carries. Store the sum at memory location 4300H.
b. Consider the sum to be 16 bit number. Store the sum at memory locations 4300H and 4301H.


a. Sample problem
4200H = 04H
4201H = 10H
4202H = 45H
4203H = 33H
4204H = 22H
Result = 10 +41 + 30 + 12 = H
4300H = H
Source program:
LDA 4200H
MOV C, A : Initialize counter
SUB A : sum = 0
LXI H, 420lH : Initialize pointer
BACK: ADD M : SUM = SUM + data
INX H : increment pointer
DCR C : Decrement counter
JNZ BACK : if counter 0 repeat
STA 4300H : Store sum
HLT : Terminate program execution


b. Sample problem
4200H = 04H
420lH = 9AH
4202H = 52H
4203H = 89H
4204H = 3EH
Result = 9AH + 52H + 89H + 3EH = H
4300H = B3H Lower byte
4301H = 0lH Higher byte
Source program:
LDA 4200H
MOV C, A : Initialize counter
LXI H, 4201H : Initialize pointer
SUB A :Sum low = 0
MOV B, A : Sum high = 0
BACK: ADD M : Sum = sum + data
JNC SKIP
INR B : Add carry to MSB of SUM
SKIP: INX H : Increment pointer
DCR C : Decrement counter
JNZ BACK : Check if counter 0 repeat
STA 4300H : Store lower byte
MOV A, B
STA 4301H : Store higher byte
HLT :Terminate program execution

4 comments:

  1. It will give only sum of 8bit no's....
    But will it work for more than two 16bit no???????????

    ReplyDelete
  2. with carry :p
    carry ----> D

    MVI D 00h
    LDA 8500h
    MOV C A
    SUB A
    LXI H 8501h
    L: ADD M
    JNC L1
    INR D
    L1: INX H
    DCR C
    JNZ L
    MOV M A
    INX H
    MOV M D
    HLT


    ReplyDelete
  3. 4200H = 04H
    WHY WE ARE STORING 04H AT THIS ADRESS?

    ReplyDelete