开发者

Recursive Ackermann-Peter Function in x86 Assembly (NASM)

开发者 https://www.devze.com 2023-03-10 11:59 出处:网络
im trying to implement the recursive Ackermann-Peter-Function in x86 NASM-Assembly. The Function is defined as follows:

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 Statement开发者_Python百科s 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


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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消