Print Hex routine

Print a number to the screen in hex format

PrtHex uses xlatb to look up a char in a table. BX = offset of the table while AL = the offset from the first
(0 as first). On return, AL = char of position requested.

So if BX = offset Hex and AL = the 2nd position, then AL will return a '1'
; Assemble with NBASM

.model tiny
.code
.186
           org  100h

           mov  ax,1234h
           call PrtHex
           mov  ax,4C00h
           int  21h

PrtHex     proc near uses ax bx cx

           mov  bx,offset Hex
           mov  cx,04h
HexLoop:   push  ax
           mov  al,ah
           shr  al,04h
           xlatb
           mov  dl,al
           mov  ah,02
           int  21h
           pop  ax
           shl  ax,04h
           loop HexLoop
           mov  ah,02
           mov  dl,'h'
           int  21h
           ret
PrtHex     endp

Hex        db  '0123456789ABCDEF'

.end

How about a smaller version!
(Thanks to The Bass Demon for even a smaller code snippet)

; Assemble with NBASM

.model tiny
.code
.186
           org  100h
           mov  ax,1234h
           call PrtHex
           ret

PrtHex     proc near
           mov  cx,04    ; if you know ch = 0 then mov cl,04 saves a byte
Loop1:     rol  ax,04
           push ax
           and  al,0Fh
           daa
           add  al,-16
           adc  al,+64
           int  29h
           pop  ax
           loop Loop1
          
           mov  al,'h'
           int  29h
           ret
PrtHex     endp

.end

This one comes from a newsgroup post from Charles A. Crayne who was once the moderator
of clax (comp.lang.asm.x86). He has now since passed away, may he rest in peace.

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Charles A. Crayne
As an additional example, I am still using essentially the same routine
which I wrote almost 25 years ago, albeit updated for 32-bit registers:

;hex to ascii routines
;eax    value to be converted
;esi    ->result string
;returns esi->next position
hexdd:  push    eax
        shr     eax,16          ;do high word first
        call    hexdw
        pop     eax
hexdw:  push    eax
        shr     eax,8           ;do high byte first
        call    hexdb
        pop     eax
hexdb:  push    eax
        shr     eax,4           ;do high nibble first
        call    hexdn
        pop     eax
hexdn:  and     eax,0fh         ;isolate nibble
        add     al,'0'          ;convert to ascii
        cmp     al,'9'          ;valid digit?
        jbe     hexdn1          ;yes
        add     al,7            ;use alpha range
hexdn1: mov     [esi],al        ;store result
        inc     esi             ;next position
        ret
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-