News:

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

Compiling same code with different data types

Started by John Kiro, July 28, 2007, 04:23:47 PM

Previous topic - Next topic

John Kiro

Hello There!

I'm back again to the MASM forum after 2 years of absense (& hence, account deletion) :8)

I'm currently translating some old work from MASM32 to a portable assembler.. I started by trying NASM, then FASM, but as both lack type definition, and some features as sizeof operator (needed to make my code as clean as possible), I moved to HLA.

I've had a previous experience with HLA, but I've quit quickly, due to some issues I didn't like too much about HLA (such as the increased typing required), but this is not a big problem to me now, as I'm translating code from MASM to HLA almost automatically using vi (vim actually).

My question here is about using iteration on data types; I tried something like:


type signal16: int16;
type signal32: int32;
...
#for (SIGNAL in [signal16, signal32])
<generate code for each data type by using SIGNAL as alias>
#endfor


But this failed because signal16 and signal32 are datatypes not constants  :(

So what is the most straightforward way to achieve this iteration?

Thanks,
John

Evenbit

You are using the wrong keywords.. plus, you need to define the iterator first before you can use it.  Read this section of Art of Assembly:  http://webster.cs.ucr.edu/AoA/Windows/HTML/IntermediateProceduresa5.html#1004783

Here is the example given of how to define an iterator:
iterator range( start:uns32;  last:uns32 ); nodisplay;

begin range;



mov( start, eax );

while( eax <= last ) do



push( eax );

yield();

pop( eax );

inc( eax );



endwhile;



end range;

For an example of code which 'calls' this iterator... it would look something like this:
foreach range( 1, 10 ) do



stdout.put( "Iteration = ", (type uns32 eax), nl );



endfor;


Nathan.

John Kiro

Hi Evenbit

I meant a compile-time repetition, and on several data types, not several constants. (In the example I gave, signal16 & signal32 are datatypes, not constants).

John

Randall Hyde

Quote from: John Kiro on July 28, 2007, 04:23:47 PM
Hello There!

I'm back again to the MASM forum after 2 years of absense (& hence, account deletion) :8)

I'm currently translating some old work from MASM32 to a portable assembler.. I started by trying NASM, then FASM, but as both lack type definition, and some features as sizeof operator (needed to make my code as clean as possible), I moved to HLA.

I've had a previous experience with HLA, but I've quit quickly, due to some issues I didn't like too much about HLA (such as the increased typing required), but this is not a big problem to me now, as I'm translating code from MASM to HLA almost automatically using vi (vim actually).

My question here is about using iteration on data types; I tried something like:


type signal16: int16;
type signal32: int32;
...
#for (SIGNAL in [signal16, signal32])
<generate code for each data type by using SIGNAL as alias>
#endfor


But this failed because signal16 and signal32 are datatypes not constants  :(

So what is the most straightforward way to achieve this iteration?

Thanks,
John


Well, what you can do is convert the type names to strings and work with strings.  E.g.,


type signal16: int16;
type signal32: int32;
...
#for (SIGNAL in [@typename(signal16), @typename(signal32)])
<generate code for each data type by using @text(SIGNAL) as alias>
#endfor


Of course, you could have just typed "signal16" and "signal32" as the array constant operands, but using @typename is more general, should you want to refer to those types indirectly using text equates or macros.

If you actually have a longer list of typenames you want to work with, you might consider creating a macro that will build the type declarations and the array of type names all in one operation, e.g.,

#macro signalTypes( sizeParms[] ):i;
    #for( i := 0 to @elements( sizeParms )-1)
         @text( "signal" + parms[i] ) : @text( "int" + parms[i]);
        #if( @defined( signalList )
             ?signalList := [ signalList, "signal" + parms[i] );
        #else
            ?signalList := [parms[i];
        #endif
    #endfor
#endmacro
  .
  .
  .
type
   signalTypes( 16, 32, 64 );
   #for( sigType in signalList )
       // use @text( sigType ) whenever you want the type
   #endfor

Cheers,
Randy Hyde



John Kiro

Hello Dr. Randall

Thanks for your answer. For now at least, I'll go for the first solution, as it's simpler for me.
But I'm now having a problem with @size, as I want to use it in scaled addressing mode. Here is the example I use:


program types;

#include( "stdlib.hhf" )

type signal16: int16;
type signal32: int32;

begin types;

#for(SIG in [@typename(signal16),@typename(signal32)])
        ?SIGSIZE:=@size(@text(SIG));
//      ?SIGSIZE2:=@text(@string:SIGSIZE);   //doesn't work either
        #print("Current signal type is ", SIG);
        #print("Current signal size is ", SIGSIZE);
        mov(ecx,[edx+ecx*SIGSIZE]);    //ERROR!
        //mov(ecx,[edx+ecx*2]);
#endfor

end types;


Here is the output during assembly:


hla -c  types.hla
Current signal type is int16
Current signal size is 2
Error in file "types.hla" at line 19 [errid:107619/hlaparse.c]:
syntax error, unexpected LocalConstID, expecting intconst.
Near: << SIGSIZE >>

types.hla [70]:
error: undefined symbol.
make: *** [types.o] Error 1


So what can I be missing here?

Thanks,
John

Randall Hyde

I'll have to look at the compiler source code, but I suspect the problem is that scaled indexed addressing doesn't allow an arbitrary expression, just a literal numeric constant.  Here's a quick work-around that should solve the problem:


#macro x(i);
  @text( @string(i))
#endmacro
  .
  .
  .
mov( eax, [edx+ecx*x(expression)] );


I'll look into allowing generalized expressions in the next release. I thought I'd done this a while back, but perhaps I'm wrong.
hLater,
Randy Hyde

John Kiro

Thanks Dr. Randall

And may be in a point in the future, HLA could contain some sort of templates, as in some HLL?...

Regards,
John


Evenbit

Quote from: John Kiro on July 31, 2007, 06:59:29 PM
And may be in a point in the future, HLA could contain some sort of templates, as in some HLL?...
Do you mean something like a STL?  Check the Files section at this Yahoo Group:

http://tech.groups.yahoo.com/group/aoaprogramming/

Nathan.

John Kiro

Hi Evenbit

I didn't check STL yet.. but I doubt that HLA currently supports templates that could be useful in my case.

Thanks anyway.
John

Randall Hyde

Quote from: John Kiro on July 31, 2007, 06:59:29 PM
Thanks Dr. Randall

And may be in a point in the future, HLA could contain some sort of templates, as in some HLL?...

Regards,
John


To a large extent, "templates" is just the HLL name for structured macros.
I've actually done quite a bit with "templates" for HLA, having developed the HLA standard template library (along with help from Berndt). It pretty much simulates everything the C++ standard template library does.

I've also created (recently) a macro that lets you easily overload procedures and macros. That macro will appear in the next release of the HLA stdlib v2.x (Webster is being moved to a new machine and I've waiting for the system administrators to add the software to the new system that will let me upload stuff).
hLater,
Randy Hyde