Re: LZ: display hex


[Prev][Next][Index][Thread]

Re: LZ: display hex



>Could anyone tell me the best way to display hex numbers? I'm going to look
>through the source to hexview and popchar. I was just wondering if anyone
>knew a better or shorter way to display a single 8-bit hex number.
>
>David Kristensen at the University of Missouri - Kansas City
>dkristensen@cctr.umkc.edu


Here is something I posted to this list back in February. Given the
(apparent) number of new members since then, I am reposting it in its
entirety.


================================


Displaying the value in A in hexadecimal is a fairly common task; I have
seen many ways of doing it.  Many people write a routine from scratch to do
the job, with varying levels of efficiency.  Unfortunately, other
programmers simply adopt someone else's routine, and don't necessarily
choose a good one.


In the interest of stamping out bad hex-display routines, I submit the
following.  Perhaps this could (should?) be included somewhere on the
Zshell home page?


\\\\\\\
Here are two ways to display the value in A as a two-digit hex number.  The
first method is shorter; the second is slightly more understandable (but
longer by 2 bytes).


Note that both routines destroy the value of A; other registers should be
untouched.
\\\\\\\


PrintHex:
  push  af
  rrca
  rrca
  rrca
  rrca
  CALL_(HexDigit)
  pop   af
HexDigit:
  and   $0F
  add   a,$90
  daa
  adc   a,$40
  daa
  ROM_CALL(TX_CHARPUT)
  ret


\\\\\\\\
Version 2: the same up to the HexDigit section ...
\\\\\\\\
HexDigit:
  and   $0F
  add   a,$30               ;= '0'
  cp    $3A                 ;= '9' + 1
  jr    c,PrintIt
  add   a,7                 ;= 'A' - ':'
PrintIt:
  ROM_CALL(TX_CHARPUT)
  ret


\\\\\\\\
To display the four-digit hex number in (e.g.) HL, one could place the
following three lines above the either of the "PrintHex" routines given
above:


PrintHex4:
        ld      a,h
        CALL_(PrintHex)
        ld      a,l


\\\\\\\\
Any comments, questions, or additions?


\\      Darryl Nester     \/ Assoc. Prof. of Mathematics //
\\  nesterd@bluffton.edu  \/       Bluffton College      //
\\    ph: 419-358-3483    \/   Bluffton, OH  45817-1196  //


References: