Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use the TEST instruction to see if two bits are set?

How could you use the TEST instruction (or a sequence of TEST instructions) to see if bits zero and four in the AL register are both set to one? How would the TEST instruction be used to see if either bit is set? How could the TEST instruction be used to see if neither bit is set? I use the emu8086 assembler.

like image 644
Jαcκsσи Avatar asked Dec 23 '22 19:12

Jαcκsσи


2 Answers

(See my answer to a similar question.)

Using TEST, you can mask against 17 (= 0b10001, i.e. bits zero and four are set).

TEST AL, 17

Then:

  • ZF will be 0 if at least one bit (either bit zero or bit four) was set
  • PF will be 1 if either two or zero bits are set

So after TEST:

  • not ZF and PF - both bits set
  • not ZF but not PF - one bit set
  • ZF - neither bit set

Here is a full example:

    TEST AL, 17
    JZ none_set
    JPE both_set
one_set:
    ...
none_set:
    ...
both_set:
    ...

Please note that this only happens to work in the case of checking for 2 bits, and specifically in the least significant byte of the masked result.

Testing for 3 or more bits would best be done with x & mask == mask using AND and CMP. (That would be efficient for the 2-bit case as well, letting you use only one branch after a couple other instructions, instead of maybe 2 branches after one TEST).

like image 174
Aurel Bílý Avatar answered Feb 02 '23 00:02

Aurel Bílý


How could you use the TEST instruction (or a sequence of TEST instructions) to see if bits zero and four in the AL register are both set to one?

You can use the Parity Flag trick as in the answer by Aurel Bílý -- though as noted that only works if both bits are in the low 8 bits.

Here's a simple way with multiple test instructions:

    test al, 16
    jz not_both_set
    test al, 1
    jz not_both_set
both_set:
    ...
not_both_set:

Equivalently, the last conditional branch may be inverted:

    test al, 16
    jz not_both_set
    test al, 1
    jnz both_set
not_both_set:
    ...
both_set:

Here's another way that does not use test but instead uses a temporary register, and does an and then a cmp on that:

    mov ah, al
    and ah, 17
    cmp ah, 17
    jne not_both_set
both_set:
    ...
not_both_set:

How would the TEST instruction be used to see if either bit is set?

Just provide the mask with both bits set to the test instruction, Zero Flag clear (Non-Zero) means at least one of the bits is set.

How could the TEST instruction be used to see if neither bit is set?

Again use the mask of both bits, Zero Flag set means neither bit is set.

like image 24
ecm Avatar answered Feb 01 '23 23:02

ecm