News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Segmentation fault when calling C function fgets in assembly

Started by johny23, April 23, 2016, 09:24:04 AM

Previous topic - Next topic

johny23

I tried to read an input into a buffer with fgets. I pushed the 3 parameters, but got segmentation fault. I tried to see the problem with GDB, but I didn't understand the message that I got there.             
This is the code:                                                         
                                               
                                                                                                                              
    section .rodata
    buffer: db 10
    section .text
            align 16
            global main
            extern fgets
            extern stdin 
    main:
    push ebp
    mov ebp, esp
    pushad
    push dword[stdin];
    push 10;
    push buffer;
    call fgets;
    add esp, 12;
    popad ; Restore registers
    mov esp, ebp ; Function exit code
    pop ebp
    ret                         
 
                           
And this is the message that I got:                 

Program received signal SIGSEGV, Segmentation fault.
__GI__IO_getline_info (fp=fp@entry=0xf7fb1c20 <_IO_2_1_stdin_>,
    buf=buf@entry=0x80484f0 "\n", n=8, n@entry=9, delim=delim@entry=10,
    extract_delim=extract_delim@entry=1, eof=eof@entry=0x0) at iogetline.c:86
86      iogetline.c: No such file or directory.

                   
What is wrong with my code?

hutch--


K_F

It's 25 years since I bothered mixing C with asm... but a question.

It is correct to put C function declarations in a .text segment ?
'Sire, Sire!... the peasants are Revolting !!!'
'Yes, they are.. aren't they....'

TWell

That read buffer size was only 1 byte in readonly section?
    section .bss
    buffer: resb 15

Vortex

Hi johny23,

Here is an example for you. The application receives keyboard input using the function fgets and displays the text in the console :

include         fgets.inc

_iobuf STRUCT
    _ptr        DWORD ?
    _cnt        DWORD ?
    _base       DWORD ?
    _flag       DWORD ?
    _file       DWORD ?
    _charbuf    DWORD ?
    _bufsiz     DWORD ?
    _tmpfname   DWORD ?
_iobuf ENDS


FILE TYPEDEF _iobuf

BUFF_SIZE equ 256

.data

format          db 13,10,'%s',0
msg             db 'Type some text and hit RETURN',13,10,0

.data?

stdout          dd ?
stdin           dd ?
stderr          dd ?
buffer          db BUFF_SIZE dup(?)

.code

start:

    call    crt___p__iob
    mov     stdin,eax           ; #define stdin  (&__iob_func()[0])

    mov     ecx,SIZEOF(FILE)

    add     eax,ecx
    mov     stdout,eax          ; #define stdout (&__iob_func()[1])

    add     eax,ecx
    mov     stderr,eax          ; #define stderr (&__iob_func()[2])

    invoke  crt_printf,ADDR msg

    invoke  crt_fgets,ADDR buffer,BUFF_SIZE,stdin

    invoke  crt_printf,ADDR format,ADDR buffer
           
    invoke  ExitProcess,0

END start