Difference between revisions of "Converting Binary Coded Decimal"
m (1 revision) |
(added pointer to other implementation) |
||
(4 intermediate revisions by one other user not shown) | |||
Line 1: | Line 1: | ||
− | [[Category:BASIC]] | + | [[Category:BASIC]][[Category:Programming tips]] |
'''B'''inary '''C'''oded '''D'''ecimal (BCD) is a method of representing a decimal number as a hexadecimal value using the hex digits 0-9 and ignoring A-F. For example, the number 21 (hex &15) would be represented as &21 (decimal 33). It can be a convenient internal representation of decimal numbers as each hexadecimal digit can be displayed without any further processing. See [http://en.wikipedia.org/wiki/Binary-coded_decimal Wikipedia]. | '''B'''inary '''C'''oded '''D'''ecimal (BCD) is a method of representing a decimal number as a hexadecimal value using the hex digits 0-9 and ignoring A-F. For example, the number 21 (hex &15) would be represented as &21 (decimal 33). It can be a convenient internal representation of decimal numbers as each hexadecimal digit can be displayed without any further processing. See [http://en.wikipedia.org/wiki/Binary-coded_decimal Wikipedia]. | ||
BCD values can be decoded with: | BCD values can be decoded with: | ||
− | + | number% = VALSTR$~bcd% | |
Numbers can be encoded in BCD using: | Numbers can be encoded in BCD using: | ||
− | + | bcd% = EVAL("&"+STR$number%) | |
+ | |||
+ | A 7-bit binary number can be converted to BCD so it can be printed in hex with the following 6502 code: | ||
+ | |||
+ | \ Convert binary number to BCD | ||
+ | \ ---------------------------- | ||
+ | \ On entry, A=value 0-99 | ||
+ | \ On exit, A=BCD value &00-&99 | ||
+ | \ X corrupted | ||
+ | .BINtoBCD | ||
+ | TAX :\ Transfer number to X | ||
+ | LDA #&99 :\ Start with -1 in BCD form | ||
+ | SED :\ Switch to Decimal arithmetic | ||
+ | .BINtoBCDlp | ||
+ | CLC:ADC #1 :\ Add 1 with BCD arithmetic | ||
+ | DEX :\ Decrement input value in X | ||
+ | BPL BINtoBCDlp :\ Loop until input value < 0 | ||
+ | CLD:RTS :\ Switch back to Binary arithmetic | ||
+ | See also [[{{TALKPAGENAME}}#More_efficient_variants?|Discussion]] for an enhanced implementation. | ||
+ | |||
+ | A BCD number can be converted to binary with the following 6502 code: | ||
+ | |||
+ | \ Convert BCD to Binary | ||
+ | \ -------------------------------------------- | ||
+ | \ On entry, A=BCD value &00-&99 | ||
+ | \ On exit, X=binary value 0-99 | ||
+ | \ A=corrupted | ||
+ | .BCDtoBIN | ||
+ | LDX #&FF :\ Start with result=-1 | ||
+ | SED :\ Switch to Decimal arithmetic | ||
+ | SEC :\ Prepare for subtraction | ||
+ | .BCDtoBINlp | ||
+ | INX :\ Add 1 to result | ||
+ | SBC #1 :\ Subtract 1 with BCD arithmetic | ||
+ | BCS BCDtoBINlp :\ Loop until BCD value < 0 | ||
+ | CLD:RTS :\ Switch back to Binary arithmetic | ||
+ | See also [[{{TALKPAGENAME}}#More_efficient_variants?|Discussion]] for an enhanced implementation. | ||
+ | |||
[[User:Jgharston|Jgharston]] 21:11, 23 June 2007 (BST) | [[User:Jgharston|Jgharston]] 21:11, 23 June 2007 (BST) | ||
+ | [[User:Jgharston|Jgharston]] ([[User talk:Jgharston|talk]]) 00:19, 3 October 2016 (UTC) |
Latest revision as of 21:12, 7 November 2016
Binary Coded Decimal (BCD) is a method of representing a decimal number as a hexadecimal value using the hex digits 0-9 and ignoring A-F. For example, the number 21 (hex &15) would be represented as &21 (decimal 33). It can be a convenient internal representation of decimal numbers as each hexadecimal digit can be displayed without any further processing. See Wikipedia. BCD values can be decoded with:
number% = VALSTR$~bcd%
Numbers can be encoded in BCD using:
bcd% = EVAL("&"+STR$number%)
A 7-bit binary number can be converted to BCD so it can be printed in hex with the following 6502 code:
\ Convert binary number to BCD \ ---------------------------- \ On entry, A=value 0-99 \ On exit, A=BCD value &00-&99 \ X corrupted .BINtoBCD TAX :\ Transfer number to X LDA #&99 :\ Start with -1 in BCD form SED :\ Switch to Decimal arithmetic .BINtoBCDlp CLC:ADC #1 :\ Add 1 with BCD arithmetic DEX :\ Decrement input value in X BPL BINtoBCDlp :\ Loop until input value < 0 CLD:RTS :\ Switch back to Binary arithmetic
See also Discussion for an enhanced implementation.
A BCD number can be converted to binary with the following 6502 code:
\ Convert BCD to Binary \ -------------------------------------------- \ On entry, A=BCD value &00-&99 \ On exit, X=binary value 0-99 \ A=corrupted .BCDtoBIN LDX #&FF :\ Start with result=-1 SED :\ Switch to Decimal arithmetic SEC :\ Prepare for subtraction .BCDtoBINlp INX :\ Add 1 to result SBC #1 :\ Subtract 1 with BCD arithmetic BCS BCDtoBINlp :\ Loop until BCD value < 0 CLD:RTS :\ Switch back to Binary arithmetic
See also Discussion for an enhanced implementation.
Jgharston 21:11, 23 June 2007 (BST)
Jgharston (talk) 00:19, 3 October 2016 (UTC)