Search code examples
assembly8085

Sum integers from 1 to 100 without increment or decrement instructions (and without using Gauss's formula)?


Write an Assembly Language Program for Intel 8085 microprocessor to calculate the sum of N natural numbers, that is, ΣN = 1+2+3+ … +(N-1)+N.

Fulfil as many constraints as you can:

  1. Not using the formula of N*(N+1) / 2
  2. Not using any increment and decrement instructions like INR, DCR, INX, and DCX
  3. Not using memory address to read data and write output results (Assume Accumulator register for the same)

My Code is :

START: LDA 3000H
MOV B, A
INR A
MOV C, A
MVI A, 00H

LOOP1: ADD B
DCR C
JNZ LOOP1

MVI C, 02H
MVI B, 00H

LOOP2: INR B
SUB C
JNZ LOOP2

MOV A,B
STA 3001H
HLT

I have tried and made the program but with using all these constraints, while the question asks not to use them. So my doubt is whether it is possible to make a program without using these.


Solution

  • I think the following code satisfies all conditions :

    START:LDA 3000H
    MVI B, 00H
    MVI C, 00H
    MVI L, 01H
    
    LOOP:MOV H, A
    MOV A, C
    ADD L
    MOV C, A
    MOV A, B
    ADD C
    MOV B, A
    MOV A, H
    SUB L
    JNZ LOOP
    
    MOV A, B
    HLT
    

    It doesn't uses guass formula instead add one by one and instead of using increment decrement instructions, it uses add and sub instructions. The result is stored in the accumulator register.