13 Replies - 849 Views - Last Post: 05 November 2017 - 10:53 PM Rate Topic: -----

#1 pani14625  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-April 17

covert c++ Fibonacci numbers to pep/8 assembly

Posted 16 April 2017 - 09:26 AM

Hi Guys, I'm trying to translate the below code from c++ to (PEP/8 virtual machine assembly code),but it seems like I might not be using the branching instructions correctly. I also have no clue on how to translate if (f3 >= low && f3 <=high )to assembly language. below is the c++ code follow by the assembly code.

#include <iostream>
using namespace std;
int main()
{
int high;
int low;
int odd=0;
// Initialize first three Fibonacci Numbers
int f1 = 0, f2 = 1, f3 = 1;

// Count fibonacci numbers in given range
int result = 0;
cout << "Enter low and high limits:" << endl;
cin >> low;
cin >> high;

while (f1 <= high)
{
if (f1 >= low)
result++;
f1 = f2;
f2 = f3;
f3 = f1 + f2;

// take the numbers that are between low and high only and check to see if they are odd
if (f3 >= low && f3 <=high )
   {
     if( f3 % 2 == 1 )
     odd++;
     cout << f3 << " "; //this one could go out of the if stement 

   }
}

cout << endl;
cout << odd << " of the " << result << " numbers printed were odd" << endl;
//cout << result << " ";
return 0;
} 








;declaring all my local variables 
         BR      main        
high: .EQUATE 12 ;#2d
low:  .EQUATE 10 ;#2d
odd:  .EQUATE 8  ;#2d
f1:   .EQUATE 6  ;#2d
f2:   .EQUATE 4  ;#2d
f3:   .EQUATE 2  ;#2d
result: .EQUATE 0 ;#2d


main:    SUBSP 14,i ;Allocate #high,#low,#odd,#f1,#f2,#f3,#result 
         lda   0,i ; initilize counters odd=0, result=0,f1=0
         sta   odd,s
         sta   result,s
         sta   f1,s
         lda   1,i ; initialize f2, and f3 to=1
         sta   f2,s
         sta   f3,s
         STRO msg1,d ; message to the screen to enter low and high numbers 
         deci low,s
         deci high,s    ; program is working good from here and up so far
while: lda f1,s
       cpa high,s
       BRGT endWh
if:    lda f1,s
       cpa low,s
       BRLT while
       LDA result,s
       adda 1,i
       sta result,s
       lda f1,s
       adda f2,s
       sta f1,s
       lda f2,s
       adda f3,s
       sta f2,s
       lda f3,s
       adda f1,s
       adda f2,s
       sta f3,s
       br while
 
        
endWh: deco result,s   
STOP  
msg1:    .ASCII  "Enter low and high limits:\x00"               
         .END                  




Is This A Good Question/Topic? 0
  • +

Replies To: covert c++ Fibonacci numbers to pep/8 assembly

#2 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2130
  • View blog
  • Posts: 4,196
  • Joined: 30-May 10

Re: covert c++ Fibonacci numbers to pep/8 assembly

Posted 16 April 2017 - 09:45 AM

I think the first step is to copy each line of the C++ code to the assembly code as a comment, then write the 2 or 3 assembler instructions which correspond to that C++ line.

It'll help you figure out what you should be doing next, and help us figure out if you seem to be doing the right thing.
Was This Post Helpful? 1
  • +
  • -

#3 pani14625  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-April 17

Re: covert c++ Fibonacci numbers to pep/8 assembly

Posted 16 April 2017 - 10:23 AM

Salem_c, thank you, that's a great idea. I did like you suggested. I uploaded the code reflecting the comments. I'm a bit confused with the if statement. The book mentions we need to do the opposite of what the c++ code is asking for, and after the if statement I'm guessing I return back to the while loop correct?
;Computer Systems, Fourth edition
;Figure 6.10
;
;declaring all my local variables 
         BR      main        
high: .EQUATE 12 ;#2d
low:  .EQUATE 10 ;#2d
odd:  .EQUATE 8  ;#2d
f1:   .EQUATE 6  ;#2d
f2:   .EQUATE 4  ;#2d
f3:   .EQUATE 2  ;#2d
result: .EQUATE 0 ;#2d


main:    SUBSP 14,i ;Allocate #high,#low,#odd,#f1,#f2,#f3,#result 
         lda   0,i ;
         sta   odd,s    ;odd=0
         sta   result,s ;result=0
         sta   f1,s     ;f1=0
         lda   1,i 
         sta   f2,s      ;f2=1
         sta   f3,s      ;f3=1
         STRO msg1,d     ; msg1: .ASCII  "Enter low and high limits:\x00"  
         deci low,s
         deci high,s  
