Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.sys.apple2.programmer > #1474

Binary-to-Decimal Conversion Subroutine 27-Apr-80 Woz

From D Finnigan <dog_cow@macgui.com>
Newsgroups comp.sys.apple2.programmer
Subject Binary-to-Decimal Conversion Subroutine 27-Apr-80 Woz
Date 2015-01-12 18:02 +0000
Organization Mac GUI
Message-ID <dog_cow-1421085783@macgui.com> (permalink)

Show all headers | View raw


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/

Back to comp.sys.apple2.programmer | Previous | NextNext in thread | Find similar | Unroll thread


Thread

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

csiph-web