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?
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.
You can talk about precedence only when you have an equation/statement that has multiple operators one line.
bool a = True && False || False && True
In most cases the AND operator has higher precedence than the OR operator, hence the AND operators are taken care of first then the OR operator. Hence you get the answer 'a = False'.
In assembly, you can't have equation/statement that have multiple operators in one line. There are only instructions. Like this (x86 NASM syntax):
and ax, bx ;ax = ax & bx (bitwise, unlike C &&)
Hence there is no such thing as 'precedence' in assembly. It all depends on how YOU write the program. If you choose to do the OR operator first, you can, because there are no equations/statements, only instructions.
The best place to look up this kind of information is Google. :) See the x86 tag wiki for links to manuals for some assemblers, and other good documentation, if Google doesn't turn them up easily.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With