while: lda f1,s     ;while (f1 <= high)     
       cpa high,s
       BRGT endWh
if:    lda f1,s   ;if (f1 >= low)
       cpa low,s
       BRLT  
       LDA result,s ;result++;
       adda 1,i
       sta result,s
       lda f1,s   ;f1 = f2;
       adda f2,s
       sta f1,s
       lda f2,s  ;f2 = f3;
       adda f3,s 
       sta f2,s
       lda f3,s  ;f3 = f1 + f2;
       adda f1,s
       adda f2,s
       sta f3,s
       br while
 
        
endWh: deco result,s

STOP  
msg1:    .ASCII  "Enter low and high limits:\x00"               
         .END                  






Was This Post Helpful? 0
  • +
  • -

#4 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2130
  • View blog
  • Posts: 4,196
  • Joined: 30-May 10

Re: covert c++ Fibonacci numbers to pep/8 assembly

Posted 17 April 2017 - 01:47 AM

Well it helps if you work from correctly indented and formatted code to begin with.
I also added all the implied { } to show what's really going on.
#include <iostream>
using namespace std;
int main()
{
  int high;
  int low;
  int odd = 0;

  // Initialize first three Fibonacci Numbers
  int f1 = 0, f2 = 1, f3 = 1;

  // Count fibonacci numbers in given range
  int result = 0;
  cout << "Enter low and high limits:" << endl;
  cin >> low;
  cin >> high;

  while (f1 <= high) {
    if (f1 >= low) {
      result++;
    }
    f1 = f2;
    f2 = f3;
    f3 = f1 + f2;

    // take the numbers that are between low and high only and check to see if they are odd
    if (f3 >= low && f3 <= high) {
      if (f3 % 2 == 1) {
        odd++;
      }
      cout << f3 << " ";        //this one could go out of the if stement 
    }
  }

  cout << endl;
  cout << odd << " of the " << result << " numbers printed were odd" << endl;
  //cout << result << " ";
  return 0;
}



> I'm a bit confused with the if statement. The book mentions we need to do the
> opposite of what the c++ code is asking for
I've no idea, I haven't read the book you're reading to know what it's trying to say in context.
Nor do I know what the full repertoire of instructions on the Pep/8 assembler.

But the first if statement ONLY guards result++, you still have to do the rest of the loop body regardless.
If you just jump back to the start of the loop, it will lock up in an infinite loop of doing nothing.

But it might help to put the following labels in your asm, then fill in the code.
if1:
; evaluate truth statement, branch to else1 if false
then1:
; stuff to do if true
  jmp fi1:
else1:
; stuff to do if false
fi1:


Obviously simpler if statements can be reduced after your first pass by removing redundant empty branches of code, and trivial jump to next statement.
Was This Post Helpful? 0
  • +
  • -

#5 rob477  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 7
  • Joined: 04-November 17

Re: covert c++ Fibonacci numbers to pep/8 assembly

Posted 05 November 2017 - 03:42 PM

Hey pani14625,

I'm doing a similar project and wanted to see if you made any progress with this program.

Thanks!!
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13483
  • View blog
  • Posts: 53,820
  • Joined: 12-June 08

Re: covert c++ Fibonacci numbers to pep/8 assembly

Posted 05 November 2017 - 03:45 PM

The user hasn't been on since April perhaps not try dredging up work to see if you can borrow it. ;)
Was This Post Helpful? 1
  • +
  • -

#7 rob477  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 7
  • Joined: 04-November 17

Re: covert c++ Fibonacci numbers to pep/8 assembly

Posted 05 November 2017 - 03:49 PM

Is asking for help not a good thing to do on here?

You must be super busy if you're replying to a post that hasn't been active since april. But thanks for the tip lol.
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13483
  • View blog
  • Posts: 53,820
  • Joined: 12-June 08

Re: covert c++ Fibonacci numbers to pep/8 assembly

Posted 05 November 2017 - 03:51 PM

Perhaps start a new post with your questions and showing what you have tried.
Was This Post Helpful? 1
  • +
  • -

#9 rob477  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 7
  • Joined: 04-November 17

Re: covert c++ Fibonacci numbers to pep/8 assembly

Posted 05 November 2017 - 09:36 PM

Thanks for the special attention and help modi123_1... LOL

Next time I'll check the date posted and last time user was online before I join your wonderful forum looking for some help.




