fscanf in inline assembly code

Posted on 2004-11-11
Last Modified: 2010-04-17
i have a method called readData(FILE* f, int[] x, int* arraysize)
i wanted to know how to you get data from the file ptr f and store it into the x array using inline assembly code
here is the part of code i am trying to fix

int hold;
    char format[] = "%d";
        mov  ebx,x
       mov  ecx,cursize
       mov  edx,f

      mov  dword ptr [ecx],0  ; cursize = 0
      ; call fscanf
      ; push fscanf parameters & call fscanf
        lea  ebx,hold
        push ebx
        lea  ebx,format
        push ebx      
        lea  ebx,f
        push ebx    
        call fscanf
        add  esp,12      

Question by:majorlag
    LVL 48

    Expert Comment

    Why do you need this in Assembly?

    Anyway, wtite this in C and read Assembly code generated by compiler. You can see Assembly code in compiler listings, or in debugger, selecting "View Assembly" options.

    Author Comment

    I am using Visual Studios .net 2003 to do inline assembly and i dont think or know where the "View Assembly" options is.
    I need to do this in Assembly because my project is to read  a list of data from a file and put them into an array.
    And the only thing I am stuck on is how to get data from the file and saving it to a register so i can copy it to the array.
    I heard it is almost the same as scanf command but I tried many different ways and couldnt get it to work with fscanf.
    so it would be nice if you or somebody can show me how
    thank you in advance
    LVL 48

    Accepted Solution

    I still don't understand - is it possible to do what you want in C? I don't see anything special in reading data from file, to do this in Assembly. Can you write this in C?

    For example, create C++ Console application with the following code:

    int _tmain(int argc, _TCHAR* argv[])
        int n;
        scanf("%d", &n);

        return 0;

    Set breakpoint on sscanf line and select Start command. When debugger stops on this lise, select: Debug - Windows - Disassembly. You can see how scanf code is translated to Assembly by C++ compiler:

        int n;
        scanf("%d", &n);

    00411B1E  lea         eax,[n]
    00411B21  push        eax  
    00411B22  push        offset string "%d" (4270C8h)
    00411B27  call        @ILT+980(_scanf) (4113D9h)
    00411B2C  add         esp,8
    LVL 1

    Assisted Solution

    I'm puzzled as to why you want to do it in assembly - if you want to write formatted data from a file into an array, and use scanf to transform it from ascii representation to integer, for example

    <code fragment, no error checking, haven't tried compiling>  :)

    int myArr[10] = {0};

    for ( int i = 0; i < 10; i++ )
       scanf("%d", &myArr[i]);

    myArr could be a lot more complex - say a class/struct with multiple data members

     scanf("%d %d %d", &myArr[i].m_a, &myArr[i].m_b, &myArr[i].m_c);

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Suggested Solutions

    Title # Comments Views Activity
    string initialization in java 11 94
    Fix45 challenge 15 66
    Fibonacci challenge 11 61
    countHi challenge 25 61
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    This is about my first experience with programming Arduino.
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now