The MASM Forum Archive 2004 to 2012

Project Support Forums => HLA Forum => Topic started by: Emil_halim on November 19, 2010, 02:20:02 PM

Title: wsprintf hla declaration
Post by: Emil_halim on November 19, 2010, 02:20:02 PM
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 ?
Title: Re: wsprintf hla declaration
Post by: Sevag.K on November 19, 2010, 09:00:16 PM

what does "..." do?
Title: Re: wsprintf hla declaration
Post by: Emil_halim on November 20, 2010, 05:52:05 AM
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?
Title: Re: wsprintf hla declaration
Post by: Sevag.K on November 20, 2010, 08:46:41 AM

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.
Title: Re: wsprintf hla declaration
Post by: Emil_halim on November 20, 2010, 04:27:10 PM
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 ?
Title: Re: wsprintf hla declaration
Post by: Sevag.K on November 20, 2010, 06:11:02 PM

as far as i know, you can only do it with macros.
Title: Re: wsprintf hla declaration
Post by: Emil_halim on November 20, 2010, 06:30:51 PM

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.
Title: Re: wsprintf hla declaration
Post by: Emil_halim on November 20, 2010, 07:35:41 PM

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;

Title: Re: wsprintf hla declaration
Post by: Sevag.K on November 20, 2010, 09:00:57 PM

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.
Title: Re: wsprintf hla declaration
Post by: Emil_halim on November 23, 2010, 05:23:48 PM
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.
Title: Re: wsprintf hla declaration
Post by: Emil_halim on November 23, 2010, 06:22:43 PM

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;