Hey I'm currently taking assembly language and I got stuck for understanding the conversion routine any Hex/bi/oct number to decimal
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;
This is the code provided in textbook and I'm quite confused in which part and how does a hex number convert to decimal number
Also
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;
I wonder how this one converts as well. What I thought was input a hex number, but where it gets converted to decimal number?
So... you would normally expect a ConvertToHex function to look something like this pseudocode:
PrintAsHex(val) {
do {
digit = val mod 16; // get the rightmost digit (in Hex)
if digit < 10 print digit; // print 0-9
else print val('A' + (digit-10)) // print A-F
val = val / 16; // shift down next digit
} while (val > 0)
}
Actual assembly for this would use shifts and stuff, but in any case they don't show you that routine! Instead, they tell you about the higher level routines that do the the "conversion" for IO (input/output) calling those (unshown) lower level functions.
They make two important points in the book:
By default, HLA displays all byte, word, and dword variables using the hexadecimal numbering system when you use the stdout.put routine. Likewise, HLA's stdout.put routine will display all register values in hex
That's why stdout.put( "The value ", value, " converted to hex is $", eax, nl ); prints the value in the eax register as Hex format. 'nl' is just a newline.
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
That's why stdout.puti32( ebx ) prints as ebx in Decimal.
One final note - though different output functions are provided depending on which format you want to display a number, the input fuction is the same: stdin.get reads them all, but you have to include a specifier in the input if it's not Decimal.
As noted in the book: