NO

Author Topic: Reading from stdin  (Read 411 times)

Offline Vortex

  • Member
  • *
  • Posts: 802
    • http://www.vortex.masmcode.com
Reading from stdin
« on: January 10, 2024, 07:37:33 PM »
Hello,

Here is a quick example reading from the stdin stream with the C libray function fgets :

Code: [Select]
include fgetsPipe.inc

FILE TYPEDEF _iobuf

BUFF_SIZE equ 1024

.data?

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

.data

string  db 'Read line : %s',0

.code

start:

    call    __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  fgets,ADDR buffer,BUFF_SIZE,stdin
    test    eax,eax
    jz      finish

    invoke  printf,ADDR string,ADDR buffer
    jmp     @b

finish:

    invoke  ExitProcess,0

END start

To pipe a simple file to the project executable :

Code: [Select]
type Build.bat | fgetsPipe.exe
Code: [Select]
Read line : \PellesC\bin\polib /OUT:msvcrt.lib /MACHINE:x86 /DEF:msvcrt.def
Read line :
Read line : \PellesC\bin\poasm /AIA32 fgetsPipe.asm
Read line : \PellesC\bin\polink /SUBSYSTEM:CONSOLE /LIBPATH:\PellesC\lib\Win fgetsPipe.obj kernel32.lib msvcrt.lib
« Last Edit: January 10, 2024, 07:39:35 PM by Vortex »
Code it... That's all...

Offline Vortex

  • Member
  • *
  • Posts: 802
    • http://www.vortex.masmcode.com
Re: Reading from stdin
« Reply #1 on: January 11, 2024, 08:26:14 PM »
Here is the 64-bit version :

Code: [Select]
include fgetsPipe.inc

FILE TYPEDEF _iobuf

BUFF_SIZE equ 1024

.data?

buffer  db BUFF_SIZE dup(?)

.data

string  db 'Read line : %s',0

.code

start PROC PARMAREA=4*QWORD

LOCAL stdout:QWORD
LOCAL stdin:QWORD
LOCAL stderr:QWORD

    call    __iob_func          ;__p__iob works on 32-bit operating systems.
    mov     stdin,rax           ; #define stdin  (&__iob_func()[0])

    mov     rcx,SIZEOF(FILE)

    add     rax,rcx
    mov     stdout,rax          ; #define stdout (&__iob_func()[1])

    add     rax,rcx
    mov     stderr,rax          ; #define stderr (&__iob_func()[2])
@@:
    invoke  fgets,ADDR buffer,BUFF_SIZE,stdin
    test    rax,rax
    jz      finish

    invoke  printf,ADDR string,ADDR buffer
    jmp     @b

finish:

    invoke  ExitProcess,0

start ENDP

END start
Code it... That's all...