;Write a program in Pep/8 that reads two integers from the user and then prints all the 
;Fibonacci numbers that lie between the two integers entered. Also, print how many numbers 
;were printed and how many of them were odd.
;Use only local variables.
;
;Hint: Fibonacci numbers are as follows:
;0 1 1 2 3 5 8 13 21 34 55 89 ...
;
;Example:
;Enter low and high limits:  50 500
;55 89 144 233 377
;4 of the 5 numbers printed were odd.

         BR main
counter:         .equate     12   ;#2d
low:             .equate     10   ;#2d
high:            .equate     8    ;#2d
num_odd:         .equate     6    ;#2d 
num1:            .equate     4    ;#2d
num2:            .equate     2    ;#2d
num3:            .equate     0    ;#2d

main:            subsp       14,i     ;allocate #counter #low #high #num_odd #num1 #num2 #num3 ;WARNING: num not specified in .EQUATE.
                 lda         0,i
                 sta         num1,s
                 sta         num3,s
                 lda         1,i
                 sta         num2,s
                 stro        msg1,d
                 deci        low,s
                 deci        high,s
                 
while:           lda         num3,s
                 cpa         high,s
                 brgt        EndWhile
                 lda         num1,s
                 adda        num2,s
                 sta         num3,s
if:              lda         num3,s
                 cpa         low,s
                 brlt        Endif 
                 lda         num3,s
                 cpa         high,s
                 brgt        Endif
                 deco        num3,s
                 charo       32,i
                 lda         counter,s
                 adda        1, i
                 sta         counter,s
if2:             lda         num3,s
                 anda        1, i                 
                 cpa         0,i 
                 breq        Endif  
                 lda         num_odd,s
                 adda        1,i
                 sta         num_odd,s      
                 
Endif:           lda         num2,s
                 sta         num1,s
                 lda         num3,s
                 sta         num2,s
                 br          while
EndWhile:        charo       '\n',i
                 deco        num_odd,s
                 stro        output1,d
                 deco        counter,s
                 stro        output2,d
                 addsp       14,i        ;deallocate #counter #low #high #num_odd #num1 #num2 #num3 ;WARNING: num not specified in .EQUATE.
                 stop
msg1:            .ascii      "Enter low and high limits: \x00"
output1:         .ascii     " out of \x00"
output2:         .ascii      " were odd.\x00"
.END
:code:

This post has been edited by modi123_1: 05 November 2017 - 10:06 PM
Reason for edit:: In the future, please use the [code] tag button in the editor..

Was This Post Helpful? -1
  • +
  • -

#10 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13483
  • View blog
  • Posts: 53,820
  • Joined: 12-June 08

Re: covert c++ Fibonacci numbers to pep/8 assembly

Posted 05 November 2017 - 10:07 PM

No problem. Glad to lend a hand to help you get the best visibility for your questions. :^:

Speaking of - was there a question to go with that code?
Was This Post Helpful? 1
  • +
  • -

#11 rob477  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 7
  • Joined: 04-November 17

Re: covert c++ Fibonacci numbers to pep/8 assembly

Posted 05 November 2017 - 10:22 PM

Lol you started a new post for me.

Thats the solution so no need to dredge any longer. Just wanted to share.

Seems I offended a certain Macintard as well.
Was This Post Helpful? -2
  • +
  • -

#12 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13483
  • View blog
  • Posts: 53,820
  • Joined: 12-June 08

Re: covert c++ Fibonacci numbers to pep/8 assembly

Posted 05 November 2017 - 10:27 PM

Ah.. well if you use your speaking words I might know what's up. I'll merge it back.

Also dial back the sass, sport.
Was This Post Helpful? 1
  • +
  • -

#13 rob477  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 7
  • Joined: 04-November 17

Re: covert c++ Fibonacci numbers to pep/8 assembly

Posted 05 November 2017 - 10:48 PM

Lol you've just spent so much time and energy on a single post out of thousands. Dunno what I did to draw so much attention in the first place. Hope you get paid for your moderation.

Your first post: "The user hasn't been on since April perhaps not try dredging up work to see if you can borrow it."

And this last one: "Ah.. well if you use your speaking words I might know what's up."

...are a bit confusing in the sense that I'm not entirely sure what you were trying to say, maybe they need some punctuation, but I feel like the sass is strong in each so just playing along.

But sure thing, boss.
Was This Post Helpful? 0
  • +
  • -

#14 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13483
  • View blog
  • Posts: 53,820
  • Joined: 12-June 08

Re: covert c++ Fibonacci numbers to pep/8 assembly

Posted 05 November 2017 - 10:53 PM

Indeed, time well spent on the new guy.

As it is, dial back the attitude and make name calling less of a thing.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1