.386
.model flat, stdcall
option casemap:none

include c:\masm32\include\windows.inc
include c:\masm32\include\masm32.inc
include c:\masm32\include\kernel32.inc
include c:\masm32\include\user32.inc


includelib c:\masm32\lib\masm32.lib
includelib c:\masm32\lib\kernel32.lib
includelib c:\masm32\lib\user32.lib

BUFFER_SIZE = 501
.data

buffer BYTE BUFFER_SIZE DUP(?)
filename BYTE "output.txt",0
fileHandle HANDLE ?
stringLength DWORD ?
bytesWritten DWORD ?

WriteToFile_1 DWORD ?

.code
start:
		
		; Create a new text file.
		INVOKE CreateFile,
			ADDR filename, GENERIC_WRITE, FILE_SHARE_WRITE, NULL,
			CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0
			
		mov fileHandle,eax

		; Ask the user to input a string.
		invoke StdIn, addr buffer, BUFFER_SIZE
		mov stringLength, eax

		; Write the buffer to the output file.
		mov eax,fileHandle
		mov edx,OFFSET buffer
		mov ecx,stringLength
		INVOKE WriteFile,		; write buffer to file
                      	eax,			; file handle
			edx,			; buffer pointer
			ecx,			; number of bytes to write
			ADDR WriteToFile_1, 	; number of bytes written
			0			; overlapped execution flag

		mov eax, WriteToFile_1
		invoke dwtoa, eax, addr buffer
		invoke StdOut, addr buffer

		;CLOSE FILE
		mov eax,fileHandle
		INVOKE CloseHandle, eax


invoke ExitProcess, 0
end start
