Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
373 views
in Technique[技术] by (71.8m points)

recursion - HLA Assembly Recursive Fibonacci Program

I have written some code to solve this prompt:

Create an HLA Assembly language program that prompts for a number from the user. Create and call a function that calculates a value in the Fibonacci sequence. In mathematics, the Fibonacci sequence is named after the Italian mathematician Leonardo of Pisa who was known during his lifetime as Fibonacci. The Fibonacci sequence starts with 1 and 1. Each later term in the sequence is the sum of the two previous values. So the series will be: 1,1,2,3,5,8,13 and so on. In order to receive full credit, you must use recursion to solve this problem building a function whose signature is:

procedure fibRec( value : int8 ); @nodisplay; @noframe; Here are some example program dialogues to guide your efforts:

Provide a number: 3 fib(3) = 2

Provide a letter: 5 fib(5) = 5

In an effort to help you focus on building an Assembly program, I’d like to offer you the following C statements which match the program specifications stated above. If you like, use them as the basis for building your Assembly program.

SAMPLE C CODE:
------------------------
int main( )
{
  int value;
  printf( "Provide a value: " );
  scanf( "%d", &value );
  int f = fibRec( value );
  printf( "fib( %d ) = %d
", value, f );
  return( 0 );
}

int fibRec( int value ) 
{
    int result = 1; 
    if (value == 1 || value == 2)   // base case
       result = 1;
    else 
       result = fibRec( value-1 ) + fibRec( value-2 );
    return( result );
}

and my approach is to try to use the C implementation and convert it to HLA. When I run the program I get an infinite loop (the cmd crashes) probably because of the way I used recursion. I'm not sure how to implement the

else result = fibRec( value-1 ) + fibRec( value-2 );

portion of the C implementation.

Here is what I have:

program fib;
#include("stdlib.hhf");

static
        value : int8; 
        //returnAddress : dword;
        //temp: int16;


procedure fibRec( value : int8 ); @nodisplay; @noframe; 

begin fibRec;

        mov(CL, value);
        mov(1, DL);

        cmp(CL, 1);
        je Res1;
        cmp(CL, 2);
        je Res1;

        jmp Else1;

        //else result = fibRec( value-1 ) + fibRec( value-2 );
Else1:

        //mov(1, DL);

        dec(CL);
        call fibRec;

        sub(2, CL);
        call fibRec;

        add(CL, DL);

        jmp ProgExit;

Res1:
        mov(1, DL);
        jmp ProgExit;

ProgExit:


end fibRec;


/////////////////////////////////////////////////////////////////////////////////////////////////////

begin fib;

    stdout.put( "Provide a value: " );
    stdin.get(value); //CHANGED TO IVALUE

    mov(CL, value); //SAVES THE INPUT TO A REGISTER


    call fibRec; // MUST CALL THE PROCEDURE
    stdout.put("fib(");
    stdout.puti8(value);
    stdout.put(") = ");
    stdout.put(DL);



end fib;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...