URGENT C with inline Assembly, Fscanf a file, getData, Always return overflow - HELP

First of all, I want to apologize for the reposting.

http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_21204217.html

Hi, I'm having trouble with the following function, getData( ). It's supposed to load an array x with data from file f, returning cursize, the number of elements loaded into x. This getData( ) function will only be called if the filename the user enters is a valid one. Otherwise, the main will just terminates the program. And if the file is opened successfully, then getData will either return a boolean value FALSE (Overflow) or TRUE (Data from the file has been loaded into the X array) back to main. I also include my main below getData( ) function. Overflow should only occur when cursize is larger than maxsize (15) and there is still data in the file that has not been loaded.

The PROBLEM I'M HAVING is that I have the codes compiled successfully, but no matter if it's an empty file, a file with less than 15 integers, or a file with 20 integers (overflow), it ALWAYS says that it's overflowed. Please help.



Boolean getData( FILE* f, int x[], int* cursize, int maxsize )
{
   int hold;
   char format[] = "%d";

   _asm
   {
       mov ebx, x      
       mov esi, cursize          
       mov dword ptr [esi], 0     //cursize = 0    

   Top:
         push esi                         //save ptr to cursize
         push ebx                         //save ptr to x[cursize]
         lea ebx, hold                    //save the address
       push ebx                         //fscanf's 3rd param &d
       lea ebx, format               //save the format
       push ebx                         //fscanf's 2nd param "%d"

       push f

       call fscanf                    //fscanf(f, "%d", &d)

       add esp, 12                    //clear param off stacks
       pop ebx                         //restore saved registers
       pop esi

       mov eax, hold
       cmp eax, EOF                    //the end of the file yet?
       jne Next                         //if not, keep going      

      mov eax, 1                   //Finish loading, if cursize is 0,
                                   //this file is empty
       jmp Done                         //return TRUE

   Next:  
       mov ecx, maxsize                //cursize > 15?
       cmp dword ptr [esi], ecx            //check overflow
       jg Ovfl                         //cursize > maxsize
          mov [ebx], eax                  //x[cursize] stores that value      

       inc dword ptr [esi]                //cursize++
       add ebx, 4                    //next element
       loop top
   Ovfl:                              //Not EOF and overflowed
       mov eax, 0                    //return FALSE
   Done:
   }
}

/*THIS IS MY MAIN FUNCTION*/

#define MAX 15

int main()
{
   int SECRET_NUM = 100;  /* WHAT IS THIS FOR?  */
   int x[MAX];
   int cursize;
   int lowest, highest, avg, median;
   FILE* dataFile;      /* internal file pointer for the file */
   char filename[100];  /* string to hold the filename */
   Boolean loadOK;

   /* Ask the user for a filename, then open the data file.
      Be sure to check that the open was successful.
      If not, display a message, and terminate the program.
   */
   printf("Please enter a file name: ");
   scanf("%s", filename);
   dataFile = fopen(filename, "r");

   if ( dataFile != NULL )
   {
      loadOK = getData( dataFile, x, &cursize, MAX );

      /* If the load was not OK, we ran out of room.
         Display a message, and terminate the program.
      */
       if ( loadOK == 0 )
       {
          printf("Fail to load data, OVFL occurred.\n");
         exit(EXIT_FAILURE);
       }

       /* Array loaded successfully */
       else
      {
          /* If there were zero elements in the data file, display a
            message and terminate the program.
          */      
           if ( cursize == 0 )
           {
               printf("The file is empty. Program terminated\n");
               exit(EXIT_FAILURE);
           }

           else
           {
                  sortData( x, cursize );                
               displayGrades( x, cursize );
               doStats( x, cursize, &lowest, &highest, &avg, &median );
               displayStats( cursize, lowest, highest, avg, median );
           }
       }
   }

   else
   {
       printf("Fail to open file.\n");
       exit(EXIT_FAILURE);
   }

   printf("\n\nSECRET_NUM SHOULD BE 100: %d\n", SECRET_NUM);

   return 0;
}


Thanks for any help guys.
PaladinxyzAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BeyondWuCommented:
bool getData( FILE* f, int x[], int* cursize, int maxsize )
{
   int hold;
   char format[] = "%d";

   _asm
   {
       mov ebx, x      
       mov esi, cursize          
       mov dword ptr [esi], 0     //cursize = 0    

   Top:
         push esi                         //save ptr to cursize
         push ebx                         //save ptr to x[cursize]
         lea ebx, hold                    //save the address
       push ebx                         //fscanf's 3rd param &d
       lea ebx, format               //save the format
       push ebx                         //fscanf's 2nd param "%d"

       push f

       call fscanf                    //fscanf(f, "%d", &d)

       add esp, 12                    //clear param off stacks
       pop ebx                         //restore saved registers
       pop esi

      // <--- Here is the problem!!!
                       // if it reaches at EOF, the return value will be EOF, the hold will not be updated
       cmp eax, EOF                    //the end of the file yet?
       jne Next                         //if not, keep going      
       mov eax, 1                                    //Finish loading, if cursize is 0,
       jmp Done                         //return TRUE
       // endof modification

       mov eax, hold
       cmp eax, EOF                    //the end of the file yet?
       jne Next                         //if not, keep going      

      mov eax, 1                 //Finish loading, if cursize is 0,
                              //this file is empty
       jmp Done                         //return TRUE

   Next:  
       mov ecx, maxsize                //cursize > 15?
       cmp dword ptr [esi], ecx            //check overflow
       jg Ovfl                         //cursize > maxsize
          mov [ebx], eax                  //x[cursize] stores that value      

       inc dword ptr [esi]                //cursize++
       add ebx, 4                    //next element
       loop top
   Ovfl:                              //Not EOF and overflowed
       mov eax, 0                    //return FALSE
   Done:
   }
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mbizupCommented:
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I will leave the following recommendation for this question in the Cleanup topic area:
    Accept: BeyondWu {http:#12580929}

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

mbizup
EE Cleanup Volunteer
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Assembly

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.