How should I write such an if
statement in assembly?
if ((a == b AND a > c) OR c == b) { ...
Platform: Intel 32-bit machine, NASM syntax.
Update
For variable types and value, use whatever is more easy to understand. Integers would works fine for me, I guess.
For a simple expression with no side effects (such as the one given), the order of evaluation does not matter(1). That means you can check the "quick" cases first (those that decide with least code executed, c == b
in this case).
In generic assembly, it will be basically something like this (assume a
in ax
, b
in bx
, c
in cx
):
cmp bx, cx ; check b == c?
jeq is_true ; final result is true.
cmp ax, cx ; otherwise b != c, so check a <= c?
jle is_false ; if so, final result is false.
cmp ax, bx ; otherwise a > c, so check a == b?
jeq is_true ; if so, final result is true.
is_false:
; do false bit ; otherwise final result is false.
jmp next
is_true:
; do true bit
next:
; carry on
If there's no false bit, it can be simplified to something like:
cmp bx, cx
jeq is_true
cmp ax, bx
jne next ; no false bit, next statement.
cmp ax, cx
jle next ; no false bit, next statement.
is_true:
; do true bit
next:
; carry on
(1) If the expression contained side-effects then order of evaluation may become important due to the possibility of short-circuiting evaluations. By that, I mean consider an expression such as (pseudo-code):
if a == b and c == d:
do_something()
In that case, once you establish that a
and b
are equal, there is no need to compare c
and d
.
I haven't covered that (other than in this footnote) since:
no source language was specified in the question (making it unclear whether it even has short-circuiting operations); and
the expression appears to have no side effects anyway.