Assembly Language Help (alang.hlp) (Table of Contents; Topic list)
Important Notice
The pages on this site contain documentation for very old MS-DOS software, purely for historical purposes. If you're looking for up-to-date documentation, particularly for programming, you should not rely on the information found here, as it will be woefully out of date.
BinToHex
 Map                                       Up Contents Index Back
────────────────────────────────────────────────────────────────────────────
 
;* BinToHex - Converts binary word to 6-byte hexadecimal number in
;* ASCIIZ string. String is right-justified and includes "h" radix.
;*
;* Shows:   Instruction - xlat
;*
;* Params:  Num - Number to convert to hex string
;*          Sptr - Pointer to 6-byte string
;*
;* Return:  None
 
        .DATA
hex     BYTE    "0123456789ABCDEF"      ; String of hex numbers
 
        .CODE
BinToHex PROC USES di,
        Num:WORD, Sptr:PBYTE
 
        LoadPtr es, di, Sptr            ; Point ES:DI to 6-byte string
        mov     bx, OFFSET hex          ; Point DS:BX to hex numbers
        mov     ax, Num                 ; Number in AX
        mov     cx, 2                   ; Loop twice for two bytes
 
        .REPEAT
        xchg    ah, al                  ; Switch bytes
        push    ax                      ; Save number
        shr     al, 1                   ; Shift high nibble to low
        shr     al, 1
        shr     al, 1
        shr     al, 1
        xlat                            ; Get equivalent ASCII number in AL
        stosb                           ; Copy to 6-byte string, increment DI
        pop     ax                      ; Recover number
        push    ax                      ; Save it again
        and     al, 00001111y           ; Mask out high nibble
        xlat                            ; Get equivalent ASCII number in AL
        stosb                           ; Copy to 6-byte string, increment DI
        pop     ax                      ; Recover number
        .UNTILCXZ                       ; Do next byte
        mov     ax, 'h'                 ; Put null, 'h' radix in AX
        stosw                           ; Copy to last two bytes in string
        ret
 
BinToHex ENDP
                                    -♦-