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

发布于 2024-11-11 18:35:53 字数 631 浏览 3 评论 0原文

我正在尝试在 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))

我的问题是我什至无法想象如何正确开始。到目前为止,我只在汇编中递归地实现了“x 的幂”函数。

这是我到目前为止所拥有的: http://pastebin.com/rsWALyCq (德语提示只要求 n 和 m)

我感谢每一位我可以通过这个获得帮助。

--

所以我现在将推送/弹出语句设为对称,但仍然出现分段错误。我尝试调试整个事情并在第一个案例中放置了一条调试消息。我编译了该程序并使用 n=0 和 m=0 进行了尝试,但他没有打印调试消息,因此他甚至没有输入第一种情况。我似乎无法找出他为什么不这样做。

这是我目前的尝试: http://pastebin.com/D4jg7JGV

im trying to implement the recursive Ackermann-Peter-Function in x86 NASM-Assembly. The Function is defined as follows:

*a(0;m) = m + 1

*a(n + 1; 0) = a(n; 1)

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

My Problem is i can't even imagine how to start properly. By now i only implemented an "power of x" Function recursively in Assembly.

Here is what i have so far:
http://pastebin.com/rsWALyCq (The german prompts just ask for n and m)

Im thankfull for every bit of help i can get with this one.

--

SO i made the push/pop Statements Symetric now, but still get an Segmentation fault. I tried to debug the whole thing and placed a Debug-Message inside the firstcase. I compiled the Program and tried it with n=0 and m=0 and hes not printing the Debug-Message, so he isnt even entering the firstcase. I can't seem to manage to find out why hes not doing it.

Heres my current try:
http://pastebin.com/D4jg7JGV

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜空下最亮的亮点 2024-11-18 18:35:53

解决方案:

好吧,我发现了问题:

我没有正确管理 ebp 和 esp。因此,我现在在每个函数调用中都使用了 ENTER 和 LEAVE 宏,现在整个事情都可以正常工作了。
这是解决方案。感谢您的宝贵时间:

asm_main:
    ENTER 0,0               ;setup Routine
    PUSHA
    MOV eax, prompt1        ;ask for n

完整代码:
http://pastebin.com/ZpPucpcs

SOLUTION:

Ok I found the problem:

I didn't manage the ebp and esp right. So, I now used the ENTER and LEAVE Macros in every function call and now the whole thing works properly.
Here is the Solution. Thank you for your time:

asm_main:
    ENTER 0,0               ;setup Routine
    PUSHA
    MOV eax, prompt1        ;ask for n

Full Code:
http://pastebin.com/ZpPucpcs

十二 2024-11-18 18:35:53

如果您可以递归地执行此操作(伴随所有随之而来的堆栈帧增长),那么这相当简单。基本思想,在子程序的代码中:

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

If you're ok with doing this recursively (with all the attendant stack frame growth), then this is fairly straightforward. The basic idea, in the code of the subroutine:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文