[FC][HexToDec][8,16,24,32Bits]

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems.

Moderator: Moderators

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

[FC][HexToDec][8,16,24,32Bits]

Post by FlameCyclone » Fri May 17, 2019 9:16 pm

8Bits Hexadecimal To Decimal

Code: Select all

;[FC][8位HEX转DEC][20181122]
;FlameCyclone

 .ORG $8000

DEC_1 = $00
DEC_10 = DEC_1+$01
DEC_100 = DEC_1+$02

;0xFF转换为255耗时114
;0x63转换为99耗时136
;0x09转换为9耗时37
;0x64转换为100耗时48
;0x0A转换为10耗时48

 LDA #$63
 JSR HEX_TO_DEC
 RTS

HEX_TO_DEC:
 LDY #$00
DIGIT_CLEAR:
 STY DEC_100
 STY DEC_10
 STY DEC_1
COUNT_100:
 CMP #100
 BCC SET_100
 SBC #100
 INY
 BNE COUNT_100
SET_100:
 STY DEC_100
 LDY #$00
COUNT_10:
 CMP #10
 BCC SET_10
 SBC #10
 INY
 BNE COUNT_10
SET_10:
 STY DEC_10
SET_1:
 STA DEC_1
 RTS
16Bits Hexadecimal To Decimal

Code: Select all

;[FC][16位HEX转DEC][20181122]
;FlameCyclone

 .ORG $8000
 
Hex_L = $00
Hex_H = $01
Dec_Digit = $02

;$0xFFFF转65535耗时898
;$0x270F转9999耗时1191
;$0x3E7转999耗时738
;$0x63转99耗时332
;$0x09转9耗时134

 LDX #$FF
 TXS

 LDA #$0F
 STA Hex_L
 LDA #$27
 STA Hex_H
 JSR Hex_To_Dec
 RTS

Hex_To_Dec: 
 LDY #$00
Dec_Clear_Beg:
 STY Dec_Digit
 STY Dec_Digit+1
 STY Dec_Digit+2
 STY Dec_Digit+3
 STY Dec_Digit+4
 
 LDX #$04
Convert_Compare:
 LDA Hex_H
 CMP Dec_Data_H,X
 BCC Convert_Digit_End
 BNE Subtraction_16Bit
 LDA Hex_L
LowByte_Compare:
 CMP Dec_Data_L,X
 BCC Convert_Digit_End
 BEQ Subtraction_16Bit
Subtraction_8Bit:
 SBC Dec_Data_L,X
 STA Hex_L
 INY
 BNE LowByte_Compare
Subtraction_16Bit:
 LDA Hex_L
 SBC Dec_Data_L,X
 STA Hex_L
 BCS Subtraction_16Bit_NoBrrow
 DEC Hex_H
Subtraction_16Bit_NoBrrow:
 LDA Hex_H
 SEC
 SBC Dec_Data_H,X
 STA Hex_H
Set_Digit:
 INY
 BNE Convert_Compare
Convert_Digit_End:
 STY Dec_Digit,X
 LDY #$00
 DEX
 BNE Convert_Compare
Convert_Finish:
 STA Dec_Digit
 RTS

Dec_Data_L:
 .DB $01,$0A,$64,$E8,$10
Dec_Data_H:
 .DB $00,$00,$00,$03,$27
24Bits Hexadecimal To Decimal

Code: Select all

;[FC][24位HEX转DEC][20190402]
;FlameCyclone

 .ORG $8000
 
Hex_LL = $00
Hex_LH = $01
Hex_HL = $02
Dec_Digit = $03

DIGIT_COUNT = 08

;零页占用: 11字节
;  程序段: 0x7C字节
;$0xFFFFFF转16777215耗时2516
;$0x98967F转9999999耗时4306
; $0xF423F转999999耗时3717
; $0x1869F转99999耗时3100
;  $0x270F转9999耗时2463
;   $0x3E7转999耗时1756
;    $0x63转99耗时1068
;    $0x09转9耗时330

 LDX #$FF
 TXS

 LDA #$7F
 STA Hex_LL
 LDA #$96
 STA Hex_LH
 LDA #$98
 STA Hex_HL
 JSR Hex_To_Dec
 RTS

Hex_To_Dec: 
 LDY #$00
 LDX #$00
Dec_Clear:
 STA Dec_Digit,X
 INX
 CPX #DIGIT_COUNT
 BCC Dec_Clear
 DEX
Convert_Compare:
 LDA Hex_HL
HL_Compare:
 CMP Dec_Data_HL,X
 BCC Convert_Digit_End
 BNE Sub_LL
 LDA Hex_LH
