Mixed C++ and ASM


// CFuncs.cpp
//
#include 
using namespace std;

// prototypes
int  Func1( int );

int main(  )
{
   int num, myedx, choice;
   do
   {
      cout << "enter 0 to quit\n"; // menu
      cin >> choice;               // get user choice

     num = Func1(choice);          // call functon with int return

     __asm{ mov myedx, edx }       // single asm statement to grab edx register
                                   // after returning before printing which might destroy edx

     // out edx in hex and dec as well as displaying argument and return
     cout << "edx register contents: " << hex <<  myedx  << " and in dec " << dec << myedx << endl;
     cout << choice << " and result is " << num  << endl << endl;
   }
   while ( choice != 0 ); // until user chooses to exit
   return 0;
}

// function in c++ with embedded asm
int Func1( int number )
{
  __asm
  {
   mov eax, number
   shl eax, 1 // double it
   mov number , eax
   cmp eax, 100
   jne ENDING
      // here only when eax is 100 (input is 50)
      push number // argument to func1 when we call ourself
      call Func1  // RECURSION - doubles the argument
      pop edx     // returns here - cleans up stack
      mov edx, eax // move the NEW calculated number from eax to edx
                   // see if it survives two returns
                   
  ENDING: // just returns
  }
  return number;
}