and / or Assembly Commands

Started by doomkaiber001, November 26, 2010, 05:29:45 PM

Previous topic - Next topic

doomkaiber001

Could someone help me... I'm being taught ASM and have came across the commands and (e.g. and r5, r4, r3) and or (e.g. or r5, r4, r3), and I've tried to find out what they mean but just can't. Thanks for any help!  :)

Deathwolf

lolz

megazig

Quote from: doomkaiber001 on November 26, 2010, 05:29:45 PM
Could someone help me... I'm being taught ASM and have came across the commands and (e.g. and r5, r4, r3) and or (e.g. or r5, r4, r3), and I've tried to find out what they mean but just can't. Thanks for any help!  :)

and r5, r4, r3 means
r5 = r4 & r3;
bitwise and of register 4 and register 3 are put into register 5

or r5, r4, r3 means
r5 = r4 | r3;
bitwise or of register 4 and register 3 are put into register 5

doomkaiber001

I understand what it is now:
AND:
r4's value is 3 (0011)
r3's value is 1 (0001)
and r5, r4, r3 (Store 1 into r5 (0011 & 0001 = 0001))

OR:
r4's value is 3 (0011)
r3's value is 1 (0001)
or r5, r4, r3 (Store 3 into r5 (0011 | 0001 = 0011))

dcx2

Yup, it's standard boolean logic.  For more fun, look up XOR too; it can be used to selectively flip bits.  And DeMorgan's law.

Also look up the topic of masking.  Quite often you will see masks used to isolate specific flag bits.

For instance (this is fabricated), Mario might have a state variable.  bit 0 of that state variable is the jump bit.  If bit 0 is set, then Mario is jumping.  You may see either the AND instruction or a RLWINM (Rotate Left With Immediate aNd Mask) used to test this bit before doing conditional branches.

Importantly, many instructions like AND and OR will modify the Condition Register (CR).  This can be used to test if Mario is jumping and if he is, conditionally branch to a different section.  So, if you press the jump button, and Mario is already jumping (bit 0 = 1), then we can't jump again, so branch over the jump code.

doomkaiber001

Quote from: dcx2 on November 28, 2010, 02:24:48 AM
Yup, it's standard boolean logic.  For more fun, look up XOR too; it can be used to selectively flip bits.  And DeMorgan's law.

Also look up the topic of masking.  Quite often you will see masks used to isolate specific flag bits.

For instance (this is fabricated), Mario might have a state variable.  bit 0 of that state variable is the jump bit.  If bit 0 is set, then Mario is jumping.  You may see either the AND instruction or a RLWINM (Rotate Left With Immediate aNd Mask) used to test this bit before doing conditional branches.

Importantly, many instructions like AND and OR will modify the Condition Register (CR).  This can be used to test if Mario is jumping and if he is, conditionally branch to a different section.  So, if you press the jump button, and Mario is already jumping (bit 0 = 1), then we can't jump again, so branch over the jump code.

XOR;
Is this just the same as 'or', but if r3 equals 2, and r4 equals 3, it would be like:
XOR r5, r4, r3 (2 = 0010 / 3 = 0011. 0010 | 0011 becomes 0001)

If there are two 1's, the result will be a 0.

Romaap

This is the truth table of XOR, where C is the result of A xor B.

ABC
110
101
011
000

Basically, when B is true the bit in A will be the opposite of A (!A).

doomkaiber001

Ohh... I understand that now. I'll just need to practice it.

doomkaiber001

#8
De Morgans law. I've looked it up, and this is what I've found;

(A & B) C'      then becomes
A' | B' & C'

I might of got a bit confused on the last part.

Those ' were meant to indicate NOT.

doomkaiber001

#9
Quote from: dcx2 on November 28, 2010, 02:24:48 AM
Yup, it's standard boolean logic.  For more fun, look up XOR too; it can be used to selectively flip bits.  And DeMorgan's law.

Also look up the topic of masking.  Quite often you will see masks used to isolate specific flag bits.

For instance (this is fabricated), Mario might have a state variable.  bit 0 of that state variable is the jump bit.  If bit 0 is set, then Mario is jumping.  You may see either the AND instruction or a RLWINM (Rotate Left With Immediate aNd Mask) used to test this bit before doing conditional branches.

Importantly, many instructions like AND and OR will modify the Condition Register (CR).  This can be used to test if Mario is jumping and if he is, conditionally branch to a different section.  So, if you press the jump button, and Mario is already jumping (bit 0 = 1), then we can't jump again, so branch over the jump code.

Is this what you meant by masking?

0101 0101 0101

Now say you wanted 4bits in the middle to all be 1's, you would apply a mask.

0101 0101 0101
0000 1111 0000    - Mask
0101 1111 0101    - Total

Is that right?


doomkaiber001

NOT;

A  B  C
0  0  0
0  1  0
1  0  1
1  1  0

I think it's when there is a one in column A but not in B?

Romaap

Quote from: doomkaiber001 on November 30, 2010, 06:34:17 AM
Quote from: dcx2 on November 28, 2010, 02:24:48 AM
...

Is this what you meant by masking?

0101 0101 0101

Now say you wanted 4bits in the middle to all be 1's, you would apply a mask.

0101 0101 0101
0000 1111 0000    - Mask
0101 1111 0101    - Total

Is that right?


Nope, a mask is for when you only want some specific bits in a series of bits.
When you want to apply a mask you use AND, but in your example you've used OR.

Quote from: doomkaiber001 on November 30, 2010, 07:16:35 AM
NOT;

A  B  C
0  0  0
0  1  0
1  0  1
1  1  0

I think it's when there is a one in column A but not in B?

This is not correct, you're not supposed to do "A NOT B".
Its supposed to be NOT A (!A  or  ¬A), and its truthtable is just:
(C being the result of NOT A)

A  C
1  0
0  1

So its just the opposite of A.

doomkaiber001

I found these on google... lol. Guess I'm not skilled at googling! :D

doomkaiber001

With the mask, did I do the correct thing apart from OR?

dcx2

Quote from: doomkaiber001 on November 30, 2010, 06:17:36 AM
De Morgans law. I've looked it up, and this is what I've found;

(A & B) C'      then becomes
A' | B' & C'


De Morgan's is, roughly,

!A & !B == !(A | B)

Where ! is the NOT operator, & is the AND operator, | is the OR operator, and == means they're logically equivalent.

Think of it like words.  " if it is (not rainy) and (not sunny), then it is not (rainy or sunny)"

Quote from: doomkaiber001 on November 30, 2010, 06:34:17 AM
Is this what you meant by masking?

0101 0101 0101

Now say you wanted 4bits in the middle to all be 1's, you would apply a mask.

0101 0101 0101
0000 1111 0000    - Mask
0101 1111 0101    - Total

Is that right?

Romaap is correct that masking usually implies using the AND operator to clear bits that we are not interested in (masking = hiding).  However, sometimes it's useful to use the OR operator to set bits, and the operand which contains the bits to set is still referred to as a mask.  But you wouldn't really ever use an OR mask to test for something.