X86斐波那契程序 [英] X86 Fibonacci program

查看:51
本文介绍了X86斐波那契程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是写一个程序,计算斐波那契数列的前七个值.给出的公式是:

My assignment is to write a program that calculates first seven values of fibonacci number sequence. the formula given is:

Fib(1) = 1, Fib(2) = 1, Fib(n) = Fib(n-1) + Fib(n-2)

我相信这是一个函数,但是我不明白如何将其合并到代码中.我需要将值放在EAX寄存器中.我正在使用MASM,但这没有任何区别.有提示吗?

I believe that is a function but I do not understand how to incorporate it into code. I need to place the values in EAX register. I am using MASM not that makes any difference. Any hints?

推荐答案

我怀疑这是一项学术任务,所以我只打算部分回答这个问题.

I suspect that this is an academic assignment so I'm only going to partially answer the question.

斐波那契数列对非负整数的正式定义如下:

The fibonacci sequence is formally defined for non-negative integers as follows:

F(n) = n                   | n < 2
     = F(n - 1) + F(n - 2) | n >= 2

这给出了:

  n | F(n)
  0 |   0
  1 |   1
  2 |   1
  3 |   2
  4 |   3
  5 |   5
  6 |   8
  7 |  13
etc etc...

您仅需几个寄存器即可完成操作,让我们对其进行识别:

You can do it with just a few registers, let's identify them:

  • R n (请求的斐波那契数的编号)
  • R f1 (用于计算斐波那契数)
  • R f2 (也用于计算斐波那契数)
  • R x (用于保存返回值的寄存器.可以与任何其他寄存器重叠)
  • Rn (the number of the requested fibonacci number)
  • Rf1 (used to calculate fibonacci numbers)
  • Rf2 (also used to calculate fibonacci numbers)
  • Rx (the register to hold the return value. can overlap with any other register)

R n 作为参数传递给函数.R f1 应该从0开始,R f2 应该从1开始.

Rn is passed as the argument to the function. Rf1 shall start at 0, and Rf2 shall start at 1.

这是我们得到答案的方法,按例程划分:

Here's what we do to get the answer, split up by routines:

开始

  1. 将R f1 初始化为0.
  2. 将R f2 初始化为1.
  3. 继续循环.
  1. Initialize Rf1 to 0.
  2. Initialize Rf2 to 1.
  3. Continue to Loop.

循环

  1. 从R n 中减去2.
  2. 如果R n 小于0,请跳至完成".
  3. 将R f2 添加到R f1 ,并将结果存储在R f1 中.
  4. 将R f1 添加到R f2 ,并将结果存储在R f2 中.
  5. 跳到循环.
  1. Subtract 2 from Rn.
  2. If Rn is less than 0, jump to Finish.
  3. Add Rf2 to Rf1, storing the result in Rf1.
  4. Add Rf1 to Rf2, storing the result in Rf2.
  5. Jump to Loop.

完成

  1. 如果 Rn AND 1 为假(暗示 Rn 是偶数)跳转到 FinishEven.
  2. 将R f1 存储为返回值.
  3. 返回.
  1. If Rn AND 1 is false (implying that Rn is even) jump to FinishEven.
  2. Store Rf1 as the return value.
  3. Return.

FinishEven

  1. 将R f2 存储为返回值.
  2. 返回.

通过R n = 5进行跟踪:

Tracing through for Rn = 5:

  1. R f1 = 0
  2. R f2 = 1
  3. R n = R n -2//R n = 3
  4. 测试R n <0//错误
  5. R f1 = R f1 + R f2 //R f1 = 0 +1 = 1
  6. R f2 = R f1 + R f2 //R f2 = 1 + 1 = 2
  7. 无条件跳转到循环
  8. R n = R n -2//R n = 1
  9. 测试R n <0//错误
  10. R f1 = R f1 + R f2 //R f1 = 1 + 2 = 3
  11. R f2 = R f1 + R f2 //R f2 = 3 + 2 = 5
  12. 无条件跳转到循环
  13. R n = R n -2//R n = -1
  14. 测试R n <0//是
  15. 跳到结束
  16. 测试R n &1//是
  17. R x = R f2 //5
  1. Rf1 = 0
  2. Rf2 = 1
  3. Rn = Rn - 2 // Rn = 3
  4. Test Rn < 0 // false
  5. Rf1 = Rf1 + Rf2 // Rf1 = 0 + 1 = 1
  6. Rf2 = Rf1 + Rf2 // Rf2 = 1 + 1 = 2
  7. Unconditional Jump to Loop
  8. Rn = Rn - 2 // Rn = 1
  9. Test Rn < 0 // false
  10. Rf1 = Rf1 + Rf2 // Rf1 = 1 + 2 = 3
  11. Rf2 = Rf1 + Rf2 // Rf2 = 3 + 2 = 5
  12. Unconditional Jump to Loop
  13. Rn = Rn - 2 // Rn = -1
  14. Test Rn < 0 // true
  15. Jump to Finish
  16. Test Rn & 1 // true
  17. Rx = Rf2 // 5

我们的表显示F(5)= 5,所以这是正确的.

Our table shows that F(5) = 5, so this is correct.

这篇关于X86斐波那契程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