X86 Fibonacci program

情到浓时终转凉″ 提交于 2021-02-08 12:04:29

问题


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)

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?


回答1:


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

This gives:

  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:

  • 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)

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:

Begin

  1. Initialize Rf1 to 0.
  2. Initialize Rf2 to 1.
  3. Continue to Loop.

Loop

  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.

Finish

  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. Store Rf2 as the return value.
  2. Return.

Tracing through for Rn = 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

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




回答2:


TITLE  Chapter 4 Exercise 6                (ch04_06.asm)

Comment !
Description: Write a program that uses a loop to calculate the first
seven values in the Fibonacci number sequence { 1,1,2,3,5,8,13 }.
Place each value in the EAX register and display it with a
call DumpRegs statement inside the loop.

Last update: 05/02/2002
!
INCLUDE Irvine32.inc

.code
main PROC
    mov   eax,1
    call  DumpRegs
    mov   ebx,0 ; initial setup
    mov   edx,1
    mov   ecx,6 ; count
L1:
    mov  eax,ebx    ; eax = ebx + edx
    add  eax,edx
    call DumpRegs   ; display eax
    mov  ebx,edx
    mov  edx,eax
    Loop L1

    exit
main ENDP
END main


来源:https://stackoverflow.com/questions/12865984/x86-fibonacci-program

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!