Section 03 Part 02 – The AND Instruction “A sane mind should not be guilty of a logical fallacy, yet there are very fine minds incapable of following mathematical demonstrations.” ~Henri Poincare

Introduction

AND – AND logical

This instruction will perform AND logical between the source operand and destination operand, the result is saved to the destination operand.

So what is AND logical?

Let us take two bits, one from the source operand and one from the destination operand, and perform AND logical:

 source operand destination operand Result 0 AND 0 = 0 0 AND 1 = 0 1 AND 0 = 0 1 AND 1 = 1

The idea is that both have to be 1 for the result to be 1.  To understand why it’s called AND, you might need to pretend that 0 is referred to as “false” and 1 is referred to as “true”.  So let’s look at the table again:

 source operand destination operand Result False AND False = False False AND True = False True AND False = False True AND True = True

The description of this logic is; “If the source and the destination is true, then the result is true”, and that is where the name of the logic comes from.

Examples

OK, so down to business, here is an example (we’ll pretend that d0 contains 01234567):

 andi.b    #\$E2,d0

This instruction will AND the byte E2 with the byte 67 inside d0.  Since this is a binary instruction, E2 and 67 in binary is 1110 0010 and 0110 0111.

 E2 1 1 1 0 0 0 1 0 AND AND AND AND AND AND AND AND AND 67 0 1 1 0 0 1 1 1 = = = = = = = = = 62 0 1 1 0 0 0 1 0

As you can see in the table, both the source and the destination, must be set to 1 for the result to be 1.

E2 AND 67 = 62

And so, 62 is saved into d0, d0 now contains 01234562.

You can AND one register to another:

 and.w     d0,d1

You can AND on memory in various ways too:

 andi.w    #\$07FF,\$00004000           and.w     d0,\$00004000           and.l     \$00004000,d7

And you can do it on memory using the address registers:

 andi.w    #\$07FF,(a1)           and.w     d0,(a3)           and.l     (a6),d7

Of course, there are things you cannot do such as AND-ing from one memory directly to another, or directly to an address register:

 and.w     \$00020000,\$0002004E           andi.w    #\$3F10,a0

AND Immediate

You may remember that the “add” and “sub” instructions have this.  If the source operand is “immediate”, you must use the instruction “andi” instead of “and” (where the “i” stands for immediate):

 andi.w    #\$003F,d0

Again, don’t worry if you use just plain “and” instead, as the assembler will turn it into “andi” for you, when it assembles your code.