General > The Workshop

TEST vs BT

(1/2) > >>

16bitPM:
In the book "Assembly Language Step by Step" by Jeff Duntemann, on page 306, he writes the following :

--- Quote ---Here’s something important to keep in mind: TEST is only useful for finding
1 bits. If you need to identify 0 bits, you must first flip each bit to its opposite
state with the NOT instruction. NOT changes all 1 bits to 0 bits, and all 0 bits to 1
bits. Once all 0 bits are flipped to 1 bits, you can test for a 1 bit where you need
to find a 0 bit. (Sometimes it helps to map it out on paper to keep it all straight
--- End quote ---

And just below he dedicates an entire paragraph to "Looking for 0 Bits with BT".

So the thing is: either I don't get it or he's wrong. If I want to test any bit for 0, I just do TEST dst, bitmask. If the zero-flag is set, the tested bits are all 0. So I can definitely test for 0-bits, or did he mean something else?

FORTRANS:
Hi,

Interesting.  Not having that book, I cannot say what he meant.
But you are right, test for zero bits works the way you say it does.
And all need to be zero to return the zero flag set.

And if you test for more that one set bit, you would have to
reverse what the quote says.  As testing for more that one set bit
would return non-zero if any bit was set.  Not just all set.  So NOT
and test for the now zeroed bits would be required.

Right?  Or is he testing only one bit at a time?  In which case he
might want zero to be false and non-zero to be true.  (If so, ick.)

Regards,

Steve N.

NoCforMe:
Wow; I can't believe I didn't even know that this instruction (BT) was available to me, all these years. I'm going to have to use it sometime.

Anyhow, I always use TEST to check for one or more 1 bits, not zero bits. For some reason I've never needed to do that. (Well, I guess I have used a NOT - TEST sequence once or twice to check for zeroes.)

jj2007:

--- Quote from: NoCforMe on October 24, 2022, 07:06:35 AM ---Wow; I can't believe I didn't even know that this instruction (BT) was available to me
--- End quote ---

--- Code: ---Intel(R) Core(TM) i5-2450M CPU @ 2.50GHz (SSE4)

3       cycles for 100 * bt eax, 1
3       cycles for 100 * test eax, 2
4       cycles for 100 * bt ax, 1
11      cycles for 100 * test al, 2

3       cycles for 100 * bt eax, 1
3       cycles for 100 * test eax, 2
4       cycles for 100 * bt ax, 1
11      cycles for 100 * test al, 2

4       cycles for 100 * bt eax, 1
3       cycles for 100 * test eax, 2
4       cycles for 100 * bt ax, 1
11      cycles for 100 * test al, 2

4       cycles for 100 * bt eax, 1
3       cycles for 100 * test eax, 2
4       cycles for 100 * bt ax, 1
11      cycles for 100 * test al, 2

8       bytes for bt eax, 1
9       bytes for test eax, 2
9       bytes for bt ax, 1
6       bytes for test al, 2
--- End code ---

hutch--:
If you bother to look up the authoritive source, the mnemonic TEST has many uses.

test eax, eax
jz label

or

test rax, rax
jz label