
		Fifth Steps
		===========

 In the last tutorial we looked at subroutines. Now we are going to move on
to binary operations. Before doing so lets cover some of the terminology:

 Terminology
 -----------

Binary		In computer junky terminology binary is refering to the
		base 2 number system which consists of the digits 0 and 1.

Bit		A bit is a binary digit, ie 0 or 1. All information is
		stored in a computer as sets of bits.

Bit Set		A bit is said to be set if its value is 1.

Bit Clear	A bit is said to be clear if its value is 0.

Byte		A byte is a set of eight bits. We often wish to refer to
		an individual bit in a byte and for this reason a each bit
		in a byte has a number depending on its position. The bits
		are numberd from right to left starting at 0. So bit 0
		refers to the right most bit of a byte and bit 7 refers to
		the left most bit. Take for example the byte 1011 0110 :

		bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0
		 1    0    1    1    0    1    1    0

Word		A word is a set of 16 bits. Obviously then a word is 
		equivalent to two bytes. The same bit numbering system is 
		adopted as for a byte, the bits being numberd 0 to 15 from
		right to left.

Long Word	A long word is a set of 32 bits, or 2 words, or 4 bytes.
		The bits are numberd 0 to 31 from right to left.

MSB		Most Signifigant Bit. This is the bit with the highest
		reference number. So in a byte, the MSB will be bit 7, in a
		word the MSB will be bit 15 and in a long word the MSB will
		be bit 31.

LSB		Least Signifigant Bit. This is always bit 0 wether refering
		to a byte, word or long word.

Sign Bit	If dealing with signed numbers, the MSB is sometimes
		refered to as the Sign Bit.

 As you now know, the internal registers are all 32 bits long ( ie they can
hold long word values ). With most 68000 instructions you can specify the
size of data you can operate on as byte ( .b ) word ( .w ) or long word (.l
). If you specify .l then bits 0 to 31 are effected. If you specify .w the
bits 0 to 15 are effected and if you specify .b then bits 0 to 7 are
effected. This was demonstrated earlier in the series, but to clarify
again:

 If x represents an individual bit in a long word, the following diagram
shows what bits are effected by the different size declarations

  xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
  
  |----------------.l---------------|

		    |-------.w------|

			     |--.b--| 

 There is an instruction that lets you test if a particular bit is set or
clear in an internal register or in memory.

 BTST
 ----

 Syntax		btst.s	Dn,<ea>
	   or   btst.s  #data,<ea>

	 where .s is .b or .l ( note .w not allowed )

 If you are testing a bit in memory then the instruction must use the .b
size.

 This instruction test the state of a bit in the destination and sets the
zero flag accordingly. You specify the bits reference number either in a
data register or as an immediate value. It is usual to follow this
instruction with a Bcc instruction so that some action can be taken
depending on the state of the bit tested.

 By way of an example consider the memory address $BFE001. This address is
used by the system to indicate various conditions. In particular, bit 6 is
used to indicate the state of the left mouse button. If bit 6 is set the
the left mouse button is not being pressed, if it is clear then the button
is being pressed. By testing this bit a loop can be devised that will wait
for the user to press the left mouse button:

wait		btst	#6,$bfe001
		bne	wait

		rts

 The btst instruction is testing bit 6 of the memory address $bfe001. As
long as this bit is not zero the program stays in the loop. As soon as the
user presses the left mouse button, the program finishes.

 You could have put the bit number in a data register as follows:

		moveq.l	#6,d0
loop		btst	d0,$bfe001
		bne	loop

		rts

 If you are wondering about the right mouse button, then test bit 10 of
memory address $DFF016 in the same way as for the left mouse button.

loop		btst	#10,$dff016
		bne	loop

		rts

 Boolean Algebra
 ---------------

 What ! This is not going to be an indepth study of abstract mathematics,
just a brief introduction. Boolean Algebra lends itself very well to
computer programming and the 68000 supports all the main Boolean operators
AND, OR, NOT and Exclusive OR ( EOR ).

 Truth Tables
 ------------

 All Boolean operator operate on bits. To see the result of an operator,
truth tables are used. 

 AND truth table

	s   d   s AND d
	---------------
	0   0      0
	0   1      0
	1   0      0		See example
	1   1      1

 To use this table the combination of bits to be ANDed together should be
looked up in the first two coloums. The result of the operation can be
found in the third coloum. For example:

	1 AND 0 = 0  ( Third row in above example, marked )

 Here are the truth tables for the other operators. I will explain how to
apply these operations shortly.

 OR truth table

	s   d   s OR d
	0   0     0
	0   1     1
	1   0     1
	1   1     1

 EOR truth table

	s   d   s EOR d
	0   0      0
	0   1      1
	1   0      1
	1   1      0

 NOT truth table

	s   NOT s
	0    1
	1    0

 Note that NOT operates on only one bit. It changes the state of the bit,
so a set bit is cleared and a clear bit is set.

 The 68000 instructions that correspond to the above operators work on
buytes, words or long words. The source and destination must both be the
same size and the result is generated in a bitwise fashion. This means that
bit 0 of the result is obtained by applying the operator to bit 0 of the
source and destination. The same is true for bit 1, bit 2, bit 3 .....

 AND
 ---

 Syntax		AND.s  <ea>,Dn
	  or	AND.s  Dn,<ea>

	 where .s can be .b .w or .l

 Logical AND. This instruction preforms a bitwise AND operation on the
