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! :)
stwx r5, r4, r3 ;)
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
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))
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.
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.
This is the truth table of XOR, where C is the result of A xor B.
Basically, when B is true the bit in A will be the opposite of A (!A).
Ohh... I understand that now. I'll just need to practice it.
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.
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?
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?
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.
I found these on google... lol. Guess I'm not skilled at googling! :D
With the mask, did I do the correct thing apart from OR?
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.
Thanks... So where do you use NOT anyway?
Quote from: dcx2 on November 30, 2010, 07:01:31 PM
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)"
Also this:
!A | !B == !(A & B)
So in words: " if it is (not rainy) or (not sunny), then it is not (rainy and sunny)"
Quote from: doomkaiber001 on November 30, 2010, 07:06:17 PM
Thanks... So where do you use NOT anyway?
NOT is used to tell that something is the opposite of something. (true becomes false and false becomes true)
Or did I misunderstood your question?
You answered part of the question :) . The other part is what I could use it for.
It's hard to just explain when you need something like that. It's more like...you should understand what a unary operator is, as opposed to a binary operator. Or ternary operator, but there aren't very many of those! It's a concept that is used elsewhere as a building block. For instance, there's a NAND operation; NOT AND. !(A&B) It flips the output of the truth table for an AND. (BTW, we typically use Q for output)
A B Q = NAND
0 0 1
0 1 1
1 0 1
1 1 0
NAND is very important with computers because you can make any operation out of NAND gates and it's very easy to make bits of silicon calculate NAND operations.
Oh, so does not (if in a truth table with or) only when there is a zero;
A B C
0 0 1
0 1 1
1 0 1
1 1 0
NOT is a unary operator. That is, it takes *one* operand, and inverts it, and makes it the output.
NOT A = Q
A Q
0 1
1 0
AND, OR, XOR, NAND, etc are binary operators. They take *two* operands.
A AND B = Q
A B Q
0 0 0
0 1 0
1 0 0
1 1 1
In some higher level programming languages, there is also a ternary operator that takes three operands. But they're rarely used.
Here, use this digital logic page. It has pretty pictures you can click on to make it do stuff.
http://www.play-hookey.com/digital/basic_gates.html
http://www.play-hookey.com/digital/derived_gates.html
Oh! So its the opposite of that it would usually become? It's Inverted!