HEX TO DEC

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems. See the NESdev wiki for more information.

Moderator: Moderators

Post Reply
FlameCyclone
Posts: 15
Joined: Fri Jun 02, 2017 2:45 am

HEX TO DEC

Post 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]
Last edited by FlameCyclone on Wed Jun 14, 2017 1:57 am, edited 1 time in total.
User avatar
FrankenGraphics
Formerly WheelInventor
Posts: 2064
Joined: Thu Apr 14, 2016 2:55 am
Location: Gothenburg, Sweden
Contact:

Re: HEX TO DEC

Post 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:
User avatar
Quietust
Posts: 1920
Joined: Sun Sep 19, 2004 10:59 pm
Contact:

Re: HEX TO DEC

Post 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.
Quietust, QMT Productions
P.S. If you don't get this note, let me know and I'll write you another.
Post Reply