-1

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.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
imasy36
  • 41
  • 4
  • 1
    `N*(N-1) / 2` isn't the right formula, so you can avoid using that formula and instead use the correct one: `N*(N+1) / 2`. http://mathcentral.uregina.ca/qq/database/qq.02.06/jo1.html. Or use a variation on it that avoids possible overflow by right-shifting whichever of N or N+1 is even *before* multiplying, as in [Writing an Assembly Program to add from 1 to 100](https://stackoverflow.com/a/61200641) for x86 – Peter Cordes Apr 14 '20 at 19:25
  • 4
    I don't think anyone wants to do your homework for you. What have you tried, where are you stuck? – Nate Eldredge Apr 14 '20 at 19:25
  • @PeterCordes Sorry for formula, I just copied the question so I didn't notice the mistake. 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 or the question is wrong. – imasy36 Apr 15 '20 at 04:40
  • The question doesn't claim that it's possible to meet all 3 constraints at once so the question can't be "wrong" per se. The constraints might be unsatisfiable, though. I fixed the formatting and title in your question, and included your comment. Now it's a reasonable question. To answer it: 8085 has a `sub` instruction; you could use it instead of inc/dec for looping. Inconvenient but possible. – Peter Cordes Apr 15 '20 at 04:50
  • The question title says sum upto 100, which would result into 5050 that cannot be stored in 8-bit accumulator. – codeR Jun 24 '21 at 11:33

2 Answers2

2

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.

imasy36
  • 41
  • 4
  • To satisfy the third constraint, the `LDA` instruction should be removed from your solution – codeR Jun 24 '21 at 11:32
2

The question title says sum upto 100 or 0x64, which would result into 100*(100+1)/2 = 5050 or 0x13BA that cannot be stored in 8-bit accumulator. In that case we can keep the result in a register pair say HL.

      MVI A,64H    ; A <- n
      MVI C,01H
      MVI D,00H
      MVI E,00H
      MVI H,00H
      MVI L,00H

LOOP: MOV E,A      ; E <- n
      DAD D        ; HL <- HL + n
      SUB C        ; n <- n-1
      JNZ LOOP

      HLT

The initialization instructions at the begining are written using MVI for better clarity and can easily be converted to efficient ones.

codeR
  • 165
  • 1
  • 1
  • 13