Re: [stella] 6502 basics: signed LSR

Subject: Re: [stella] 6502 basics: signed LSR
From: Russ Perry Jr <slapdash@xxxxxxx>
Date: Sun, 15 Feb 2004 12:52:17 -0600
At 5:41 PM +0000 2/15/04, KirkIsrael@xxxxxxxxxxxxx wrote:
> Is there a better way of dividing a signed 1-byte integer
> by 2 than this? (which says, if var is positive, just do LSR,
> otherwise LSR and OR w/ -128 i.e. #%10000000)[?]

I'll leave that answer to someone more qualified...

> It just seems odd to have to branch to preserve the sign bit

It wouldn't be if there was a DIV command of some sort, but
since there isn't, you need some trickery.

> but I wasn't clever enough to come up with a better way...
>
>	LDA var
>	BPL posval
>negval
>	LSR
>	ORA #-128
>	JMP done
>posval
>	LSR
>done
>	STA var 

I think another way (but arguably not any better or worse?) is:

	LDA var
	ASL	;sign (Bit7) goes into C, 0 into Bit0
	BCC adjust
	ORA #1	;1 into Bit0
adjust
	ROR	;C (was Bit7 from ASL) into Bit7, Bit0 into C
	ROR	;C into Bit7 (#1 from ORA if negative, else 0)
	STA var

It plays with the accumulator more, but has only one branch, so I
think it comes out a little faster (less cycles) for negative numbers
as a result.  It's fewer bytes too?  Of course, for positive numbers,
I think your method is faster -- less monkeying around.  Do you need
speed or consistency?
-- 
//*================================================================++
||  Russ Perry Jr   2175 S Tonne Dr #114   Arlington Hts IL 60005  ||
||  847-952-9729   slapdash@xxxxxxx [NEW!]   VIDEOGAME COLLECTOR!  ||
++================================================================*//
----------------------------------------------------------------------------------------------
Archives (includes files) at http://www.biglist.com/lists/stella/archives/
Unsub & more at http://www.biglist.com/lists/stella/


Current Thread