News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Simple console piping example

Started by Vortex, January 22, 2012, 10:19:47 AM

Previous topic - Next topic

Vortex

Here is a simple console pipe example. The executable pipe.exe reads data from STDIN and converts it to uppercase.

dir /b | pipe

BUILD.BAT
CRT0
FILES.TXT
PIPE.ASM
PIPE.EXE
PIPE.INC
SAMPLE.TXT
STDOUT.ASM
UPPERCASE.ASM


pipe "This is a test"

THIS IS A TEST


Source code :


include pipe.inc

BUFSIZE equ 1024

.data?

dwRead  dd ?
buffer  db BUFSIZE dup(?)

.code

main PROC C uses esi argc:DWORD,argv:DWORD

LOCAL hInput:DWORD

    invoke  GetStdHandle,STD_INPUT_HANDLE
    mov     hInput,eax

    mov     esi,OFFSET buffer
    cmp     argc,1+1
    jne     @f
    mov     edx,argv
    mov     esi,DWORD PTR [edx+4]
    call    PrintStr
    ret
@@:
    invoke  ReadFile,hInput,esi,BUFSIZE,ADDR dwRead,NULL
    test    eax,eax
    je      @f
    cmp     dwRead,0
    je      @f
    mov     eax,dwRead
    mov     BYTE PTR [esi+eax],0
    call    PrintStr
    jmp     @b
@@:
    ret

main ENDP

PrintStr PROC

    invoke  uppercase,esi
    invoke  StdOut,eax
    ret

PrintStr ENDP

END