News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

fprintf

Started by mfazio, March 01, 2007, 07:15:56 AM

Previous topic - Next topic

mfazio

Im looking to implement the fprintf function in one of my applications, so that i might be able to format strings into my output file... How do I go about this with masm32?

simple E.G.

push    "hello %s"
push    edi
call       fprintf

OUTPUTS

hello <name>

ragdog

hi mfazio


try this I hope it help you!

.386
.model flat,stdcall
option casemap:none

   include windows.inc
   include user32.inc
   include kernel32.inc

   includelib user32.lib
   includelib kernel32.lib

.data
szFormat db "hello %s",0
szName   db "masm32",0
szCap    db "Information",0

.data?
hBuffer dd ?

.code
start:
    invoke wsprintf,addr hBuffer,addr szFormat,addr szName
    invoke MessageBox,0,addr hBuffer,addr szCap,MB_OK
   invoke ExitProcess,eax
end start

greets
ragdog

mfazio

although this solution is a good compromise, i would really like to see if it is possible to use the fprintf function.  i have seen talk about using mvscrt?

ragdog


why you want and cause this fprinf to use
I find it better and it´s simply with wsprinf api

MichaelW

Why wouldn't it be possible?

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    include \masm32\include\masm32rt.inc
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    .data
      stream dd 0
      integer dd 12345
      double  dq 1.2345 
      string  db "this is a string",0     
    .code
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
start:
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    mov stream, rv(crt_fopen,"fprintf.out","w")
    fn crt_fprintf,stream,"%s%c",ADDR string,10
    fn crt_fprintf,stream,"%d%c",integer,10
    fn crt_fprintf,stream,"%f%c",double,10
    fn crt_fclose,stream
    inkey "Press any key to exit..."
    exit
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
end start

eschew obfuscation

BogdanOntanu

Or you could use this simple sprintf()  implementation and roll your own in ASM style.

Take care:
1) it will not compile because it is in TASM syntax (you only have to change the function head)
2) it requires two "dword to ascii zecimal" and a "dword to ascii hexa" functions. Contains a hardcoded "8"
3) it is easy to extend with other formats and features

But you can easyly convert it to MASM and the dword to whatewer functions are well implemented in the MASM32 package.
So I leave it as an exercise to you... have fun ;)


;------------------------------
; simple sprintf() replacement
;------------------------------
Str_Printf PROC C
ARG @@dest_str_ptr:dword, @@format_str_ptr:dword, @@params:dword:?
USES ecx,edx,ebx,esi,edi

mov edi,[@@dest_str_ptr]
mov esi,[@@format_str_ptr]
lea ecx,@@params

@@parse_loop:
mov al,[esi]
inc esi

; check null terminator
test al,al
jz @@finish

; check format specificator
cmp al,"%"
jz @@is_format

;----------------------------------
; simply copy non format chars
;----------------------------------
@@copy_char:
mov [edi],al
inc edi
jmp @@parse_loop

@@is_format:
mov al,[esi]
inc esi

cmp al,"x"
jz @@put_nr_hex

cmp al,"s"
jz @@put_string

cmp al,"u"
jz @@put_nr_unsigned

cmp al,"%"
jz @@copy_char

;-----------------------------
; unknown specificator
;-----------------------------
mov al,"?"
mov [edi],al
inc edi
jmp @@finish

@@put_nr_hex:
pushad
mov eax,[ecx]
Call Dwtoa_Hex32 STDCALL, eax, edi
popad

; jump over number
add edi,8

; next parameter
add ecx,4
jmp @@parse_loop

@@put_string:
push esi

mov esi,[ecx]

@@loop_string:
mov al,[esi]
inc esi

test al,al
jz @@string_done

mov [edi],al
inc edi
jmp @@loop_string

@@string_done:

pop esi

add ecx,4
jmp @@parse_loop

@@put_nr_unsigned:

; get parameter
mov eax,[ecx]
CAll Dwtoa_Dec,edi,eax

add edi,eax

; next parameter
add ecx,4
jmp @@parse_loop

@@finish:
;----------------------------------------
; place null terminator at destination
;----------------------------------------
xor eax,eax
mov [edi],al

ret
ENDP


If you need it: I can post the code for Dwtoa_Hex32 and Dwtoa_Dec procedures... but they are trivial
Ambition is a lame excuse for the ones not brave enough to be lazy.
http://www.oby.ro