X86斐波那契程序 [英] X86 Fibonacci program
问题描述
我的任务是写一个程序,计算斐波那契数列的前七个值.给出的公式是:
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:
开始
- 将R f1 初始化为0.
- 将R f2 初始化为1.
- 继续循环.
- Initialize Rf1 to 0.
- Initialize Rf2 to 1.
- Continue to Loop.
循环
- 从R n 中减去2.
- 如果R n 小于0,请跳至完成".
- 将R f2 添加到R f1 ,并将结果存储在R f1 中.
- 将R f1 添加到R f2 ,并将结果存储在R f2 中.
- 跳到循环.
- Subtract 2 from Rn.
- If Rn is less than 0, jump to Finish.
- Add Rf2 to Rf1, storing the result in Rf1.
- Add Rf1 to Rf2, storing the result in Rf2.
- Jump to Loop.
完成
- 如果 Rn AND 1 为假(暗示 Rn 是偶数)跳转到 FinishEven.
- 将R f1 存储为返回值.
- 返回.
- If Rn AND 1 is false (implying that Rn is even) jump to FinishEven.
- Store Rf1 as the return value.
- Return.
FinishEven
- 将R f2 存储为返回值.
- 返回.
通过R n = 5进行跟踪:
Tracing through for Rn = 5:
- R f1 = 0
- R f2 = 1
- R n = R n -2//R n = 3
- 测试R n <0//错误
- R f1 = R f1 + R f2 //R f1 = 0 +1 = 1
- R f2 = R f1 + R f2 //R f2 = 1 + 1 = 2
- 无条件跳转到循环
- R n = R n -2//R n = 1
- 测试R n <0//错误
- R f1 = R f1 + R f2 //R f1 = 1 + 2 = 3
- R f2 = R f1 + R f2 //R f2 = 3 + 2 = 5
- 无条件跳转到循环
- R n = R n -2//R n = -1
- 测试R n <0//是
- 跳到结束
- 测试R n &1//是
- R x = R f2 //5
- Rf1 = 0
- Rf2 = 1
- Rn = Rn - 2 // Rn = 3
- Test Rn < 0 // false
- Rf1 = Rf1 + Rf2 // Rf1 = 0 + 1 = 1
- Rf2 = Rf1 + Rf2 // Rf2 = 1 + 1 = 2
- Unconditional Jump to Loop
- Rn = Rn - 2 // Rn = 1
- Test Rn < 0 // false
- Rf1 = Rf1 + Rf2 // Rf1 = 1 + 2 = 3
- Rf2 = Rf1 + Rf2 // Rf2 = 3 + 2 = 5
- Unconditional Jump to Loop
- Rn = Rn - 2 // Rn = -1
- Test Rn < 0 // true
- Jump to Finish
- Test Rn & 1 // true
- Rx = Rf2 // 5
我们的表显示F(5)= 5,所以这是正确的.
Our table shows that F(5) = 5, so this is correct.
这篇关于X86斐波那契程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!