Home Back to 370 Assembler Tips & Tricks Index Links

Simple Displayable Hex to Binary Conversion

I recently had the misfortune of reviewing the most dreadful piece of code to convert character (displayable) hexadecimal to binary.  This tortured routine used all manner of 'or', 'shift' and other instructions.

Much like my other example of converting binary to displayable hexadecimal, the opposite conversion can also be accomplished with only two instructions.

Here's a code snippet illustrating the technique using and TR, PACK and TRT to validate the hexadecimal data:

 


           .
           .
           .
         MVC   HEXDIS,ANYDATA      Move some data to convert
*                                  to HEXDIS.
*
         LA    R2,8                Initialize return code.
         TRT   HEXDIS,HEXTRT       Validate the displayable hex.
         LTR   R15,R2              Set the return code.
*
*        RC = X'00' If all characters are valid hex.
*        RC = X'08' If any character is not valid hex.
*
         BNZR  R14                 Return to caller if invalid.
*
HEXOK    DS    0H
         TR    HEXDIS,HEXBTR       Translate F0-C6 to 00-0F.
*
         PACK  HEXBIN(L'HEXBIN+1),HEXDIS(L'HEXDIS+1)
*
*        HEXBIN now contains the binary representation
*        of HEXDIS.
*
         BR    R14                 Return to caller.
           .
           .
           .
         CNOP  0,4                 Fullword alignment.
HEXTRT   EQU   *                   256 byte hex validation table.
         SPACE ,
         DC    XL16'08080808080808080808080808080808'  00 - 0F
         DC    XL16'08080808080808080808080808080808'  10 - 1F
         DC    XL16'08080808080808080808080808080808'  20 - 2F
         DC    XL16'08080808080808080808080808080808'  30 - 3F
         DC    XL16'08080808080808080808080808080808'  40 - 4F
         DC    XL16'08080808080808080808080808080808'  50 - 5F
         DC    XL16'08080808080808080808080808080808'  60 - 6F
         DC    XL16'08080808080808080808080808080808'  70 - 7F
         DC    XL16'08080808080808080808080808080808'  80 - 8F
         DC    XL16'08080808080808080808080808080808'  90 - 9F
         DC    XL16'08080808080808080808080808080808'  A0 - AF
         DC    XL16'08080808080808080808080808080808'  B0 - BF
* Displayable          A B C D E F
         DC    XL16'08000000000000080808080808080808'  C0 - CF
         DC    XL16'08080808080808080808080808080808'  D0 - DF
         DC    XL16'08080808080808080808080808080808'  E0 - EF
* Displayable        0 1 2 3 4 5 6 7 8 9
         DC    XL16'00000000000000000000080808080808'  F0 - FF
         SPACE ,
HEXBTR   EQU   *                   256 byte hex conversion table.
         SPACE ,
         DC    XL16'00000000000000000000000000000000'  00 - 0F
         DC    XL16'00000000000000000000000000000000'  10 - 1F
         DC    XL16'00000000000000000000000000000000'  20 - 2F
         DC    XL16'00000000000000000000000000000000'  30 - 3F
         DC    XL16'00000000000000000000000000000000'  40 - 4F
         DC    XL16'00000000000000000000000000000000'  50 - 5F
         DC    XL16'00000000000000000000000000000000'  60 - 6F
         DC    XL16'00000000000000000000000000000000'  70 - 7F
         DC    XL16'00000000000000000000000000000000'  80 - 8F
         DC    XL16'00000000000000000000000000000000'  90 - 9F
         DC    XL16'00000000000000000000000000000000'  A0 - AF
         DC    XL16'00000000000000000000000000000000'  B0 - BF
* Binary               A B C D E F
         DC    XL16'000A0B0C0D0E0F000000000000000000'  C0 - CF
         DC    XL16'00000000000000000000000000000000'  D0 - DF
         DC    XL16'00000000000000000000000000000000'  E0 - EF
* Binary             0 1 2 3 4 5 6 7 8 9
         DC    XL16'00010203040506070809000000000000'  F0 - FF
           .
           .
           .
HEXBIN   DS    XL4                 4 Byte Binary Field.
         DS    X                   1 Byte Pad for PACK.
HEXDIS   DS    CL8                 8 Byte Displayable Hex Field.
         DS    C                   1 Byte Pad for PACK.
           .
           .
           .

In the above example, we convert an eight byte displayable hexadecimal field into a four byte binary field.

Note the extra byte after the binary and displayable hexadecimal fields.  I typically prefer to convert a word at a time, but you could convert any number of bytes up to 15 at one time.  My recommendation is to stick to converting words (4 bytes) or double-words (8 bytes) at a time.

I will continue to add tips and techniques as time permits.

Please direct any inquiries or problems regarding this web to webmaster@marcsweb.com


Page design, composition and HTML by Marc Niegowski
Copyright © 1998-2012, Marc Niegowski - Connectivity, Inc., All Rights Reserved
23 W. Fourth Street • Media • Pennsylvania • 19063-2805 • USA
Phone: 610-566-0227 • Fax: 610-566-0641 • Email: Marc@Tech-Center.com

Revision Date: Wednesday, November 15, 2006 10:03:39 AM