StNtMaN 0 Newbie Poster

C++ Code:

#include <iostream>

using namespace std;

int sum(int n)

{

  if (n <= 0)

    return 0;

  else

    return n + sum(n-1);

}

int main()

{

   cout << "Range num? ";

   int num;

   cin >> num;

   cout << sum(num) << endl;

   return 0;

}

My Pep/8 try at it:

         br      main
RetVal:  .EQUATE 6
num:     .EQUATE 2
n:       .EQUATE 0
sum:     SUBSP   4,i
if:      LDA     n,s
         BRLE    else
         BR      endIf 
else:    LDA     n,s
         ADDA    sum,s
         STA     RetVal,s
endif:   ADDSP   2,i

main:    STRO    msg,d
         DECI    num,s
         LDA     sum,s    
         DECO    num,d
         CHARO   '\n',i
         STOP
msg:     .ASCII  "Range num?"
.END

You must use equates to access the stack and follow the call to the function as discussed in the book (pass the parameter, return address, return a value and so on). There are NO global variables in the resulting code (except a global message of "Range num? "). It must be able to do sum a range greater than 2.

I can't seem to figure out mainly the endIf and the sum(num). if there is anythign else wrong that I could fix please let me know! Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.