#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2003
    Location
    Melbourne, Australia
    Posts
    595
    Rep Power
    14

    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. #2
  3. fork while true;
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2005
    Location
    England, UK
    Posts
    5,538
    Rep Power
    1051
    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!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2003
    Location
    Melbourne, Australia
    Posts
    595
    Rep Power
    14
    So it's as simple as multiplying the first by 16 and adding on the second? Thanks mate, that'll help
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    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

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2003
    Location
    Melbourne, Australia
    Posts
    595
    Rep Power
    14
  10. #6
  11. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Alpharetta, GA
    Posts
    1,806
    Rep Power
    1570
    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.
    Last edited by Schol-R-LEA; May 8th, 2006 at 08:22 PM.
    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 Short Understanding the C/C++ Preprocessor
    Taming Python A 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
  12. #7
  13. fork while true;
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2005
    Location
    England, UK
    Posts
    5,538
    Rep Power
    1051
    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
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2010
    Posts
    2
    Rep 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

IMN logo majestic logo threadwatch logo seochat tools logo