2011-06-02 40 views
1

即時嘗試在x86 NASM組合中實現遞歸阿克曼 - 彼得函數。功能定義如下:x86組裝中的遞歸阿克曼 - 彼得函數(NASM)

* A(0; M)= M + 1

* A(N + 1; 0)= A(N 1)

* A(N + 1; m + 1))= a(n; a(n + 1; m))

我的問題是我甚至無法想象如何正確啓動。到目前爲止,我只在Assembly中實現了一個「x次方」函數。

這裏是我到目前爲止有: http://pastebin.com/rsWALyCq(德國提示只要求n和m)

林心存感激的幫助,我可以用這一個讓每一個位。

-

所以我做推/流行語句對稱的現在,但仍得到一個分割故障。我試圖調試整個事件,並在第一個案例中放置了Debug-Message。我編譯了程序並嘗試了n = 0和m = 0,並且他沒有打印Debug-Message,所以他甚至沒有輸入第一個字母。我似乎無法弄清楚爲什麼他不這樣做。

繼承人我現在嘗試: http://pastebin.com/D4jg7JGV

回答

2

SOLUTION:

好吧,我發現了問題:

不知怎的,我沒有管理EBP和ESP不正確的。所以我知道在每個Funktion-Call中都使用了ENTER和LEAVE宏,現在整個事情都能正常工作。 繼承人的解決方案,感謝您的時間:

http://pastebin.com/ZpPucpcs

0

如果你確定有這樣做遞歸(所有的服務員棧幀增長),那麼這是相當簡單的。其基本思想,在子程序的代碼:

ACKERMANN 
Get n and m off the stack or from registers 
Compare n to zero. 
If it is equal, jump to FIRSTCASE 
Compare m to zero 
If it is equal, jump to SECONDCASE 
put n + 1 into the first argument slot 
put m into the second argument slot 
call ACKERMANN 
put n into the first argument slot 
put the return of previous call into second argument slot 
call ACKERMANN 
put the return of the previous call into the return register/stack slot 
return 

FIRSTCASE 
put m+1 in the return register/stack slot 
return 

SECONDCASE 
Put n into the first argument slot 
put 1 into the second argument slot 
call ACKERMANN 
put the return of previous call into return register/stack slot 
return 
+0

謝謝,我想這會幫助我,現在我儘量把這種結構。如果我完成了,我會回到這裏發佈完整的解決方案。 – WeGi 2011-06-02 17:50:57

+0

我試圖把整個東西翻譯成Asm,但是在彙編和編譯後,我得到了一個Segmentation錯誤,並且確實找不到它發生的原因。即使我以n = 0和m = 0開始,所以他必須跳轉到firstcase。 http://pastebin.com/s6xh7kPc – WeGi 2011-06-02 18:18:20

+0

只查看了你的代碼,但是我看到你的push/pops不是對稱的。如果滿足條件,你將在沒有推動的情況下彈出,而在兩個調用之間的彈出比彈出更多。 – hirschhornsalz 2011-06-02 20:14:42