News:

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

wsprintf hla declaration

Started by Emil_halim, November 19, 2010, 02:20:02 PM

Previous topic - Next topic

Emil_halim

hi all

what is the hla equivalent to this masm proto

wsprintf PROTO C buffer:DWORD,format:DWORD,args:VARARG


i.e how to declare c\c++ parameter "..."  in hla ?


Emil_halim

simply ... means uncompleted  parameter declaration.

you put this three dots in the place of arguments to tell the compiler that the function will
acepet unknown coming parameters.

just like the VARARG in masm.

so how to do it in hla?

Sevag.K


hla uses the put macro for such purposes.

stdout.put( args[] ); // to stdout
fileio.put( hfile, argsp[ ); // to a file
str.put( string, args[] );  // to string buffer
sock.put( args[] ); // for writing to sockets
etc.

no put statement exists for arbitrary char array buffers, but it's easy to write your own put macro, look up hla.put in hla.hhf along with the various put macros in fileio.hhf, strings.hhf and stdout.hhf

there is also the blob option which comes in handy.  this has a put statement as well, but you have to work with a blob structure.

and i have written the tBufferBase/tBuf/tBuffer classes in hidelib and the kEdit class from the kEdit project which have various buffer control functions, but these options are only worth it for large buffers... when you need to do combining, inserting, cutting, dynamic growing, etc.
this one you also can extent on the tBufferBase class if you want a base class for a dynamic growing buffer.

Emil_halim

ok , using macro with array arg '[]' will do it.

but i want to make that for a procedure , i have a like printf function in my dll that i am working in.

so is there a way in hla ?

Sevag.K


as far as i know, you can only do it with macros.

Emil_halim


so , i have two choice to make it.

1- using emit to declare it in masm .

2- compile my function with c/c++ compiler to a coff file then link it to my dll.

i will try the first.

Emil_halim


need help please .

i am trying to push the parameters in reverse order by fro-endfor loop and push instruction , then using #asm-#endasm to call the
printf function in masm pass.

but it refuses to compile .

program printf;

#include("stdlib.hhf");

#emit(" printf PROTO C format:DWORD,args:VARARG ");
#macro _printf( arga[] );
       #for( i := @elements( arga )-1 downto 0 )
          push ( @text( arga[i] ));
       #endfor
       #asm
            call  printf
       #endasm     
#endmacro
     
begin printf;

   stdout.put("using c printf function with hla" nl);
   
   _printf("i = %d" , 10);
     
end printf;


Sevag.K


there is always a bit of difficulty in porting a printf macro to assemblers.

you can't push a string constant onto the stack.  i don't thing there is any mechanism for doing this so you may have to use static strings and push the address of those instead.

Emil_halim

ok , i have tried it with the following code but has one error

it gives 'Expected an array expression' Near: << ) >>

here is the code ,so any help ?

program printf;

#include("stdlib.hhf");

#emit(" printf PROTO C format:DWORD,args:VARARG ");
#macro _printf( arga[] );
       ?aCnt:uns32 := 0;
       ?_arglist_ :string;
       ?_arglist_ := "$invoke printf ";
       #while ( aCnt < @elements( args ) )               //     <--- this line gives above error
   ?_arglist_ := _arglist_ + "," + args[aCnt];
   ?aCnt := aCnt + 1;
   #endwhile     
       #emit(_arglist_);     
#endmacro
     
begin printf;

   stdout.put("using c printf function with hla" nl);
   
   _printf( "i = %d" , 10 );
     
end printf;



edited:

sorry it was mistype error.

Emil_halim


ok here is it , works fine , any suggestion or improvement to the macro are welcome.


program printf;

#include("stdlib.hhf");

#emit(" printf PROTO C format:DWORD,args:VARARG ");
#macro _printf( parm[] ):fromt;
       ?frmt:string;
       ?frmt := @string:fromt + " db " + parm[0] + ",0";
       #emit(".data");
       #emit(frmt);
       #emit(".code");             
       ?aCnt:uns32 := 1;
       ?_arglist_ :string;
       ?_arglist_ := "invoke printf ,ADDR " + @string:fromt;
       #while ( aCnt < @elements( parm ) )
   ?_arglist_ := _arglist_ + " , " + parm[aCnt];
   ?aCnt := aCnt + 1;
   #endwhile     
       #emit(_arglist_);     
#endmacro
     
begin printf;

   stdout.put("using c printf function with hla" nl);
   
   _printf( "i = %d" , 10 );
     
end printf;