3.8 Register Type Coercion


3.8 Register Type Coercion

You can also cast a register to a specific type using the type coercion operator. By default, the 8-bit registers are of type byte, the 16-bit registers are of type word, and the 32-bit registers are of type dword. With type coercion, you can cast a register as a different type as long as the size of the new type agrees with the size of the register. This is an important restriction that does not exist when applying type coercion to a memory variable.

Most of the time you do not need to coerce a register to a different type. As byte, word, and dword objects, registers are already compatible with all one, two, and four byte objects. However, there are a few instances where register type coercion is handy, if not downright necessary. Two examples include boolean expressions in HLA high level language statements (e.g., if and while) and register I/O in the stdout.put and stdin.get (and related) statements.

In boolean expressions, HLA always treats byte, word, and dword objects as unsigned values. Therefore, without type coercion, the following if statement always evaluates false (because there is no unsigned value less than zero):

 if( eax < 0 )      stdout.put( "EAX is negative!", nl ); endif; 

You can overcome this limitation by casting EAX as an int32 value:

 if( (type int32 eax) < 0 ) then      stdout.put( "EAX is negative!", nl ); endif; 

In a similar vein, the HLA Standard Library stdout.put routine always outputs byte, word, and dword values as hexadecimal numbers. Therefore, if you attempt to print a register, the stdout.put routine will print it as a hex value. If you would like to print the value as some other type, you can use register type coercion to achieve this:

 stdout.put( "AL printed as a char = '", (type char al), "'", nl ); 

The same is true for the stdin.get routine. It will always read a hexadecimal value for a register unless you coerce its type to something other than byte, word, or dword.




The Art of Assembly Language
The Art of Assembly Language
ISBN: 1593272073
EAN: 2147483647
Year: 2005
Pages: 246
Authors: Randall Hyde

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net