format PE Console
entry start

include 'win32ax.inc'

section '.data' data readable writeable
;----------------------------------------------------
include 'ascii.asm'
_oh					dd 0
_consoleWritten		dd 0
_obuffer			rb 0xFF
_starttime			dd 0



section '.code' code readable executable
;----------------------------------------------------
start:
	invoke	GetStdHandle,STD_OUTPUT_HANDLE
			mov  [_oh],eax
	stdcall	consoleWrite,<'Benchmark Console',13,10,0>

	invoke	GetTickCount
			mov  [_starttime],eax

			mov  esi,1000000
			mov  ebx,string1

	ascii2d_start:
			cmp  esi,0
			je   ascii2d_end
			
	stdcall	sleepsleep_ascii2d,ebx
;			cmp  ecx,0
;			je  @f
;	cinvoke	wsprintf,_obuffer,<'(unsigned) EAX return : %lu',13,10,0>,eax
;			jmp  writeoutput
;	@@:
;	cinvoke	wsprintf,_obuffer,<'(signed) EAX return : %ld',13,10,0>,eax
;	writeoutput:
;	stdcall	consoleWrite,_obuffer
			dec  esi
	@@:
			inc  ebx
			cmp  byte [ebx], 0
			jne  @b
			inc  ebx
			jmp  ascii2d_start
	
	ascii2d_end:
	invoke	GetTickCount
			mov  ecx,[_starttime]
			sub  eax,ecx
	cinvoke	wsprintf,_obuffer,<'millisecond used to process : %lu ',13,10,0>,eax
	stdcall	consoleWrite,_obuffer
	invoke	ExitProcess,0

; input :	asciiAddr = ascii string number starting address / pointer
; output :	EAX = holding translate number
;			ECX = 0 (signed) 1 (unsigned)
proc sleepsleep_ascii2d uses ebx esi edi, asciiAddr
			mov  eax,[asciiAddr]
			mov  edi,_tbl
			dec  eax
			xor  ecx,ecx
	@@:
			inc  eax
			inc  ecx
			cmp  byte [eax],0
			jne  @b
			sub  ecx,2
	; ecx got length now
			
			mov  ebx,[asciiAddr]
			xor  eax,eax
			xor  esi,esi	; hold total
			mov  al, [ebx+ecx]
			sub  al,'0'
			add  esi,eax
	@@:
			dec  ecx
			cmp  ecx,0
			je   .checkNegative
			add  edi,4
			xor  eax,eax
			mov  al,[ebx+ecx]
			sub  al,'0'
			mul  dword [edi]
			add  esi,eax
			jmp  @b
			
	.checkNegative:
			xor  eax,eax
			mov  al,[ebx]
			cmp  al,'-'
			je   .negativeFound
			add  edi,4
			sub  al,'0'
			mul  dword [edi]
			add  eax,esi
			inc  ecx
			ret
			
		.negativeFound:
			mov  eax,esi
			neg  eax
			ret

_tbl	dd 0
		dd 10
		dd 100
		dd 1000
		dd 10000
		dd 100000
		dd 1000000
		dd 10000000
		dd 100000000
		dd 1000000000
endp
	
;---------------------------
proc consoleWrite ascii
;---------------------------
			mov  eax,[ascii]
			dec  eax
			mov  ecx,-1
	@@:
			inc  eax
			inc  ecx
			cmp  byte [eax],0
			jne  @b
	invoke	WriteConsole,[_oh],[ascii],ecx,_consoleWritten,NULL
	.finish:
			ret
endp

section '.idata' import data readable
;----------------------------------------------------
library	kernel32,	'KERNEL32.DLL',\
		user32,		'USER32.DLL'		
		include 'API\KERNEL32.INC'
		include 'API\USER32.INC'
