
Assembly: convert hex to decimal
Hi,
Can someone tell me how to convert a 2 digit HEX value to it's decimal equivalent in Assembly please? Or just the theory to do it and I can implement it. It's for the Motorolla 68K processor.
Thanks

Theory only, we don't do the work for you
obviously hex has 16 values, 0  15, a  f = 10 through 15, i'm sure you konw that much. now, the way it goes to a number is like so
you have your number, lets take 'ab'
a, being the first digit gets multiplied by 16, and b is added on.
There you have your number
You will need your operating system's system call numbers to look at to get input and output. You could instead use the C library on your computer to use functions like printf and scanf (although i'm not sure if your lecturers will allow that)
Good Luck!

So it's as simple as multiplying the first by 16 and adding on the second? Thanks mate, that'll help

Yep. Theory works like this:
* Consider a number (say 15482) in decimal system
* This is equivalent to (1 * 10000) + (5 * 1000) + (4 * 100) + (8 * 10) + 2
* This is equivalent to (1 * 10^4) + (5 * 10^3) + (4 * 10^2) + (8 * 10^1) + (2 * 10^0)
where ^ is the powerof operator.
* Now consider a number (say 12F3B) in hexadecimal system
* Using similar logic, this is equivalent to (1 * 16^4) + (2 * 16^3) + (F * 16^2) + (3 * 16^1) + (B * 16^0)
Now all you have to do is compute 16^4, 16^3 etc. and also take F=15 and B=11 and there you have it... the answer in decimal
Note that you can use this logic to convert a number from *ANY* base into decimal.
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!"  Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne
"I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website."  Nilpo

Cool, thanks guys

I would add that if you needed a more general version of this  one that could handle values that weren't a fixed size  it would probably be easier and more efficient to convert the hex string to a binary integer, and then back to a decimal string, than to write a function specifically for converting hex string to a decimal string  especially since you would need an ASCII to binary integer and binary to ASCII routines anyway, and it's fairly easy to write ones that will handle all bases from 2 to 36.
The general algorithm for converting from binary to a printable ASCII or UNICODE string in a given base is (in Python):
Code:
def int2str(n, base):
digit = n % base
if base >= 10 and digit >= 10:
numeral = chr((digit  10) + ord('A'))
else:
numeral = chr(digit + ord('0'))
if base > n:
return numeral
else:
return int2str(n / base, base) + numeral
While this is a recursive algorithm, it is fairly easy to convert it to interation by pushing the resulting digits on the stack and looping, then popping them off when it is done; in assembly language, it amounts to about the same thing either way, really.
The stringtobinary algorithm is more or less the same as the one which LP gave (again, in Python):
Code:
from string import upper
def str2int(s, base):
accum = 0
# ASCII/UNICODE values of these characters
zero = ord('0')
nine = ord('9')
alpha = ord('A')
omega = ord('Z')
for n in range(len(s)):
numeral = ord(upper(s[(n+1)]))
if numeral in range(zero, nine):
accum += (numeral  zero) * (base ** n)
elif (numeral in range(alpha, omega)) and (base > 10):
accum += ((numeral  alpha) + 10) * (base ** n)
else:
# invalid number
accum = 0
break
return accum
Converting these algorithms into m68K assembly is, of course, left as an excercise.
Last edited by ScholRLEA; May 8th, 2006 at 09:22 PM.

I'd try ScholR's method, the code is certainly cleaner, and more flexible too.
Remember that hex is base 16, so to pass that to the function once you've written it
April 3rd, 2010, 02:31 PM

to make sure I'm doing it right, I use a hex to decimal converter like this one stringfunction.com/hexdecimal.html
David