1

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

Sampson
  • 265,109
  • 74
  • 539
  • 565
WeGi
  • 1,908
  • 1
  • 21
  • 33

2 Answers2

2

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

WeGi
  • 1,908
  • 1
  • 21
  • 33
  • ENTER isn't a "macro", it's a (slow) machine instruction. You should `push ebp` / `mov ebp, esp` instead, if you want to set up a traditional frame pointer. That's all `enter 0,0` does. – Peter Cordes Jan 28 '21 at 20:15
0

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
Shea Levy
  • 5,237
  • 3
  • 31
  • 42
  • Thanks, i think this will help me, now i try to translate that structure. I get back here to post the full solution, if im done. – WeGi Jun 02 '11 at 17:50
  • I tried to translate the whole thing to Asm now, but after assembling and compiling i get a Segmentation fault and cant really find why it happens. Even if i start with n=0 and m=0 so he has to jump to firstcase. http://pastebin.com/s6xh7kPc – WeGi Jun 02 '11 at 18:18
  • Only glanced over your code, but I see that your push/pops aren't symmetric. If a condition is met you will pop without having pushed, and between to two calls are more pushes than pops. – Gunther Piez Jun 02 '11 at 20:14