Search code examples
assemblyx86operatorsintelirvine32

x86 processor assembly language AND and OR operator precedence


I'm currently studying assembly language by following Kip Irvine's book "Assembly language for x86 processor".

In the book, the author stated that

the NOT operator has the highest precedence, followed by AND and OR

I know that in Java, && (and) operator has higher precedence than || (or) operator, but judging from what the author said, it seems to be that in assembly language, AND and OR operator seem to have the same precedence.

Is my understanding correct?

Also, where is the best place to look for this kind of information?


Solution

  • Irvine's book uses MASM as the reference assembler.
    The author is talking about the MASM operators1 - these operators are supported only for the benefit of us humans.
    They let us performs arithmetic on immediate and constants but the expression they are used in must ultimately resolve to a value at assembly time.

    aConstant EQU 35
    
    mov edx, NOT 1                       ;Same as mov edx, 0fffffffeh
    mov edx, aConstant  AND (NOT 3)      ;Same as mov edx, 32
    

    If you attempt to use one of these operators with a non-constant value, MASM will complain accordingly:

    error A2026:constant expected

    Note that here AND, OR and NOT are not the homonymous instructions, there is an ambiguity - MASM can disambiguate from the context:

    mov ax, STATUS_REG OR ENABLE_BIT      ;Operator
    or ax, bx                             ;Instruction
    

    Instructions are executed sequentially from the human perspective, so they don't need any precedence order.
    The operators form expressions and an ordering must be given.

    I cannot find such order in the MASM docs, so I tested it directly by assembling this code

      ;Results only refer to bit0 of the immediate.
      ;NOT 0 is 0ffffffffh but I consider it a 1 by the virtue of above.
    
      mov edx, 0 AND (1 OR 1)     ;0
      mov edx, (0 AND 1) OR 1     ;1
      mov edx, 0 AND 1 OR 1       ;? -> 1
    
      mov edx, (NOT 0) AND 0      ;0
      mov edx, NOT (0 AND 0)      ;1
      mov edx, NOT 0 AND 0        ;? -> 0
    
      mov edx, (NOT 0) OR 1       ;1
      mov edx, NOT (0 OR 1)       ;0
      mov edx, NOT 0 OR 1         ;? -> 1
    

    The ? -> lines are the actual tests, with the results gathered from inspecting the binary executable produced.
    This proves that the order is (from highest to lower): NOT, AND and OR.

    This is, of course, in accordance with the usual laws of logic and Irvine itself as I believe the quote was to be interpreted as:

    the NOT operator has the highest precedence, followed by AND and then followed by OR


    1 Link is a courtesy of Cody Gray's comment.