TOC PREV NEXT INDEX

Put your logo here!


19 Type Coercion in HLA


While an assembly language can never really be a strongly typed language, HLA is much more strongly typed than most other assembly languages.

Strong typing in an assembly language can be very frustrating. Therefore, HLA makes certain concessions to prevent the type system from interfering with the typical assembly language programmer. Within an 80x86 machine instruction, the only checking that takes place is a verification that the sizes of the operands are compatible.

Despite HLA playing fast and loose with machine instructions, there are many times when you will need to coerce the type of some operand. HLA uses the following syntax to coerce the type of a memory location or register operand:


 
(type typeID  memOrRegOperand)
 

 

There are two instances where type coercion is especially important: (1) when you need to assign a type other than byte, word, or dword to a register1; (2) when you need to assign an anonymous memory location a type.

Type coercion is very useful in HLA when manipulating pointer objects, especially pointers to classes and records. Consider the following example:

type
 
	myRec_t: record
 
			i:int32;
 
			c:char;
 
	endrecord;
 

 
	mrPtr_t: pointer to myRec_t;
 

 
static
 
	mpr: mrPtr_t;
 
		.
 
		.
 
		.
 
	malloc( @size( myRec_t ) );
 
	mov( eax, mpr );
 
		.
 
		.
 
		.
 
	mov( mpr, ebx );
 
	mov( cl, (type myRec_t [ebx]).c );
 
	mov( 0, (type myRec_t [ebx]).i );
 

 

As you can see here, whatever memory address appears inside the parentheses is treated like an object of the specified type. So you can treat that whole entity as though it were a variable of the specified type (myRec_t in this example) and you can apply the dot operator or any other operation that would be legal on a variable of that type.

By default, the x86 general purpose registers have the types byte, word, or dword (depending, of course, on their size). Sometimes you might want to coerce these register to a different type, especially when outputting the value of a register or comparing a register with a constant. Coercion of a register is perfectly legal as long as the coerced data type is the same size as the register, e.g.,

	(type int32 eax)
 

 

A coercion like this last example is especially useful when using the register without an output statement (like stdout.put) or in a run-time boolean expression. Consider the following:

if( eax < 0 ) then
 
	<< do something if EAX is negative>>
 
endif;
 

 

In this example, the expression is always false because EAX is a dword object (which is unsigned). Therefore, EAX can never be less than zero (even if EAX contains something that you want interpreted as a negative value). You can solve this problem by coerce EAX to an INT32 object:

if( (type int32 eax) < 0 ) then
 
	<< do something if EAX is negative>>
 
endif;
 

 

This code example will work properly since HLA is smart enough to generate the appropriate signed comparison/conditional jump sequence when it realizes one or more of the operands are signed.

1Probably the most common case is treating a register as a signed integer in one of HLA's high level language statements. See the section on HLA High Level Language statements for more details.


TOC PREV NEXT INDEX