LH_Compare
 CMP Dec_Data_LH,X
 BCC Convert_Digit_End
 BNE Sub_LL
 LDA Hex_LL
LL_Compare:
 CMP Dec_Data_LL,X
 BCC Convert_Digit_End
Sub_LL:
 LDA Hex_LL
 SBC Dec_Data_LL,X
 STA Hex_LL
 BCS Sub_LH
 DEC Hex_LH
Sub_LH:
 LDA Hex_LH
 SEC
 SBC Dec_Data_LH,X
 STA Hex_LH
 BCS Sub_HL
 DEC Hex_HL
Sub_HL: 
 LDA Hex_HL
 SEC
 SBC Dec_Data_HL,X
 STA Hex_HL
Set_Digit:
 INY
 BNE Convert_Compare
Convert_Digit_End:
 STY Dec_Digit,X
 LDY #$00
 DEX
 BNE Convert_Compare
Convert_Finish:
 STA Dec_Digit
 RTS

Dec_Data_LL:
 .DB $01,$0A,$64,$E8,$10,$A0,$40,$80
Dec_Data_LH:
 .DB $00,$00,$00,$03,$27,$86,$42,$96
Dec_Data_HL:
 .DB $00,$00,$00,$00,$00,$01,$0F,$98
32Bits Hexadecimal To Decimal

Code: Select all

;[FC][32位HEX转DEC][20190402]
;FlameCyclone

 .ORG $8000
 
Hex_LL = $00
Hex_LH = $01
Hex_HL = $02
Hex_HH = $03
Dec_Digit = $04

DIGIT_COUNT = 10

;零页占用: 14字节
;  程序段: 0xA3字节
;$0xFFFFFFFF转4294967295耗时5508
;$0x3B9AC9FF转999999999耗时7532
; $0x5F5E0FF转99999999耗时6747
;  $0x98967F转9999999耗时5974
;   $0xF423F转999999耗时5133
;   $0x1869F转99999耗时4264
;    $0x270F转9999耗时3375
;     $0x3E7转999耗时2418
;      $0x63转99耗时1476
;      $0x09转9耗时486

 LDX #$FF
 TXS

 LDA #$FF
 STA Hex_LL
 LDA #$C9
 STA Hex_LH
 LDA #$9A
 STA Hex_HL
 LDA #$3B
 STA Hex_HH
 JSR Hex_To_Dec
 RTS

Hex_To_Dec: 
 LDY #$00
Dec_Clear:
 STA Dec_Digit,X
 INX
 CPX #DIGIT_COUNT
 BCC Dec_Clear
 DEX
Convert_Compare:
HH_Compare:
 LDA Hex_HH
 CMP Dec_Data_HH,X
 BCC Convert_Digit_End
 BNE Sub_LL
 LDA Hex_HL
HL_Compare:
 CMP Dec_Data_HL,X
 BCC Convert_Digit_End
 BNE Sub_LL
 LDA Hex_LH
LH_Compare
 CMP Dec_Data_LH,X
 BCC Convert_Digit_End
 BNE Sub_LL
 LDA Hex_LL
LL_Compare:
 CMP Dec_Data_LL,X
 BCC Convert_Digit_End
Sub_LL:
 LDA Hex_LL
 SBC Dec_Data_LL,X
 STA Hex_LL
 BCS Sub_LH
 DEC Hex_LH
Sub_LH:
 LDA Hex_LH
 SEC
 SBC Dec_Data_LH,X
 STA Hex_LH
 BCS Sub_HL
 DEC Hex_HL
Sub_HL: 
 LDA Hex_HL
 SEC
 SBC Dec_Data_HL,X
 STA Hex_HL
 BCS Sub_HH
 DEC Hex_HH
Sub_HH:
 LDA Hex_HH
 SEC
 SBC Dec_Data_HH,X
 STA Hex_HH
Set_Digit:
 INY
 BNE Convert_Compare
Convert_Digit_End:
 STY Dec_Digit,X
 LDY #$00
 DEX
 BNE Convert_Compare
Convert_Finish:
 STA Dec_Digit
 RTS

Dec_Data_LL:
 .DB $01,$0A,$64,$E8,$10,$A0,$40,$80,$00,$00
Dec_Data_LH:
 .DB $00,$00,$00,$03,$27,$86,$42,$96,$E1,$CA
Dec_Data_HL:
 .DB $00,$00,$00,$00,$00,$01,$0F,$98,$F5,$9A
Dec_Data_HH:
 .DB $00,$00,$00,$00,$00,$00,$00,$00,$05,$3B

Post Reply