1

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 を要求するだけです)

これで得られるあらゆる助けに感謝します。

--

SO私はプッシュ/ポップステートメントを対称にしましたが、それでもセグメンテーション違反が発生します。全体をデバッグしようとしましたが、firstcase 内に Debug-Message を配置しました。私はプログラムをコンパイルし、n=0 と m=0 で試してみましたが、Debug-Message が出力されないため、firstcase に入ることさえありません。なぜ彼がそれをしないのか、私にはどうにかして見つけることができないようだ.

Heres 私の現在の試み: http://pastebin.com/D4jg7JGV

4

2 に答える 2

2

解決:

わかりました私は問題を見つけました:

私はebpとespを正しく管理していませんでした。そのため、すべての関数呼び出しで ENTER マクロと LEAVE マクロを使用し、すべてが適切に機能するようになりました。これが解決策です。お時間をいただきありがとうございます:

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

完全なコード: http://pastebin.com/ZpPucpcs

于 2011-06-03T08:37:11.257 に答える
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
于 2011-06-02T16:27:40.340 に答える