x86汇编(NASM)中的递归Ackermann-Peter函数

时间:2011-06-02 16:11:53

标签: assembly recursion x86 nasm ackermann

我试图在x86 NASM-Assembly中实现递归的Ackermann-Peter-Function。函数定义如下:

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

我非常感谢能得到这一点的所有帮助。

-

我现在制作推/弹声明Symetric,但仍然会出现分段错误。我试图调试整个事情并在第一个案例中放置一个Debug-Message。我编译了程序并尝试了n = 0和m = 0并且他没有打印调试消息,因此他甚至不进入第一个案例。我似乎无法找出他为什么不这样做。

继承我目前的尝试: http://pastebin.com/D4jg7JGV

2 个答案:

答案 0 :(得分:2)

<强> SOLUTION:

好的,我发现了问题:

不知怎的,我没有管理ebp和esp不对。所以我知道在每个Funktion-Call中使用了ENTER和LEAVE宏,现在整个过程正常。 继承了解决方案,谢谢您的时间:

http://pastebin.com/ZpPucpcs

答案 1 :(得分: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