English 中文(简体)
x86程序集(NASM)中的递归Ackermann-Peter函数
原标题:Recursive Ackermann-Peter Function in x86 Assembly (NASM)

我试图在x86 NASM程序集中实现递归Ackermann-Peter函数。函数定义如下:

*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的幂”函数。

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

我很感激我能得到的每一点帮助。

--

所以我现在做了push/pop Statements Symetric,但仍然存在Segmentation错误。我试着调试整件事,并在第一个案例中放置了一条调试消息。我编译了这个程序,并在n=0和m=0的情况下进行了尝试,但他没有打印调试消息,所以他甚至没有输入第一种情况。我似乎弄不明白他为什么不这么做。

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

最佳回答

解决方案:

好的,我发现了问题:

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

问题回答

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

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




相关问题
Recursive same-table query in SQL Server 2008

I have the following table in a SQL Server 2008 database: Id Name ParentFolder -- ---- ------------ 1 Europe NULL 2 Asia NULL 3 Germany 1 4 UK 1 5 China ...

Finding a class within list

I have a class (Node) which has a property of SubNodes which is a List of the Node class I have a list of Nodes (of which each Node may or may not have a list of SubNodes within itself) I need to be ...

Selecting records during recursive stored procedure

I ve got a content management system that contains a hierarchical structure of categories, with sub-categories subject to different ordering options at each level. Currently, that s retrieved by a (...

热门标签