Hi!

I'm trying to convert C++ code to nasm ..unsuccessful

Well, here is the c++ code:

int recursion(int n)
{
    if ((n==0) || (n==1))
       return 1;
    else if (n==2)
         return 7;
    else
        return recursion(n-3)+recursion(n-2);
}
int main ()
{
    for (int i=3; i<=15; i++)
    {
        cout << recursion(i)  << endl;
    }
        return 0;
}

and this is my asm converted code:

bits 32
extern _printf, _scanf
global _main

section .data
	output db "%d",10,0 
section .text

_recursion:
      mov eax, [esp+4]
      cmp eax, 1
      je .isone
      cmp eax, 2
      je .istwo
      cmp eax, 0
      je .iszero

      ;recursion
      sub eax, 3       ;(n-3)
      mov edx, eax
      push eax
      call _recursion
      pop ebx
      mov eax, edx
      inc eax          ;(n-2)
      push eax
      call _recursion
      pop eax
      add eax, ebx
      jmp .end
.isone:
      mov eax, 1
      jmp .end
.istwo:
      mov eax, 7
      jmp .end
.iszero:
      mov eax, 1
      jmp .end
.end:
      ret

_main:
      pushad
      mov ecx, 3   ;count 3....15
.doituntil:
      push ecx
      push ecx
      call _recursion
      add esp, 4

      ;cout
      push eax
      push dword output
      call _printf
      add esp, 8

      ;count=count+1
      pop ecx
      inc ecx

      ;if count<=15
      cmp ecx, 15
      jle .doituntil

      popad
      ret

I need help..
Thank you!

This article has been dead for over six months. Start a new discussion instead.