1

In my College's documentation on 8086 Assembly is the following example:

TEST AL, 16      ; tests the 5th bit's state

Is this at all correct given what the TEST instruction does? It sets flags based on AL & 16. How does that test the 5th bit?

NOTE: there's no previously mentioned value to AL, just exactly what's shown here, so I assume this has to work in the general case.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
jagnelo
  • 47
  • 1
  • 7
  • Actually I just realized I may have been too harsh here. You didn't say *what* your understanding of the TEST instruction was, so I assumed that was what the question was really about. If the main point of your question was how and-with-`0x10` tests the 5th bit, then please [edit] to clarify that. I'll probably remove my downvote. (I'm not trying to be a jerk, I'm just trying to discourage asking new questions without searching first, because duplicates dilute the search results for future readers.) – Peter Cordes Sep 10 '16 at 01:35
  • I appreciate your efforts to keep this forum clean and thank you for pointing out the fault in this post that could easily lead to seeing it as a lazy duplicate. If any other edits are needed, please do say it. Thank you! – jagnelo Sep 11 '16 at 10:59
  • 1
    Adding a note at the end is much less useful than changing the original text so it's not unclear in the first place. I did it for you to show you what I meant, hope that makes it clear and helps you write better questions in the future. :) The hard part of asking questions is quickly making it clear what you know and what you need an explanation for, especially when it's a beginner question. – Peter Cordes Sep 11 '16 at 12:29
  • I've had my share of "trouble" on this forum for not being able to quickly get to the point, so i must say i really appreciate your help. Thank you, once again. – jagnelo Sep 13 '16 at 13:49

1 Answers1

7

16 in decimal is 10000 in binary. Notice the fifth bit from the right is set, and that it is the only one.

TEST is bitwise AND. The result of a bitwise AND operation is a value that has 1's wherever there was a 1 in both original values. For example:

a       = 1010 1110
b       = 0110 1011
a AND b = 0010 1010

Supposing a was AL and b was 16:

AL        = xxxx xxxx
16        = 0001 0000
AL AND 16 = 000y 0000

The result can be either 0, if the fifth bit of AL is 0, or it can be 16, if the fifth bit of AL is 1. If the instruction using the result of the TEST instruction was, for example, a conditional jump, like JNZ, then it does in fact 'test' if the fifth bit of AL is set and acts according to the result.

To be more precise, the result is used indirectly – only various flags are set. In this case, the ZF flag is relevant. Depending on the result of the bitwise AND operation, ZF is either set to 0 if the result was 0 or it is set to 1 if the result was not 0.

Aurel Bílý
  • 7,068
  • 1
  • 21
  • 34