Many people confuse numbers and their representation. A common question beginning assembly language students ask is, "I have 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 1 followed by seven 0s.
By default, HLA displays all byte
, word
, dword
, qword
, and lword
variables using the hexadecimal numbering system when using the stdout.put
routine. Likewise, HLA's stdout.put
routine will display all register values in hexadecimal form. Consider the program in Example 2-1, which converts values input as decimal numbers to their hexadecimal equivalents.
Example 2-1. Decimal-to-hexadecimal conversion program
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;
In a similar fashion, the default input base is also hexadecimal for registers and byte
, word
, dword
, qword
, or lword
variables. The program in Example 2-2 is the converse of the one in Example 2-1; it inputs a hexadecimal value and outputs it as decimal.
Example 2-2. Hexadecimal-to-decimal conversion program
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;
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 versus 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.puti16
and stdout.puti32
routines provide the same capabilities for 16-bit and 32-bit objects. The program in Example 2-3 demonstrates the decimal conversion program (Example 2-2) using only the EBX register (that is, it does not use the variable iValue
).
Example 2-3. Variable-less hexadecimal-to-decimal converter
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.puti32( ebx ); stdout.newln(); end ConvertToDecimal2;
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.geti16
, stdin.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.puth8
, stdout.puth16
, stdout.puth32
, stdout.puth64
, stdout.puth128
, stdin.geth8
, stdin.geth16
, stdin.geth32
, stdin.geth64
, or stdin.geth128
routines. The stdout.puth8
, stdout.puth16
, stdout.puth32
, stdout.puth64
, and stdout.puth128
routines write 8-bit, 16-bit, 32-bit, 64-bit, or 128-bit objects as hexadecimal values. The stdin.geth8
, stdin.geth16
, stdin.geth32
, stdin.geth64
, and stdin.geth128
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 Example 2-4 demonstrates the use of a few of these routines:
Example 2-4. Demonstration of stdin.geth32
and stdout.puth32
program HexIO; #include( "stdlib.hhf" ) static i32: int32; begin HexIO; stdout.put( "Enter a hexadecimal value: " ); stdin.geth32(); mov( eax, i32 ); stdout.put( "The value you entered was $" ); stdout.puth32( i32 ); stdout.newln(); end HexIO;