3 Replies - 2301 Views - Last Post: 28 November 2012 - 08:13 AM

#1 StNtMaN  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 09-December 11

C++ to Pep/8 Conversion

Posted 21 November 2012 - 05:08 PM

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!

Is This A Good Question/Topic? 0
  • +

Replies To: C++ to Pep/8 Conversion

#2 turboscrew  Icon User is offline

  • D.I.C Addict

Reputation: 100
  • View blog
  • Posts: 609
  • Joined: 03-April 12

Re: C++ to Pep/8 Conversion

Posted 27 November 2012 - 06:04 AM

Guesswork again, but...
You don't seem to call your subroutine from the "main".
Maybe, instead of "LDA sum" you ment to "CALL sum"?

sum:     SUBSP   4,i  ; // Make room in stack
if:      LDA     n,s  ; // Load n to A
         BRLE    else  ; // if A is now zero (or negative?) goto "else"
         BR      endIf  ; // Goto Endif without doing anything
else:    LDA     n,s  ; // Why this? Nothing is changed from "if"-line
         ADDA    sum,s  ; // Add sum to A
         STA     RetVal,s  ; // Store  result
endif:   ADDSP   2,i  ; // Remove space from the stack
  ; // Then run the main again (no RET, no recursive call)


This post has been edited by turboscrew: 27 November 2012 - 06:05 AM

Was This Post Helpful? 0
  • +
  • -

#3 StNtMaN  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 09-December 11

Re: C++ to Pep/8 Conversion

Posted 27 November 2012 - 05:54 PM

I see what you mean with the CALL instruction. I'll add it to the program asap. I'll post back with results. Thanks!

View Postturboscrew, on 27 November 2012 - 06:04 AM, said:

Guesswork again, but...
You don't seem to call your subroutine from the "main".
Maybe, instead of "LDA sum" you ment to "CALL sum"?

sum:     SUBSP   4,i  ; // Make room in stack
if:      LDA     n,s  ; // Load n to A
         BRLE    else  ; // if A is now zero (or negative?) goto "else"
         BR      endIf  ; // Goto Endif without doing anything
else:    LDA     n,s  ; // Why this? Nothing is changed from "if"-line
         ADDA    sum,s  ; // Add sum to A
         STA     RetVal,s  ; // Store  result
endif:   ADDSP   2,i  ; // Remove space from the stack
  ; // Then run the main again (no RET, no recursive call)


Was This Post Helpful? 0
  • +
  • -

#4 turboscrew  Icon User is offline

  • D.I.C Addict

Reputation: 100
  • View blog
  • Posts: 609
  • Joined: 03-April 12

Re: C++ to Pep/8 Conversion

Posted 28 November 2012 - 08:13 AM

The first missing CALL is the subroutine CALL from "main".
The second missing CALL is the recursive CALL.
Also, the subroutine doesn't return (RET), so it runs through the main too after the subroutine.

BTW, do you know any web page with better description about Pep/8 assembly? I found this but it's not really very detailed... :D

This post has been edited by turboscrew: 28 November 2012 - 08:18 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1