Assembly language > Assembly discussions

Reading from stdin

(1/1)

Vortex:
Hello,

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


--- Code: ---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

--- End code ---

To pipe a simple file to the project executable :


--- Code: ---type Build.bat | fgetsPipe.exe
--- End code ---


--- Code: ---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
--- End code ---

Vortex:
Here is the 64-bit version :


--- Code: ---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

--- End code ---

Navigation

[0] Message Index

Go to full version