Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
May 8th, 2006, 11:41 AM
 n000b
Contributing User

Join Date: Aug 2003
Location: Melbourne, Australia
Posts: 595
Time spent in forums: 2 Days 12 h 47 m 6 sec
Reputation Power: 13
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

#2
May 8th, 2006, 12:53 PM
 LinuxPenguin
fork while true;

Join Date: May 2005
Location: England, UK
Posts: 5,538
Time spent in forums: 1 Month 3 Weeks 1 Day 19 h 30 m 28 sec
Reputation Power: 1050
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.

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!

#3
May 8th, 2006, 08:07 PM
 n000b
Contributing User

Join Date: Aug 2003
Location: Melbourne, Australia
Posts: 595
Time spent in forums: 2 Days 12 h 47 m 6 sec
Reputation Power: 13
So it's as simple as multiplying the first by 16 and adding on the second? Thanks mate, that'll help

#4
May 8th, 2006, 08:21 PM
 Scorpions4ever
Banned ;)

Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,536
Time spent in forums: 2 Months 3 Days 5 h 48 m 31 sec
Reputation Power: 4106
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 power-of 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

#5
May 8th, 2006, 08:28 PM
 n000b
Contributing User

Join Date: Aug 2003
Location: Melbourne, Australia
Posts: 595
Time spent in forums: 2 Days 12 h 47 m 6 sec
Reputation Power: 13
Cool, thanks guys

#6
May 8th, 2006, 09:18 PM
 Schol-R-LEA
Commie Mutant Traitor

Join Date: Jun 2004
Location: Norcross, GA (again)
Posts: 1,798
Time spent in forums: 1 Month 2 Weeks 3 Days 1 h 39 m 19 sec
Reputation Power: 1569
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 string-to-binary 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.
__________________
Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
#define KINSEY (rand() % 7) λ Scheme is the Red Pill
Scheme in ShortUnderstanding the C/C++ Preprocessor
Taming PythonA Highly Opinionated Review of Programming Languages for the Novice, v1.1

FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. \$4500 obo. tverres@et.ins.gov

Last edited by Schol-R-LEA : May 8th, 2006 at 09:22 PM.

#7
May 9th, 2006, 02:00 AM
 LinuxPenguin
fork while true;

Join Date: May 2005
Location: England, UK
Posts: 5,538
Time spent in forums: 1 Month 3 Weeks 1 Day 19 h 30 m 28 sec
Reputation Power: 1050
I'd try Schol-R'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

#8
April 3rd, 2010, 02:31 PM
 davitz38
Registered User

Join Date: Mar 2010
Posts: 2
Time spent in forums: 8 m 47 sec
Reputation Power: 0
to make sure I'm doing it right, I use a hex to decimal converter like this one stringfunction.com/hex-decimal.html
David

 Viewing: Dev Shed Forums > Programming Languages - More > Other Programming Languages > Assembly: convert hex to decimal