Question
PEP 8 Assembly Language Programming Write a program to calculate the sum of the first n integers. Main should input an integer and then call
PEP 8 Assembly Language Programming
Write a program to calculate the sum of the first n integers.
Main should input an integer and then call RecSum, which is a recursive function.
Add trace tags.
The code for sum is:
if (n==0) return 0;
else return n+ RecSum(n-1);
You should test it with several different inputs. You can use the formula to check whether you are getting the correct answer.
The formula is Sum(n) = n* (n+1)/2
Note: you are not coding the formula - you need to code the recursive method.
NOTE: In class I led you through two programs to start you off on this assignment. The first merely set up main with trace tags etc to verify you had correctly built the stack. RecSum1.pep
We then added the call to RecSum, but not using recursion, RecSum merely returned 0 if the parameter was 0, and 1 otherwise. RecSum2.pep Once we can do this, we are ready to tackle the recursive step.
recSum1:
BR main num: .equate 0 ;local variable #2d n: .equate 0;parameter #2d retVal: .equate 2 ;#2d main: subsp 2,i ; allocate #num deci num, s lda num,s subsp 4,i ;;allocate #retVal #n sta num,s ;call RecSum addsp 2,i ;deallocate #n deco 0,s ; output answer; addsp 4,i ;deallocate #retVal #num stop .end
Rec Sum 2:
BR main num: .equate 0 ;local variable #2d n: .equate 2;parameter #2d retVal: .equate 4 ;#2d RecSum: lda 2,s cpa 0,i brgt else br done else: lda 1,i done: sta retVal,s ret0 main: subsp 2,i ; allocate #num deci num, s lda num,s subsp 4,i ;;allocate #retVal #n sta num,s call RecSum addsp 2,i ;deallocate #n deco 0,s ; output answer; addsp 4,i ;deallocate #retVal #num stop .end
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started