#define SECONDPARAM 6

:word putch_addr;

:void fastcall _bios_putch(AL)
{
	$int 29h;
}

:void fastcall _dos_putch(AL)
{
	DL = AL;
	AH = 2;
	$int 21h;
}

#define putch _bios_putch
#define putchar _dos_putch

:void fastcall _putch(AL)
{
	BX = putch_addr;
	BX();
}

:void cdecl printf(word str,...)
{
	DSWORD[#putch_addr] = #_dos_putch;
_do_printf:
	_printf(str);
}

:void cdecl cprintf(word str,...)
{
	DSWORD[#putch_addr] = #_bios_putch;
	GOTO _do_printf;
//	_printf(str);
}

:void fastcall _printf(SI)
{
	DI = SECONDPARAM;

	for(;;)
	{
		$lodsb
		IF(AL == 0) BREAK;
		IF(AL != '%') _putch(AL);
		ELSE
		{	
			$lodsb
			SWITCH(AL)
			{
			CASE '%':
				_putch(AL);
				BREAK;
			CASE 's':
				$push si
				SI = SSWORD[BP + DI];
				DI += 2;
				for(;;)
				{
					$lodsb
					IF(AL == 0) BREAK;
					_putch(AL);
				}
				$pop si
				BREAK;
			}
		}
	}
}