Page 1 of 1

HEX TO DEC

Posted: Mon Jun 12, 2017 3:17 am
by FlameCyclone
This is my code, it can convert hex (0000-FFFF) to DEC (0-65535)

Code: Select all

; [2 bytes hexadecimal to 5 decimal]
; FlameCyclone 2017.6.11


 .ORG $8000
 
HEX_D = $70
DEC_D = $72
 
HEX_TO_DEC:        ; Hexadecimal to decimal
 PHA        ; protects current A and X data
 TXA
 PHA
 LDA #$FF
 STA HEX_D        ; sets the high order of the hexadecimal number to be converted
 LDA #$FF
 STA HEX_D + 1        ; Sets the low bit of the hexadecimal number to be converted
 JSR DEC_CLEARN        ; Clear the decimal data
 JSR TS_D        ; Hexadecimal to decimal
 PLA        ; recovery before A and X data
 TAX
 PLA
 RTS        ; return

HEX_DEC_DATA_H: Decimal corresponding hexadecimal high
 .DB $27
 .DB $03
 .DB $00
 .DB $00
 .DB $00
 
HEX_DEC_DATA_L: Decimal corresponding hexadecimal low
 .DB $10
 .DB $E8
 .DB $64
 .DB $0A
 .DB $01
 
DEC_CLEARN:        ; Clears the decimal data
 LDX #$00
 LDA #$00
 STA DEC_D
 STA DEC_D + 1
 STA DEC_D + 2
 STA DEC_D + 3
 STA DEC_D + 4
 RTS
 

TS_D:        ; Hexadecimal conversion to decimal data
 LDA HEX_D        ; Reads the hexadecimal digits to be converted
 CMP HEX_DEC_DATA_H, X        ; High conversion of hexadecimal number to decimal number corresponding to decimal number
 BNE TS_D_S        ; does not have the same conversion
 LDA HEX_D + 1        ; Read the decimal number to be converted to hexadecimal
 CMP HEX_DEC_DATA_L, X        ; ​​the high level of the hexadecimal number to be converted to the decimal number corresponding to the decimal number
 BCC TS_D_RTS        ; less than the current decimal not converted
TS_D_S:        ; The conversion is started
 LDA HEX_D        ; Reads the hexadecimal digits to be converted
 CMP HEX_DEC_DATA_H, X        ; High conversion of hexadecimal number to decimal number corresponding to decimal number
 BCC TS_D_RTS        ; less than the current decimal not converted
 LDA HEX_D + 1        ; Read the decimal number to be converted to hexadecimal
 SEC        ; entry position 1
 SBC HEX_DEC_DATA_L, X        ; ​​Low conversion of hexadecimal number to decimal place corresponding to decimal number
 STA HEX_D + 1        ; the result is sent to the lower hexadecimal number to be converted
 BCS D_DEC_HEX_D        ; skip without borrow
 DEC HEX_D        ; there is a borrow to convert hexadecimal highs
D_DEC_HEX_D:        ; Hexadecimal highs decrease
 LDA HEX_D        ; Reads the hexadecimal digits to be converted
 SEC        ; entry position 1
 SBC HEX_DEC_DATA_H, X        ; High conversion of hexadecimal digits to decimal places corresponding to decimal
 STA HEX_D        ; the result is sent to the hexadecimal number to be converted
ADD_DEC_D:        ; Decimal corresponding bit increments
 INC DEC_D, X        ; Decimal corresponding bit increments
 JMP TS_D        ; continue conversion
TS_D_RTS:        ; Converts a decimal bit to the end
 INX        ; counter increments
 CPX #$05        ; whether to convert 5 number
 BCC TS_D        ; continue to convert the next bit without conversion
 RTS        ; return [/ code]

Re: HEX TO DEC

Posted: Mon Jun 12, 2017 3:34 am
by FrankenGraphics
This is cool!

Regarding the number of threads with one routine each, do you want a separate commentary on each of them? Or is the goal sharing your code? If so, is it for the sake of learning/comparing with others, or is the intention that others may use it? If so, do you have a license or code of use in mind? Sorry, lots of questions. :lol:

Re: HEX TO DEC

Posted: Mon Jun 12, 2017 6:13 pm
by Quietust
Remark: since the main language of these forums is English (aside from the "International" section), it's a bit confusing for all of the comments in these samples to be in Chinese.