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 00.24.xx

.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 00.24.xx

.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


All rights reserved
Legal Notice
Copyright © 1984-2009 Forever Young Software
Forever Young Software for Hire
Return to My Home Page