User Tools

Site Tools


base:rotate_byte_and_act_on_1-bits

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

base:rotate_byte_and_act_on_1-bits [2016-03-03 21:01] (current)
ftc created
Line 1: Line 1:
 +====== 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 [[http://csdb.dk/forums/?roomid=11&topicid=115488#115562|a nice way of doing that on CSDb]] which doesn't clobber any registers except for the status register.
 +
 +<code>
 + asl pattern
 + bcc no_action
 + inc pattern
 + ;Perform action here
 + ...
 +no_action:
 +</code>
 +
 +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