Arithmetic Shifts. By Lee Davison.

Introduction.

Though the 6502 instruction set has an instruction that is called Arithmetic Shift Left it is really only a Logical Shift Left as the overflow bit is not affected if there is a sign change. To perform a true arithmetic shift the state of the sign before and after the shift must be taken into account.

There is no Arithmetic shift right.

The code - Arithmetic Shift Left.

To perform a true Arithmetic Shift Left an add can be used instead ..


	CLC			; clear the carry for a single byte shift	
	STA	temp		; copy A		
	ADC	temp		; effectively perform a true ASL	

.. which looks like a long way to go about it but the overflow bit is set if the sign bit has been changed by the shift.

A multiple byte Arithmetic Shift Left can be done like this ..


	ASL	lsb		; shift the least significant byte
	ROL	inter		; shift any intermediate byte(s)		
...
	LDA	msb		; copy the most significant byte	
	ADC	msb		; effectively perform a true ASL
	STA	msb		; save the most significant byte	

In this case the overflow bit is only needed for the most significant byte so the less significant bytes can be shifted using shift instructions.

The code - Arithmetic Shift Right.

To perform an Arithmetic Shift Right the state of the sign bit needs to be copied to the carry bit before the shift ..


	CMP	#$80		; copy the sign bit to the carry bit		
	ROR	A		; effectively perform a true ASR	

Unlike the left shift there is no possibility of an overflow during a right shift.


Last page update: 23rd April, 2006. e-mail me