Link to home
Start Free TrialLog in
Avatar of Paladinxyz
Paladinxyz

asked on

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

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

https://www.experts-exchange.com/questions/21204217/URGENT-C-with-inline-Assembly-Fscanf-a-file-getData-Always-return-overflow-HELP.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.
ASKER CERTIFIED SOLUTION
Avatar of BeyondWu
BeyondWu
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mbizup
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