I'm trying to call the function fgets to read from standard input. How do I get the FILE* stdin in assembly?

I'm using nasm on cygwin.
Thanks in advance.

So, I have been doing some digging, and figured out how to use the system calls _write and _read, that use file descriptors instead of a FILE*.

I found some references to a function called fileptr that returns a FILE* corresponding to a file descriptor, but that doesn't seem to be part of the C standard library.

Anyway, here is my test code. It builds and runs correctly under Cygwin.

; test_io.asm
; Test _read and _write system calls
; nasm -o test_io.o -fwin32 test_io.asm
; gcc -o test_io test_io.o
section .data
prompt      db 'Enter a string: ',0
prompt_len  equ $ - prompt

BUFSZ   equ 1024
STDIN   equ 0
STDOUT  equ 1

section .bss
buffer  resb BUFSZ

section .text
    global _main
    extern _read, _write
    push    prompt_len
    push    prompt
    push    STDOUT
    call    _write
    add     esp, 12
    push    BUFSZ - 1   ; leave space for terminating 0
    push    buffer
    push    STDIN
    call    _read
    add     esp, 12
    push    eax         ; _read returns string length in eax
    push    buffer
    push    STDOUT
    call    _write
    add     esp, 12
    mov     eax, 0

That would seem to be the way to go.

FILE*'s are constructed by the C runtime library, which may necessitate you dragging in a whole lot of stuff you didn't want. If you did want it, there would seem to be little point in writing this bit in ASM.

This article has been dead for over six months. Start a new discussion instead.