2.6 A Note About Numbers vs. Representation


2.6 A Note About Numbers vs. Representation

Many people confuse numbers and their representation. A common question beginning assembly language students ask is, "I've got a binary number in the EAX register; how do I convert that to a hexadecimal number in the EAX register?" The answer is, "You don't." Although a strong argument could be made that numbers in memory or in registers are represented in binary, it's best to view values in memory or in a register as abstract numeric quantities. Strings of symbols like 128, $80, or %1000_0000 are not different numbers; they are simply different representations for the same abstract quantity that we refer to as "one hundred twenty-eight." Inside the computer, a number is a number regardless of representation; the only time representation matters is when you input or output the value in a human readable form.

Human readable forms of numeric quantities are always strings of characters. To print the value 128 in human readable form, you must convert the numeric value 128 to the three-character sequence ‘1’ followed by ‘2’ followed by ‘8.’ This would provide the decimal representation of the numeric quantity. If you prefer, you could convert the numeric value 128 to the three-character sequence "$80." It's the same number, but we've converted it to a different sequence of characters because (presumably) we wanted to view the number using hexadecimal representation rather than decimal. Likewise, if we want to see the number in binary, then we must convert this numeric value to a string containing a one followed by seven zeros.

By default, HLA displays all byte, word, dword, qword, and lword variables using the hexadecimal numbering system when you display them using the stdout.put routine. Likewise, HLA's stdout.put routine will display all register values in hex. Consider the program in Listing 2-1 that converts values input as decimal numbers to their hexadecimal equivalents.

Listing 2-1: Decimal to Hexadecimal Conversion Program.

start example
 program ConvertToHex; #include( "stdlib.hhf" ) static     value: int32; begin ConvertToHex;     stdout.put( "Input a decimal value:" );     stdin.get( value );     mov( value, eax );     stdout.put( "The value ", value, " converted to hex is $", eax, nl ); end ConvertToHex; 
end example

In a similar fashion, the default input base is also hexadecimal for registers and byte, word, dword, qword, or lword variables. The program in Listing 2-2 is the converse of the one in Listing 2-1: It inputs a hexadecimal value and outputs it as decimal.

Listing 2-2: Hexadecimal to Decimal Conversion Program.

start example
 program ConvertToDecimal; #include( "stdlib.hhf" ) static     value: int32; begin ConvertToDecimal;     stdout.put( "Input a hexadecimal value: " );     stdin.get( ebx );     mov( ebx, value );     stdout.put( "The value $", ebx, " converted to decimal is ", value, nl ); end ConvertToDecimal; 
end example

Just because the HLA stdout.put routine chooses decimal as the default output base for int8, int16, and int32 variables doesn't mean that these variables hold "decimal" numbers. Remember, memory and registers hold numeric values, not hexadecimal or decimal values. The stdout.put routine converts these numeric values to strings and prints the resulting strings. The choice of hexadecimal vs. decimal output was a design choice in the HLA language, nothing more. You could very easily modify HLA so that it outputs registers and byte, word, dword, qword, or lword variables as decimal values rather than as hexadecimal. If you need to print the value of a register or byte, word, or dword variable as a decimal value, simply call one of the putiX routines to do this. The stdout.puti8 routine will output its parameter as an 8-bit signed integer. Any 8-bit parameter will work. So you could pass an 8-bit register, an int8 variable or a byte variable as the parameter to stdout.puti8 and the result will always be decimal. The stdout.put.geti16 and stdout.put.geti32 provide the same capabilities for 16-bit and 32-bit objects. The program in Listing 2-3 demonstrates the decimal conversion program (Listing 2-2) using only the EAX register (i.e., it does not use the variable iValue).

Listing 2-3: Variable-less Hexadecimal to Decimal Converter.

start example
 program ConvertToDecimal2; #include( "stdlib.hhf" ) begin ConvertToDecimal2;    stdout.put( "Input a hexadecimal value: " );    stdin.get( ebx );    stdout.put( "The value $", ebx, " converted to decimal is " );    stdout.put.geti32( ebx );    stdout.newln(); end ConvertToDecimal2; 
end example

Note that HLA's stdin.get routine uses the same default base for input as stdout.put uses for output. That is, if you attempt to read an int8, int16, or int32 variable, the default input base is decimal. If you attempt to read a register or byte, word, dword, qword, or lword variable, the default input base is hexadecimal. If you want to change the default input base to decimal when reading a register or a byte, word, dword, qword, or lword variable, then you can use stdin.geti8, stdin.get.geti16, stdin.get.geti32, stdin.geti64, or stdin.geti128.

If you want to go in the opposite direction, that is you want to input or output an int8, int16, int32, int64, or int128 variable as a hexadecimal value, you can call the stdout.putb, stdout.putw, stdout.putd, stdout.putq, stdout.putl stdin.getb, stdin.getw, stdin.getd, stdin.getq, or stdin.getl routines. The stdout.putb, stdout.putw stdout.putd, stdout.putq, and stdout.putl routines write 8-bit, 16-bit, 32-bit, 64-bit, or 128-bit objects as hexadecimal values. The stdin.getb, stdin.getw, stdin.getd, stdin.getq, and stdin.getl routines read 8-, 16-, 32-, 64-, and 128-bit values, respectively; they return their results in the AL, AX, or EAX registers (or in a parameter location for 64-bit and 128-bit values). The program in Listing 2-4 demonstrates the use of a few of these routines:

Listing 2-4: Demonstration of stdin.getd and stdout.putd.

start example
 program HexIO; #include( "stdlib.hhf" ) static     i32: int32; begin HexIO;     stdout.put( "Enter a hexadecimal value: " );     stdin.getd();     mov( eax, i32 );     stdout.put( "The value you entered was $" );     stdout.putd( i32 );     stdout.newln(); end HexIO; 
end example




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