|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
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!
__________________
~James [Not currently seeking freelance work] Like philosophy or interested in spirituality? Philosophorum. Game Dev Experts Forums Foresight Linux - Because your desktop should be cool! Linux FAQ FedoraFAQ UbuntuGuide |
|
#3
|
|||
|
|||
|
So it's as simple as multiplying the first by 16 and adding on the second? Thanks mate, that'll help
![]() |
|
#4
|
||||
|
||||
|
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 Puzzle of the Month solved by sizeablegrin, etienne141 and L7Sqr, superior C/C++ programmers of the month |
|
#5
|
|||
|
|||
|
Cool, thanks guys
![]() |
|
#6
|
||||
|
||||
|
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 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 Last edited by Schol-R-LEA : May 8th, 2006 at 08:22 PM. |
|
#7
|
||||
|
||||
|
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 |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Other Programming Languages > Assembly: convert hex to decimal |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|