0 Replies - 1591 Views - Last Post: 25 April 2009 - 11:58 AM

#1 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

output to stdout in windows

Posted 25 April 2009 - 11:58 AM

Description: This was done with tasm32 and tested from Borland C++. To use it assemble it to an object file and include the line 'extern int stdout(const char *);' at the begining of your C code. To use in assembly push a pointer to a null terminated string and call.This function output to the stdout stream. This uses the C calling convention so it can be called form a C program.
PUBLIC _stdout
EXTERN WriteFile		:near
EXTERN GetStdHandle		:near


MODEL flat, C
_DATA	segment dword public use32 'DATA'
	hStdOut DD 0 		;Used to hold the hStdOut
	initState DD 0		;Used to tell if we are initialized or not
_DATA	ends
_TEXT	segment dword public use32 'CODE'

_stdout proc near	; int stdout(char *str);
	push 	ebp                            
	mov 	ebp, esp
	add 	esp, -4
	mov		eax, initState	; first lets see if we are initialized...
	cmp		eax, 00h
	jne		stdout_doneinit
	push 	STD_OUTPUT_HANDLE	; push -11 -- argument to GetStdhandle
	call 	GetStdHandle
	mov		hStdOut, eax
	mov		initState, 1
	push	0
	lea		eax, [ebp-4]
	push	eax
	mov		edx, dword ptr [ebp+8]	; ebx == pointer to string..
	push	edx
	call	_strLen
	push 	eax
	push	edx
	push	hStdOut
	call	WriteFile
	mov		eax, [ebp-4]
	mov 	esp, ebp
	pop 	ebp
_stdout endp

_strLen proc near   ; _stdcall int strLen(char *str)
	push 	ebp                            
	mov 	ebp, esp                      
	push 	edi
	mov 	edi, dword ptr [ebp+8]	; get our pointer from the stack
	xor 	ecx, ecx	;zero ecx
	not 	ecx			;set ecx to the max integer 0xFFFF FFFF
	xor 	al, al		;zero  al
	cld 				;clear the direction flag so we move along the string forwards
	repne	scasb		;scan string byte for byte [di] == al
	not 	ecx			;find #of loops for repne
	dec 	ecx			;subtract 1 for the last char (00h)...
	mov 	eax, ecx	;set our return value...
	pop 	edi
	pop 	ebp
	ret 	4
_strLen 	endp

_TEXT    ends


Is This A Good Question/Topic? 0
  • +

Page 1 of 1