March 29, 2024, 03:06:03 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


printf for gui

Started by sapero, January 11, 2006, 08:55:56 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

sapero

crt printf imported function is great, but works propertly only if your program is compilled for console.
Here is a PrintfHack: create global function named printf and redirect all parameters to sprintf

sprintf needs a memory buffer for result, so we allocate small heap 2KB

printf.src#asm
%line 2
global printf
extern AllocHeap
extern sprintf
extern _astrlen
extern $WRITELN
extern FreeHeap

printf:

mov  eax,[esp]
mov  [$espsave],eax ; save return address

push dword 2048
call AllocHeap      ; buffer = AllocHeap(2048)
mov  [$lpsz],eax
mov  [esp],eax      ; virtual push buffer
call sprintf        ; printf changed to sprintf :)

push dword[$lpsz]   ; lpBuffer
push dword[$lpsz]   ; lpBuffer
call $WRITELN
call FreeHeap       ; FreeHeap(buffer)

mov  eax,[$espsave]
mov  [esp],eax
ret

segment .bss

$espsave resd 1
$lpsz    resd 1

segment .text


#endasm


Let's try:
extern int printf(...);
extern int system(...);

// compile console, run
// compile windows, run
// compile windows as project with printf.src
global sub main()
{
   OpenConsole();
   printf("\n\n it works :)\n\n");
   system("pause");
}