簡體   English   中英

Mobonacci-匯編代碼中的斐波那契操縱

[英]Mobonacci - Fibonacci manipulation in Assembly Code

大家好,我一直在努力完成這項任務。 它是fib序列的重制,其變化在於它變為f(n-2)/ 2 + f(n-1)* 2,起始兩個數字分別為1和2,因此某些序列如下。 1,2,4,9,20,44,98。 我們要使用左移和右移(shl和shr)來完成乘法和除法。我感覺我非常接近,但是由於某種原因,我的代碼將無法讀取大於2的數字,或者至少這樣我看到了 如果您可以查看一下,並給我指明可能導致此問題的原因,無論是我的實際公式部分,還是之前和之后的謝謝。 第一個主要方面是我簡單地得出結果的簡單方法。

 include Irvine32.inc

.code
main PROC
    push 1
    call fib
    call WriteDec
    call Crlf
    push 2
    call fib
    call WriteDec
    call Crlf
    push 3
    call fib
    call WriteDec
    call Crlf
    push 4
    call fib
    call WriteDec
    call Crlf
    push 5
    call fib
    call WriteDec
    call Crlf
    push 7
    call fib
    call WriteDec
    call Crlf
  exit
main ENDP

fib PROC 
   push ebp
    mov ebp, esp
    mov ebx, [ebp+8] ;get number to calc that was pushed before 

    ; if ((n == 1) || (n == 0)) return 1;
   mov eax,1
   cmp ebx,1
   jle Quit


    ;else return fib(n-2)/2 + fib(n-1)*2;


    valid:

    ;(n-2)/2
   dec ebx ;n-1
   dec ebx ; n-1 again so n-2
   push ebx  ; store the number
   call fib  ; calculate n-2
   shr ebx,1 ;(n-2)/2
   push eax  ;store it away
   mov ebx, [ebp+8] ;get number to calc

   ;(n-1)*2
   dec ebx ;(n-1)
   push ebx ;store the numebr
   call fib ;calculate n-1
    shl ebx,1 ;(n-1)*2
    pop edx ;bring back the (n-2)*2 saved value
    add eax,edx ;adds the n-2 and n-1 together
   jmp Quit 





Quit:
   pop ebp  ;return base pointer
        ret 4  ;clean stack
fib ENDP

END main

要計算f(n-2)/ 2,您需要使用SHR代替SHL。

;(n-2)/2
sub ebx,2
shr ebx,1

要計算f(n-1)* 2,您需要使用SHL代替SHR。

;(n-1)*2
dec ebx
shl ebx,1

您為什么不簡化代碼? (您不需要ReturnFib上的代碼。)

; if ((n == 1) || (n == 2)) return 1;
 mov eax, 1   ;less than 3 so result is 1, no need to calculate Fibonacci
 cmp ebx, 2
 jle Quit
valid:

編輯

這是第一學期的樣子。

;f(n-2)/2
sub ebx,2
push ebx  ; store the number
call fib  ; calculate n-2
shr eax,1
push eax  ;store it away

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM