July 15, 2024, 08:02:01 PM


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.


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

%line 2
global printf
extern AllocHeap
extern sprintf
extern _astrlen
extern $WRITELN
extern FreeHeap


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 FreeHeap       ; FreeHeap(buffer)

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

segment .bss

$espsave resd 1
$lpsz    resd 1

segment .text


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()
   printf("\n\n it works :)\n\n");