Convert an 8-bit number to hex in z80 assembler

I am writing a game for the ZX Spectrum using the z80 and have a bit of a problem. I manipulated a routine to convert the number contained in register "a" into the hexadecimal value contained in "de". I'm not sure how to convert the other path, EG is passed in hexadecimal to de and converts it to the decimal number contained in "a".

Note. The following procedure converts the input into ascii values ​​that represent values ​​from 0 to F. EG, if a = 255, then d = 70 and e = 70, since "F" is the value of ascii 70.

NumToHex    ld c, a   ; a = number to convert
            call Num1
            ld d, a
            ld a, c
            call Num2
            ld e, a
            ret  ; return with hex number in de

Num1        rra
            rra
            rra
            rra
Num2        or $F0
            daa
            add a, $A0
            adc a, $40 ; Ascii hex at this point (0 to F)   
            ret

Can someone advise a solution for working in the opposite direction or suggest a better solution?

+4
1

DE ASCII A. , DE A F. , ASCII '0'.. '9' 'A'.. 'F'.

HexToNum ld   a,d
         call Hex1
         add  a,a
         add  a,a
         add  a,a
         add  a,a
         ld   d,a
         ld   a,e
         call Hex1
         or   d
         ret

Hex1     sub  a,'0'
         cp   10
         ret  c
         sub  a,'A'-'0'-10
         ret

: "A".. "F" Hex1.

: "add a, a", , "sla a". , , .

+5

Source: https://habr.com/ru/post/1534829/


All Articles