User Tools

Site Tools


Rotate byte and perform an action on each bit set to 1

Invented by Hoogo. Written by Frantic.

Sometimes you've got a byte value and for each bit you want to perform some action if the bit is set to 1 and do nothing, or something else, if the bit is set to 0. Hoogo came up with a nice way of doing that on CSDb which doesn't clobber any registers except for the status register.

	asl pattern
	bcc no_action
	inc pattern
	;Perform action here

This works fine because ASL always shifts in a 0 as the new lsb in the byte and the INC, which changes the lsb to 1, is only executed if a 1 was shifted out into the carry flag by the ASL instruction.

In some circumstances it might be a better optimization to use 0 as flag for “take action” instead of 1, because then the INC will only be needed when no action is to be taken, but whether that is desirable obviously depend on a number of factors.

base/rotate_byte_and_act_on_1-bits.txt · Last modified: 2016-03-03 21:01 by ftc