Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2004-11-11
3
Medium Priority
?
665 Views
Last Modified: 2008-03-06
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.
0
Comment
Question by:Paladinxyz
2 Comments
 
LVL 9

Accepted Solution

by:
BeyondWu earned 2000 total points
ID: 12580929
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
 
LVL 61

Expert Comment

by:mbizup
ID: 15625402
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome to 2018! Exciting things lie ahead in the world of tech. To start things off, we compiled great member articles on how to stay safe, ways to learn, and much more! Read on to start your new year right.
Often, the users face difficulty in accessing Outlook 2016 PST files on Windows 10 computer. One of the reasons behind it is the improper functioning of MS Outlook when the user tries to open it. MS Outlook suddenly stops working, or it will not op…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question