Hey Guys, this is my first post and I am a beginner with this language.  I hope you can help me!
Before, some background

Compiler: gcc
Assembler: gas
objective: open a file, copy the text that is inside, and paste it in a new file.
Problem:  function fgets
Error: Segmentation Fault

code where the error appears:

processingfiles:    pushl %ebp        #prologue
                    movl %esp, %ebp  #prologue
                    pushl $mode2      #read only
                    pushl $sourcefilepath
                    call fopen
                    addl $8, %esp 
                    mov %eax, fp # fp=file pointer
                    cmpl $0, fp # if fp==NULL (0) error!
                    je errors
                    pushl $sourcefilepath
                    pushl $10  #amount of data to be pasted
                    pushl $file  #name of the file where the data will be pasted
                    call fgets  #here de problem!
                    jmp fin
errors:              pushl error
                    call printf
                    addl $4, %esp
                    jmp fin

fin:                    leave

Do you know what the problem is? what am I missing?
thanks in advance
Posted on 2011-02-17 12:40:22 by massem
I believe you're using the wrong parameters for fgets:

                    pushl $sourcefilepath
                    pushl $10  #amount of data to be pasted
                    pushl $file  #name of the file where the data will be pasted
                    call fgets  #here de problem!

In order pushed (opposite order listed in C source), you want file descriptor, size, buffer. You appear to be reusing the filename as a buffer(?). Or is "file" supposed to be the buffer? In any case, I think what you want is:

    pushl fp  # no '$'! - as returned from fopen
    pushl $10
    pushl $buffer  # call it what you like
    call fgets

Also, you don't "addl $12, %esp" to remove the parameters. In addition, you seem to be unconditionally jumping over the "error" section. I could be wrong about this - not very familiar with (G)as syntax, nor with the C library's "high level" (buffered) fopen, etc. I'm pretty sure fgets doesn't want any "names", but the "FILE *" returned from fopen (I guess it should be called "stream", not "file descriptor"?). Give it a try and see if it helps.


Posted on 2011-02-17 13:50:23 by fbkotler
Just taking a glance, your biggest problem (not including what fbkotler pointed out) is exactly what you're comments state.
    call fgets  #here de problem!
    jmp fin

Yep! You're right, there is a problem there. You were so diligent about cleaning up after printf and fopen, but you forgot to do the same with fgets!


    .ascii "r\0"

    .ascii "demo.txt\0"

    .ascii "error: could not open file.\n\0"


    .space 4

    .space 1025


.globl print_file_out
    enter $0, $0

        pushl $strModeRead
        pushl $strInputFile
        call fopen
        addl $0x08, %esp

        cmpl $0x00, %eax
        je _opn_err
        movl %eax, (fd)

        pushl %eax # Pointer to File for fgets()
        pushl $1024 # Maximum size of buffer.
        pushl $strBuffer # Storage buffer.
        call fgets
        addl $0x0B, %esp

        pushl (fd)
        call fclose
        addl $0x04, %esp

        mov $strBuffer, %eax # Return pointer to buffer on success       
        jmp _ovr_err


        pushl $strFileError
        call printf
        addl $0x04, %ebp

        xorl %eax, %eax # Return zero on error



Also remember that fgets() reads a line at a time, so this will print out the first line from "demo.txt" and you shouldn't leave any hanging pointers out there, so make sure you call fclose() on any fopen()'d descriptor.

P.S. Writing this has reminded me why I hate AT&T Syntax.
Posted on 2011-02-17 19:54:19 by Synfire
Hi Guys, I really appreciate your answers. You were right,  my mistakes were:

                    pushl  $sourcefilepath  --------->  instead of $sourcefilepath I should have  written the file descriptor....
                    pushl $10 
                    pushl $file  -------------->instead of the filename  I should have written a string buffer to save data
                    call  fgets 
                    addl $12, %esp

I finish my program copying stringbuffer into a new text file... like that

                  pushl  $mode1  #write mode
                    pushl $file  #file name
                    call fopen 
                    addl $8, %esp
                    movl %eax, fp
                    pushl $strBuffer
                    pushl fp
                    call fprintf
                    addl $8, %esp

and it worked! 
thank you so much for helping me, I am new in assembly language

Posted on 2011-02-18 12:50:27 by massem