Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.sys.apple2.programmer > #1474 > unrolled thread
| Started by | D Finnigan <dog_cow@macgui.com> |
|---|---|
| First post | 2015-01-12 18:02 +0000 |
| Last post | 2015-10-03 08:31 -0700 |
| Articles | 6 — 5 participants |
Back to article view | Back to comp.sys.apple2.programmer
Binary-to-Decimal Conversion Subroutine 27-Apr-80 Woz D Finnigan <dog_cow@macgui.com> - 2015-01-12 18:02 +0000
Re: Binary-to-Decimal Conversion Subroutine 27-Apr-80 Woz wssimms@gmail.com - 2015-01-12 10:22 -0800
Re: Binary-to-Decimal Conversion Subroutine 27-Apr-80 Woz D Finnigan <dog_cow@macgui.com> - 2015-01-12 18:39 +0000
Re: Binary-to-Decimal Conversion Subroutine 27-Apr-80 Woz jbrooks@blueshiftinc.com - 2015-09-25 21:12 -0700
Re: Binary-to-Decimal Conversion Subroutine 27-Apr-80 Woz qkumba <peter.ferrie@gmail.com> - 2015-10-02 08:27 -0700
Re: Binary-to-Decimal Conversion Subroutine 27-Apr-80 Woz John Brooks <jbrooks@blueshiftinc.com> - 2015-10-03 08:31 -0700
| From | D Finnigan <dog_cow@macgui.com> |
|---|---|
| Date | 2015-01-12 18:02 +0000 |
| Subject | Binary-to-Decimal Conversion Subroutine 27-Apr-80 Woz |
| Message-ID | <dog_cow-1421085783@macgui.com> |
This routine came up in a discussion in comp.sys.apple2 in November. It was published on pages 52 and 53 of the the Winter 1980 edition of The Apple Orchard Magazine. Following is my summary of the article and the source listing. Binary-to-Decimal Shortcut (Small is Beautiful) by Steve Wozniak When writing programs in machine language, it is generally preferable to calculate in binary, but to display in decimal. When a page number, game score, or the like must be printed, a 'binary-to-decimal' subroutine can be called. [then he goes on to explain...] (1) Clear the result DEC0, DEC1, and DEC2. Set the processor to DECIMAL mode, if any. (2) Shift the binary argument (HEX0 and HEX1) left, the most significant bit into the CARRY. (3) Perform the calculation RESULT <-- RESULT * 2 = CARRY by adding the result (DEC0, DEC1, and DEC2) to itself (plus the CARRY) in decimal mode. (4) Perform steps (2) and (3) a total of 16 times. [then he explains a bit more and finally comes the source listing] * HEX0 EQU 0 BINARY ARGUMENT (LOW BYTE) HEX1 EQU 1 DEC0 EQU 2 (LOW BYTE) DEC1 EQU 3 DECIMAL RESULT DEC2 EQU 4 (HIGH BYTE) * ORG $300 OBJ $6300 * HEXDEC LDA #0 STA DEC0 CLEAR RESULT (EXCEPT HIGH BYTE). STA DEC1 SED SET 6502 DECIMAL MODE. LDY #16 PREPARE FOR 16 BITS. LOOP ASL HEX0 ROL HEX1 SHIFT BIT OUT OF BINARY ARGUMENT. LDA DEC0 ADC DEC0 STA DEC0 DOUBLE DECIMAL RESULT. LDA DEC1 (PLUS CARRY) ADC DEC1 STA DEC1 ROL DEC2 SHIFT IN LAST BIT. DEY REPEAT 16 TIMES/ BNE LOOP CLD ;CLEAR DECIMAL MODE. RTS RETURN. SYM --- END ASSEMBLY --- TOTAL ERRORS: 0 32 BYTES GENERATED THIS ASSEMBLY -- ]DF$ Apple II Book: http://macgui.com/newa2guide/ Usenet: http://macgui.com/usenet/ <-- get posts by email! Apple II Web & Blog hosting: http://a2hq.com/
[toc] | [next] | [standalone]
| From | wssimms@gmail.com |
|---|---|
| Date | 2015-01-12 10:22 -0800 |
| Message-ID | <0b8c643e-175b-4c18-8582-3973d54049de@googlegroups.com> |
| In reply to | #1474 |
Am Montag, 12. Januar 2015 13:03:06 UTC-5 schrieb D Finnigan: > This routine came up in a discussion in comp.sys.apple2 in November. > > It was published on pages 52 and 53 of the the Winter 1980 edition of The > Apple Orchard Magazine. Following is my summary of the article and the > source listing. > > > Binary-to-Decimal Shortcut > (Small is Beautiful) > > by Steve Wozniak > > When writing programs in machine language, it is generally preferable to > calculate in binary, but to display in decimal. When a page number, game > score, or the like must be printed, a 'binary-to-decimal' subroutine can be > called. > > [then he goes on to explain...] > > (1) Clear the result DEC0, DEC1, and DEC2. Set the processor to DECIMAL > mode, if any. > > (2) Shift the binary argument (HEX0 and HEX1) left, the most significant bit > into the CARRY. > > (3) Perform the calculation > RESULT <-- RESULT * 2 = CARRY > by adding the result (DEC0, DEC1, and DEC2) to itself (plus the CARRY) in > decimal mode. > > (4) Perform steps (2) and (3) a total of 16 times. > > [then he explains a bit more and finally comes the source listing] > > * > HEX0 EQU 0 BINARY ARGUMENT (LOW BYTE) > HEX1 EQU 1 > DEC0 EQU 2 (LOW BYTE) > DEC1 EQU 3 DECIMAL RESULT > DEC2 EQU 4 (HIGH BYTE) > * > ORG $300 > OBJ $6300 > * > HEXDEC LDA #0 > STA DEC0 CLEAR RESULT (EXCEPT HIGH BYTE). > STA DEC1 > SED SET 6502 DECIMAL MODE. > LDY #16 PREPARE FOR 16 BITS. > LOOP ASL HEX0 > ROL HEX1 SHIFT BIT OUT OF BINARY ARGUMENT. > LDA DEC0 > ADC DEC0 > STA DEC0 DOUBLE DECIMAL RESULT. > LDA DEC1 (PLUS CARRY) > ADC DEC1 > STA DEC1 > ROL DEC2 SHIFT IN LAST BIT. > DEY REPEAT 16 TIMES/ > BNE LOOP > CLD ;CLEAR DECIMAL MODE. > RTS RETURN. > SYM > > --- END ASSEMBLY --- > > TOTAL ERRORS: 0 > > 32 BYTES GENERATED THIS ASSEMBLY This seems like the most obvious way to do a binary to decimal conversion on the 6502, but I like the Woz-optimization (Woztimization?) of doing a ROL in the high byte of the result instead of another round of ADC/STA.
[toc] | [prev] | [next] | [standalone]
| From | D Finnigan <dog_cow@macgui.com> |
|---|---|
| Date | 2015-01-12 18:39 +0000 |
| Message-ID | <dog_cow-1421087988@macgui.com> |
| In reply to | #1475 |
wssimms wrote: > Am Montag, 12. Januar 2015 13:03:06 UTC-5 schrieb D Finnigan: >> This routine came up in a discussion in comp.sys.apple2 in November. >> >> It was published on pages 52 and 53 of the the Winter 1980 edition of The >> Apple Orchard Magazine. Following is my summary of the article and the >> source listing. >> >> >> Binary-to-Decimal Shortcut >> (Small is Beautiful) >> >> by Steve Wozniak >> > > This seems like the most obvious way to do a binary to decimal conversion > on the 6502, but I like the Woz-optimization (Woztimization?) of doing a > ROL in the high byte of the result instead of another round of ADC/STA. > Furthermore, in this article, Woz goes on to say: The algorithm works as follows. The first bit shifted out of the binary argument carries a weight of 2 to the 15th. It is added to the result which is subsequently doubled 15 times (in decimal), restoring the proper weight component. Each bit is treated in this manner. The high-order result byte, DEC2, can be shifted left at step (3) instead of added to itself in decimal since it will not exceed 9 ($FFFF = 06 55 35). If this is done, DEC2 need not be initialized to zero, since the original contents will be shifted out during execution. This optimization is implemented in the following 6502 subroutine [listed in my initial article - DF]. -- ]DF$ Apple II Book: http://macgui.com/newa2guide/ Usenet: http://macgui.com/usenet/ <-- get posts by email! Apple II Web & Blog hosting: http://a2hq.com/
[toc] | [prev] | [next] | [standalone]
| From | jbrooks@blueshiftinc.com |
|---|---|
| Date | 2015-09-25 21:12 -0700 |
| Message-ID | <7be0d28b-a072-4d3e-8822-1721e354f2ce@googlegroups.com> |
| In reply to | #1474 |
On Monday, January 12, 2015 at 10:03:06 AM UTC-8, D Finnigan wrote: > This routine came up in a discussion in comp.sys.apple2 in November. > > It was published on pages 52 and 53 of the the Winter 1980 edition of The > Apple Orchard Magazine. Following is my summary of the article and the > source listing. > > > Binary-to-Decimal Shortcut > (Small is Beautiful) > > by Steve Wozniak > > > * > HEX0 EQU 0 BINARY ARGUMENT (LOW BYTE) > HEX1 EQU 1 > DEC0 EQU 2 (LOW BYTE) > DEC1 EQU 3 DECIMAL RESULT > DEC2 EQU 4 (HIGH BYTE) > * > ORG $300 > OBJ $6300 > * > HEXDEC LDA #0 > STA DEC0 CLEAR RESULT (EXCEPT HIGH BYTE). > STA DEC1 > SED SET 6502 DECIMAL MODE. > LDY #16 PREPARE FOR 16 BITS. > LOOP ASL HEX0 > ROL HEX1 SHIFT BIT OUT OF BINARY ARGUMENT. > LDA DEC0 > ADC DEC0 > STA DEC0 DOUBLE DECIMAL RESULT. > LDA DEC1 (PLUS CARRY) > ADC DEC1 > STA DEC1 > ROL DEC2 SHIFT IN LAST BIT. > DEY REPEAT 16 TIMES/ > BNE LOOP > CLD ;CLEAR DECIMAL MODE. > RTS RETURN. > SYM > > --- END ASSEMBLY --- > > TOTAL ERRORS: 0 > > 32 BYTES GENERATED THIS ASSEMBLY > For the GS programmers out there, the 65816 can convert hex to decimal in 17 bytes (10 insn), nearly 1/2 the size of the 6502. It's also much faster with 16 bit instructions: HEXIN EQU 0 ;2 bytes DECWORK EQU 2 ;2 bytes DECOUT EQU 4 ;2 bytes * On Entry: * Word to convert at HEXIN * e=0, m=0, x=0 * DPage=0 * On Exit: * A=Low 4 decimal digits * X,Y,DB,DPage preserved * e=0, m=0, x=0, Decimal flag cleared * DECOUT=Highest decimal digit * HEXIN & DECWORK altered HEXDEC SEP #9 TDC ROL HEXIN LOOP STA DECWORK ADC DECWORK ROL DECOUT ASL HEXIN BNE LOOP CLD RTS 300: e2 09 78 26 00 85 02 65 308: 02 26 04 06 00 d0 f6 d8 60 Happy coding! -JB
[toc] | [prev] | [next] | [standalone]
| From | qkumba <peter.ferrie@gmail.com> |
|---|---|
| Date | 2015-10-02 08:27 -0700 |
| Message-ID | <e7b20e06-0f31-4058-ad6b-b405fa91259e@googlegroups.com> |
| In reply to | #1806 |
> For the GS programmers out there, the 65816 can convert hex to decimal in 17 bytes (10 insn), nearly 1/2 the size of the 6502. It's also much faster with 16 bit instructions: Part of the saving comes from it being not functionally equivalent (returning A instead of the complete value in DECOUT).
[toc] | [prev] | [next] | [standalone]
| From | John Brooks <jbrooks@blueshiftinc.com> |
|---|---|
| Date | 2015-10-03 08:31 -0700 |
| Message-ID | <91fa9dc8-4798-4a85-84d7-a842568c6488@googlegroups.com> |
| In reply to | #1810 |
On Friday, October 2, 2015 at 8:27:29 AM UTC-7, qkumba wrote: > > For the GS programmers out there, the 65816 can convert hex to decimal in 17 bytes (10 insn), nearly 1/2 the size of the 6502. It's also much faster with 16 bit instructions: > > Part of the saving comes from it being not functionally equivalent (returning A instead of the complete value in DECOUT). Good catch, Peter. Yes that was intentional on my part since in actual use most HexToDec callers will immediately print or store the decimal result, so it's more useful to return it in a register. This saves code in the conversion routine and also saves an LDA by the caller. If you want the routine to be functionally equivalent to the Woz version, just add a 2 byte STA DECWORK before the RTS, which makes it a 19 byte routine for 65816 vs 32 for 6502. -JB
[toc] | [prev] | [standalone]
Back to top | Article view | comp.sys.apple2.programmer
csiph-web