Mixed Code


 

// C not CPP
// declare all variables at TOP of function
// use scanf_s with character count for strings (allow for 0 terminator)
//
#include 
#include 
#include 
//#include  NO

int myAsmProc(int, int*); // count, pointer
int myCProc(int, int*);

int main()
{
    int val = 5;
    int ref = 0;
    int *p, *p2;
    int result;
    int r, i;
    char st[10];

    // get some random
    srand( (unsigned int)time(NULL) );
    r = rand()%100;

    // get some ram
    p = (int*)(malloc(r*4));
    p2=p;
    for(i=0; i<r; i++)
    {
      *p = i;
      printf("p is %p and it points to %d.\n", p , *p );
      p++;
    }
    //std::cout << "hello";

    // call asm
    result = myAsmProc(r,p2); // count adn ORIGINAL pointer (saved)

    
    result = myCProc(r,p2);
    printf("After myCProc call: %d\n\n",result);

    // get some random
    srand( (unsigned int)time(NULL) );
    r = rand();
    
    // ram the random into the ram
    p=p2;
    *p = r;
    p++; //ack - lost my pointer to start of my mem!! no - p2
    *p = 99;
    --p;
    printf("p is %p and it points to %d. Right?  %d\n\n", p , *p, r );
    free(p2);

    printf("Who you? ");
    scanf_s("%s", st, 9); 

    printf("How many widgets? ");
    scanf_s("%d", &val); 
    
    printf("Hello %s has %d widgets\n\n", st, val);

    __asm
    {
        mov eax, val
        add eax, 1
        shl eax, 1
        mov val, eax
    }

    printf("Hello %s has %d widgets\n\n", st, val);

    result = myAsmProc(val, &ref); // or p
    printf("After myAsmProc call: %d %d %d\n\n",result, val, ref);


    getchar(); getchar();
    return 0;
}