Before we start with logical operations, I will show some more <b>Bcc</b>
instructions as I promised. Apart from data registers, address
registers and program counter, there's an another register (last one, I
promise) called <b>SR</b> (<b>status register</b>). We are now only interested in 5 bits of it, named <b>CCR</b> (<b>flag register</b>). Everything moved to CCR will be copied in the lower byte of <b>SR</b> (this works in reverse). Anyway, <b>CCR</b>

consists of 5 bits, which are flags. These flags are set or cleared
when certain thing is being done with registers, or an instruction is
called. These flags are:<br><br>

- <b>C Flag</b> (<b>carry</b>) - 9th bit of adding, shifting and bit rotation.<br><br>

- <b>V Flag</b> (<b>overflow</b>) - It'll be set when result can't be represented. Eg. when you add <b>$01 (byte)</b> to <b>$7F (byte)</b>, the result is <b>$80</b> and <b>V flag</b> will be set<br><br>

- <b>Z Flag</b> (<b>zero</b>) - Will be set when the result is zero.<br><br>

- <b>N Flag</b> (<b>negative</b>) - Will be set if the highest bit of result is set (in two complement it's the sign bit, so the signed negative)<br><br>

- <b>X Flag</b> (<b>extended</b>) - A copy of <b>C flag</b>.<br><br>

But what these things have to do with <b>CMP</b> and <b>Bcc</b>? In C, this code:

something == 1

will return 1 if it's true. Now, when you compare two values, <b>Z flag</b> is set when they are the same or is cleared when they're different. <b>BEQ</b> branches if <b>Z flag</b> is set and <b>BNE</b> will branch if <b>Z flag</b> is cleared. So... <b>BEQ</b> and <b>BNE</b> can be used not only when comparing two values. Look at that example:<br><br>

<pre class="68k">	moveq	#0,d0
	beq	zflagisset
	rts
zflagisset:</pre>

It will branch, because the result of setting <b>d0</b> to <b>0</b> is <b>0</b> and <b>Z flag</b> is set. And <b>BEQ</b> branches if <b>Z flag</b> is set. This comes in handy if you want to test if a number is null or not. Here's a list of <b>Bcc</b> instructions:<br><br>

<pre class="68k">instr	conditions				flags required

BEQ	Branch if equal				Z = 1
BNE	Branch if not equal			Z = 0

BGE	Branch if greater or equal (signed)		N = V
BGT	Branch if greater than (signed)		N = V, Z=0
BLT	Branch if lesser than (signed)		N != V, Z=0
BLE	Branch if lesser or equal (signed)		N != V

BHI	Branch if higher than (unsigned)		C = 0, Z = 0
BHS	Branch if higher or same as (unsigned)	C = 0 OR Z = 1
BLO	Branch if lower than (unsigned)		C = 1, Z = 0
BLS	Branch if lower or same as			C = 1 OR Z = 1

BCC	Branch if carry clear			C = 0
BCS	Branch if carry set			C = 1
BVC	Branch if overflow clear			V = 0
BVS	Branch if overflow clear			V = 1
BPL	Branch if plus				N = 0
BMI	Branch if minus				N = 1

BRA	Branch Always

= means equal, != means not equal =P</pre>


Enough of these branches, let's learn about bits...<br><br>
I really hope you know what bits and bytes are. If you don't, a byte
consists of 8 bits, which can be either 1 (set) or 0 (cleared). Number
7 is 111 binary. You can do fun stuff with bits. Eg. the keys pressed
on joypad are all stored in one byte. Each bit of this byte represents
a key:
<pre class="68k">7 6 5 4 3 2 1 0

S A C B R L D U</pre>

The lowest bit is up, bit 1 is down etc...<br><br>

Now if you want to know if <b>B</b> is pressed, what should you do? You should use the <b>AND</b> instruction.<br><br> 


<b>AND</b> operation compares all bits of two values and sets a bit in
a new one if this bit was set in both of the values. I'm not an expert
of logical operations so you can laugh =P Example:<br><br>
<pre class="68k">Value 1: 10101100
Value 2: 11001010
         --------
Result:  10001000</pre>

Take a look at this code:<br><br>
<pre class="68k">	move.b	($FFFFF604).w,d0	;$FFFFF604 is the joypad output in Sonic 1
	and.b	#$10,d0			;you could use #%00010000
	beq	Bnotpressed</pre>

Imagine we press <b>B</b>, <b>Up</b> and <b>Start</b>. <b>And</b> instruction will look like that:<br><br>

<pre class="68k">         SACBRLDU
Value 1: 10010001 (keys pressed)
Value 2: 00010000 (and)
         --------
Result:  00010000</pre><br><br>

The result is not zero so it won't branch! Nifty, isn't it? There are other:<br><br>
<pre class="68k">	or.b	#$8,d0</pre>

<b>OR</b> sets the result bit if value 1 bit OR value 2 bit is set.<br><br>
<pre class="68k">Value 1: 00100100
Value 2: 00001000
         --------
Result:  00101100</pre>

Next:<br><br>
<pre class="68k">	eor.b	#$24,d0</pre>

<b>EOR</b> sets the result bit if one and <u>ONLY</u> one bit is set in value bits (like x86 <b>xor</b>).<br><br>
<pre class="68k">Value 1: 00101110
Value 2: 00010100
         --------
Result:  00111010
</pre>

And next:<br><br>
<pre class="68k">	not.b	d0</pre>

<b>NOT</b> clears the bit if it was set and sets the bit if it was cleared. (changes all bits)
<pre class="68k">Before:  01001001
After:   10110110</pre>
And now on with bit shifting. This operation moves bits by an ammount
to the right or to the left. Eg. we'll shift a value 3 times to the
right:<br><br>
<pre class="68k">Before:  00101000
After:   &gt;&gt;&gt;00101000</pre>

So after this it'll be:<br><br>
<pre class="68k">         00000101</pre>
Bit shifting is fun because one shift to the left multiples a number by
2 and one shift to the right divides it by 2. The instructions:<br><br>
<pre class="68k">	lsl.b	#3,d0
	lsr.b	#2,d1</pre>

<b>LSL</b> shifts x bits to the left, <b>LSR</b> shifts x bits to the right. There's also bit rotating:<br><br>

<pre class="68k">	rol.b	#1,d0</pre>

It'll rotate a number one bit to the left:<br><br>
<pre class="68k">Before:  10101010
After:  10101010
        |-------^

         01010101</pre>

It works the same to the right. The last part of bits:<br><br>
<pre class="68k">	bset	#1,d0	;sets bit 1 of d0
	bclr	#2,d1	;clears bit 2 of d1
	bchg	#3,d2	;changes bit 3 of d2 (like not)
	btst	#6,d0	;test if bit 6 of d0 is set, sets 
			;z flag if it's cleared else clears z flag</pre>

homework:<br><br>

<pre class="68k">	move.b	#$4,d0
	or.b	#$1,d0
	eor.b	#$2,d0
	lsl.b	#1,d0
	ror.b	#2,d0
	btst	#7,d0
	bne	branch
	rts
branch:</pre>

Now your homework is to guess if it is going to branch or not. Bit operations are boring at first but you'll get used to them...
<br>