if ( condition ) ----> |
statement1; V
else +--------------+ FALSE
statement2; | condition |--------+
+--------------+ |
| |
| TRUE |
| |
V |
statement1 |
| |
+---------+ |
| |
| +<---------------+
| |
| V
| statement2
| |
| |
+-------->+
|
|
V
Evaluate "condition" (CMP)
FALSE
Branch on the FALSE outcome of "condition" to A ------------+
| |
| TRUE |
| |
V |
"statement1" assembler code |
| |
Branch always to B |
| |
+---------+ |
| |
| |
| +<------------------------------------------------+
| |
| V
A: | "statement2" assembler code
| |
| V
+---------+
|
V
B:
int x;
int y;
if ( x >= y )
y = x;
else
x = y;
Assembler construct for this if-statement:
! get the values into registers to compare....
sethi %hi(x), %l0
ld [%l0 + %lo(x)], %l0 // %l0 = x
sethi %hi(y), %l1
ld [%l1 + %lo(y)], %l1 // %l1 = y
! Now compare them and branch....
cmp %l0, %l1
bl L1 ! Jump over the "then" part to the else part
nop
sethi %hi(y), %l2
st %l0, [%l2 + %lo(y)] // y = %l0 (= x)
ba L2 ! Jump over the "else" part to the end
nop
L1: sethi %hi(x), %l2
st %l1, [%l2 + %lo(x)] // x = %l1 (= y)
L2:
int x;
int y;
int max;
if ( x >= y )
max = x;
else
max = y;
Assembler construct for this if-statement:
sethi %hi(x), %l0
ld [%l0 + %lo(x)], %l0 ! l0 = x
sethi %hi(y), %l1
ld [%l1 + %lo(y)], %l1 ! l1 = y
cmp %l0, %l1
bl L1
nop
sethi %hi(max), %l2
st %l0, [%l2 + %lo(max)] ! max = l0 (x)
ba L2 ! unconditional branch
nop
L1: sethi %hi(max), %l2
st %l1, [%l2 + %lo(max)] ! max = l1 (y)
L2: