2

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
Thor
  • 9,638
  • 15
  • 62
  • 137
  • This very statement does not have enough details to tell about mutual precedence of `AND` and `OR`. From this statement all 3 possible outcomes are equally possible. – zerkms Jun 13 '17 at 04:06
  • @zerkms thanks for your comment. So am I right in saying that in assembly language, AND and OR operator has the same precedence and if everything else been equal, the leftmost AND/OR operator will be accessed first? – Thor Jun 13 '17 at 04:10
  • 3
    Must be about expressions of particular assembler, so the best way to find information is to read documentation of particular assembler used, as it can have any operator priorities, or it may not even have and/or/not operators, if it is some very simple assembler. Irvine32 library is often used together with MSCC and MASM, so I guess MASM documentation is the best place. If you would talk about x86 instructions, then there's no precedence needed, because they will execute in the order how the code is designed, and only single instruction is executed at time. – Ped7g Jun 13 '17 at 08:58

2 Answers2

8

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.

Margaret Bloom
  • 41,768
  • 5
  • 78
  • 124
  • Thanks for posting this answer. I had left this tab open with intention of coming back to do it, but now I don't have to. I couldn't find anything in the official documentation giving the actual precedence of these operators, either, so the empirical testing will have to do. – Cody Gray - on strike Jun 14 '17 at 01:17
  • Thank you @Cody! I was actually inspired by [your comment](https://stackoverflow.com/questions/44512137/x86-processor-assembly-language-and-and-or-operator-precedence/44520885?noredirect=1#comment76029973_44512316) when deciding if posting another answer. – Margaret Bloom Jun 14 '17 at 07:54
4

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 tag wiki for links to manuals for some assemblers, and other good documentation, if Google doesn't turn them up easily.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
RainingComputers
  • 554
  • 6
  • 17
  • Thank you so much for the detailed explanation! – Thor Jun 13 '17 at 04:22
  • @Captain America No problem. – RainingComputers Jun 13 '17 at 04:23
  • 1
    @CaptainAmerica Macros however have an HLL complexity to them and this precedence business may be of some significance. I don't and have never used such mechanisms, but if you do it would be wise to confirm. – Shift_Left Jun 13 '17 at 04:27
  • @Shift_Left thanks for the heads up! really appreciate it! – Thor Jun 13 '17 at 04:29
  • @Shift_Left Macros are parsed by the assembler and converted to instructions at the end. they are not really part of the instruction set. Yes, macros may have precedence, but that depends on the assembler you use. – RainingComputers Jun 13 '17 at 04:43
  • 2
    No, the best place to look up this kind of information is assembly references, manuals etc. A search engine that prefers certain content (many times things written by people who don't know things) and shows different results to different people isn't really something to be advertised. The actual answer is very good :) – Sami Kuhmonen Jun 13 '17 at 05:22
  • 2
    Macros aren't necessarily converted to instructions. [`NOT`, `AND`, and `OR` operators in MASM](https://msdn.microsoft.com/en-us/library/94b6khh4.aspx) are often applied to constants/literals, and the assembler does assemble-time bit-twiddling using a defined precedence order. As such, regarding the assembler-defined operators discussed in the question, this entire answer is wrong. – Cody Gray - on strike Jun 13 '17 at 10:37