How to Convert Decimal Input to Hex in Assembly Language?

AI Thread Summary
To convert decimal input to hexadecimal in assembly language, the user's input must first be parsed correctly, typically as two ASCII bytes. For example, the decimal number 45 can be converted by multiplying the tens digit (4) by 10 and adding the units digit (5), resulting in 2Dh in hexadecimal. If the input is packed into one byte, additional masking and shifting are required before performing the conversion. The discussion emphasizes the need for a sequence to handle these conversions for various double-digit decimal inputs. Proper implementation of this method is left as an exercise for the user.
kloong
Messages
35
Reaction score
0
Assembly language. how to change the 80h in my register to 50h?
that goes to others to. like 45h to 2Dh.

basically, i came up with a sequence to change the user's double digit decimal input to hex value.

i.e.
when i enter 45, AL = 45h
how do i change it to 2Dh??

; al = 45h. how to change it to 2Dh?


45(decimal) = 2Dh.
must be able to do this for other conversion.
 
Physics news on Phys.org
kloong said:
Assembly language. how to change the 80h in my register to 50h?
that goes to others to. like 45h to 2Dh.

basically, i came up with a sequence to change the user's double digit decimal input to hex value.

i.e.
when i enter 45, AL = 45h
how do i change it to 2Dh??

; al = 45h. how to change it to 2Dh?


45(decimal) = 2Dh.
must be able to do this for other conversion.

I'm not quite sure if I completely understand your question, but wouldn't the user's input in this case be two ASCII bytes? (The ASCII characters for '4'--0x34, and '5'--0x35) Unless you've already parsed these by ANDing with 0xf0)

If that's the case, you can simply multiply the first byte by 10, and add it to the second byte. Simple base 10 long form, as you probably did in elementary. In this case, 4 x 10 is 40 is 0x28 and when you add 5 to it, you get 0x2d, as in your example. If instead, it's packed into one byte, you'll need to do some masking and shifting before multiplying the 10s digit by 10 and adding it to the 1s digit.

The actual implementation is left as a task to the user.
 

Similar threads

Back
Top