qa.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
   Example  Back  Contents  Index
──────────────────────────────────────────────────────────────────────────────
 
;* 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
;*          str - Pointer to 6-byte string
;*
;* Return:  None
 
        .DATA
hex     DB      '0123456789ABCDEF'      ; String of hex numbers
        .CODE
 
BinToHex PROC \
        USES di, \
        num:WORD, str:PTR BYTE
 
        LoadPtr es, di, str             ; 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
 
loop1:  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, 00001111b           ; Mask out high nibble
        xlat                            ; Get equivalent ASCII number in AL
        stosb                           ; Copy to 6-byte string, increment DI
        pop     ax                      ; Recover number
        loop    loop1                   ; Do next byte
        mov     ax, 'h'                 ; Put null, 'h' radix in AX
        stosw                           ; Copy to last two bytes in string
        ret
 
BinToHex ENDP
                                    -♦-