hi all, i've been trying to develop a recursive hanoi function in intelx86 assembly language. i use nasm to run it. i use this c function as sample
and the assembly code is
but it runs in infinite loop printing
1 --> 2
3 --> 1
2 --> 3
1 --> 2
3 --> 1
2 --> 3
...
and so on
i couldn't figure out why. any help is much appreciated. thanks..
Code:
void hanoi_rec(int n_disc, int head, int middle, int tail, int silent){ if (n_disc == 1){ if(silent != 1) printf("%d --> %d\n", head, tail); } else{ hanoi_rec(n_disc -1, head, tail, middle, silent); if(silent != 1) printf("%d --> %d\n", head, tail); hanoi_rec(n_disc -1, middle, head, tail, silent); } }
Code:
extern printf SECTION .data han1: db "%d --> %d", 10, 0 global asm_hanoi_rec asm_hanoi_rec: push ebp mov ebp, esp mov eax, [ebp+8] mov ebx, [ebp+24] cmp eax, 1 jne L5 cmp ebx, 1 je L7 push dword[ebp+12] push dword[ebp+20] push han1 call printf add esp, 12 jmp L7 L5: dec eax push eax push dword[ebp+12] push dword[ebp+20] push dword[ebp+16] push ebx call asm_hanoi_rec cmp ebx, 1 je L6 push dword[ebp+12] push dword[ebp+20] push han1 call printf add esp, 12 L6: push eax push dword[ebp+16] push dword[ebp+12] push dword[ebp+20] push ebx call asm_hanoi_rec L7: pop ebp ret 20
1 --> 2
3 --> 1
2 --> 3
1 --> 2
3 --> 1
2 --> 3
...
and so on
i couldn't figure out why. any help is much appreciated. thanks..