fibonacci序列使用程序集avr递归

时间:2018-05-03 23:05:06

标签: assembly arduino avr

我试图通过在堆栈上推送和弹出中间值来实现递归函数,也使用递归我需要实现Fibonacci数。

这是装配部件每次我运行程序时它给我10的总和为0,当我试图改变它时我得到10的总和是20或60。

在“more:”函数中我试图做f(n-1)+ f(n-2)。

.text
.global recur

recursion:
 clr r18
 call recur
 mov r24, r18
 ret

 recur:   // simple summation recurrence.  We look at R24 and if it is > we set up things to recur

     CPI R24, 3
     brge more  // do some more

     // we are base case
     LDI R24, 1
     CLR R25
     ret

 more:
     dec R24
     push R24
     dec r24
     call recur
     add r18, r24
     // at this point R24 is the result, we need to add on what we pushed
     pop r24
     call recur
     ADD R18, R24
     clr r24
     CLR R25
     ret

这是用C

写的部分
// Cooper
// 2018
// recursion

// set up connect to AVR code

extern "C" { int recur(int);}

void setup() {


  // put your setup code here, to run once:
  int v;
  v=recur(10);
  Serial.begin(9600);

  Serial.print("The summation is ");
  Serial.println(v);



}

void loop() {
  // put your main code here, to run repeatedly:
   int v;
 v=recur(10);
  delay(1000);
  Serial.println(v);

}

0 个答案:

没有答案