The MASM Forum Archive 2004 to 2012

Specialised Projects => Pelle's Macro Assembler Development => Topic started by: Vortex on January 22, 2012, 10:19:47 AM

Title: Simple console piping example
Post by: Vortex on January 22, 2012, 10:19:47 AM
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