Author Topic: Convert Hex digits to binary  (Read 17522 times)

nobody

  • Guest
Convert Hex digits to binary
« on: May 29, 2007, 02:07:26 PM »
I am very new to nasm. Please provide an in-depth explanation.

I have a string consisting of 2 hex digits. I have to convert each hex digit to binary and store the 1st digit in 4 most significant bits of AL and store 2nd digit in 4 least-significant bits of AL,

e.g. string = B7
AL should contain 1011 0111

Please explain how i should go about doing this.

Thanks

nobody

  • Guest
Re: Convert Hex digits to binary
« Reply #1 on: May 30, 2007, 03:40:28 PM »
For each of the two digits, grab the character.
If it's [0-9], then stick that number into the result.
If it's [A-Fa-f], then "subtract 'A' or 'a'", and add 10.

Also, read up on the AAA/DAA/AAS/DAS/AAM/AAD instructions.
Note that they can no longer be used in 64-bit mode.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Convert Hex digits to binary
« Reply #2 on: May 30, 2007, 06:24:57 PM »
Minor addition: after grabbing a single byte from the string, and adjusting for possible a-f, A-F, we still need to subtract '0' (30h, 48 decimal) to convert from a "character" to a "number".

For a more "general" method that will work for more than 2 digits...

grab a digit (character)
confirm that it's "valid"
convert from "character" to "number"
shift the "result so far" left by 4
"or" in the newly converted number

If you can't get it, show us what you're trying so far.

Best,
Frank