source and destination. The result is stored in the destination.

 Consider ANDing the two words 1011011011110111 and 1100111101011010. This
can be done as follows:

 s      = 1 0 1 1 0 1 1 0 1 1 1 1 0 1 1 1

 d      = 1 1 0 0 1 1 1 1 0 1 0 1 1 0 1 0

s AND d = 1 0 0 0 0 1 1 0 0 1 0 1 0 0 1 0

 A program to achieve this would be:

	move.w	#%1011011011110111,d0
	move.w	#%1100111101011010,d1
	and.w	d0,d1
	display_d1
	rts

 The AND instruction is usually used to mask out any unwanted bits from a
value. For instance the DIVU instruction produces a quotient and a
remainder in the result. If we are not interested in the remainder we can
mask it out by ANDing it with 0's since ANDing with 0 always results in 0. We
would need to preserve the quotient however and this could be achieved by
ANDing it with 1's since 1 anded with any bit will leave the bit
unaffected. The remainder is stored in the high word of the result and the
quotient in the low word, so ANDing the result with
000000000000000011111111111111111 will mask out the remainder. Obviously we
do not want to be writing long binary values like this in our programs, so
we could convert this to hex first. Each set of 4 binary digits can be
converted into a hex digit as shown in the table below:

 binary		hex
  0000		 0
  0001		 1
  0010		 2
  0011		 3
  0100		 4
  0101		 5
  0110		 6
  0111		 7
  1000		 8
  1001		 9
  1010		 A
  1011		 B
  1100		 C
  1101		 D
  1110		 E
  1111		 F

 So by writing our binary value into blocks of 4 digits we can easily
convert it to hex:

 0000 0000 0000 0000 1111 1111 1111 1111

  0    0    0    0    F    F    F    F

 The required hex value will be $0000ffff. Another abreviation we can use
is to ommit leading 0's from the value, so $ffff will in fact suffice.

 Here is an example. To see it in action assemble it to memory using Devpac
. Next select the Debug option. You will see three windows. The top window
displays the contents of all the internal registers, the bottom left shows
the program being debugged and the bottom right shows the contents of
memory. If you are not familiar with using this option I suggest you read
the manual NOW. Press CTRL z , this executes 1 instruction of your program.
Check this by looking at the contents of register d0 in the top window. Now
press CTRL z two more times and the division will be preformed. The result
can be seen by looking at the contents of register d1 in the top window.
See how both quotient ( 2 ) and remainder ( 1 ) are stored in the register.
Now press CTRL z again, the remainder has been masked out and is no longer
in register d1. Pressing CTRL z once more will return you to Genam.

start		moveq.l		#2,d0
		moveq.l		#5,d1
		divu		d0,d1
		and.l		#$ffff,d1
		rts

 This example can be found on this disc.

 Do you see how useful the Debug option is for investigating exactly what a
program is doing. It is also a priceless tool for finding out where you
have made mistakes. I suggest you get you manual out and learn a bit more
about Monam, I will answer any questions you may have.

 ANDI
 ----

 Syntax 	andi.s	#data,<ea>

 Exactly the same as AND except you do not have to use a data register. You
can for instance AND an immediate value with data stored in memory.

 OR
 --

 Syntax		or.s	<ea>,Dn
	 or	or.s	Dn,<ea>

	where .s can be .b .w or .l

 This instruction preforms a bitwise OR of the source on the destination
and stores the result in the destination. OR can be used to set bits in the
destination. Oring with 0's has no affect on the destination while ORing
with 1's will set the corresponding bits in the destination, regardless of
their initial state. For example ORing any value with $1 will make the
value odd.

 ORI
 ---

 Syntax		ori.s	#data,<ea>

 Exactly the same as OR except a dtat register does not have to be used.

 NOT
 ---

 Syntax		not.s	<ea>

	where .s can be .b .w or .l

 NOT complements all bits in the source and stores the result in the
source. Complement means all set bits are cleared and all clear bits are
set.

 eg  NOT 00110101 = 11001010

 EOR
 ---

 Syntax		eor.s	Dn,<ea>

	where .s can be .b .w or .l

 This instruction preforms a bitwise Exclusive OR of the source on the
destination and stores the result in the destination. This instruction is
often used to calculate checksums of data blocks by EORing a starting value
with each successive item in the data list.

 Note that by preforming the same EOR on a value twice, you end up with the
value you started with. Assemble and Debug the following example as
explained above to see what I mean:

start		moveq.l		#0,d0
		move.l		d0,d1
		move.w		#$2a0c,d0
		move.w		#$5432.d1
		eor.w		d1,d0
		eor.w		d1,d0
		rts

 After the second eor instruction, register d0 will again contain the value
$2A0C.

 EORI
 ----

 Syntax		eori.s		#data,<ea>

 Exactly the same as the EOR instruction except a data register does not
have to be used.

 

 The End
 -------

 Most of the more commonly used instructions have now been looked at very
briefly and it is time to move on. The next article in this series will be
concerned with actual programming so you will be able to start writing
programs that do something.
		


