Write a MARIE program to sum the numbers 1 + 2 + 3 + 4 + … + N, where the user enters N.
Answers
Answered by
0
Answer:
The formula for calculating sum of consecutive N numbers = (N(N+1))/2
Explanation:
Algorithm:
- Taking N as the input, increment it to obtain N+1.
- Multiply N with N+1.
- Divide the obtained product obtained by 2.
Steps:
- Load the data from the memory location (301BH, random choice) into the accumulator
- Move the data into B
- Increment the value in the accumulator by one and move it to the register C
- Initialize the accumulator with 0
- Multiplication step: Keep adding B to accumulator. The B has to be added equal number of times as the value of C
- Initialize B with 00H. B will store the quotient of the division
- Initialize C with 02H. This is the divisor for the division
- Division step: Keep subtracting C from A till A becomes 0. For each subtraction, increment B by one
- The final answer is in B. Move it to A. Then store the value of A in 301CH (again random choice)
Program:
LDA 301BH
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 301CH
HLT
The output will be stored in 302CH
Similar questions
Social Sciences,
6 months ago
English,
6 months ago
Math,
1 year ago
Physics,
1 year ago
English,
1 year ago