if ( condition ) ----> |
statement |
+--------------+ FALSE
| condition |--------+
+--------------+ |
| |
| TRUE |
| |
V |
statement |
| |
| |
V |
+<---------------+
|
V
Evaluate "condition" (CMP)
FALSE
Branch on the FALSE outcome of "condition" to A ------------+
| |
| TRUE |
| |
V |
Translate "statement" into assembler code |
| |
| |
V |
A: +<------------------------------------------------+
|
V
int x; Assembler construct for this if-statement:
if ( x < 0 ) sethi %hi(x), %l0
x = -x; ld [%l0 + %lo(x)], %l0 // reg. l0 = x
cmp %l0, 0 // cmp x against 0
bge L1 // Skip over "x=-x" when x >= 0
nop // DO NOT FORGET the NOP !!!
sethi %hi(x), %l0
ld [%l0 + %lo(x)], %l0 (you can omit this,
because reg. l0
already has x...)
sub %g0, %l0, %l0 // reg l0 = -x
sethi %hi(x), %l1
st %l0, [ [%l1 + %lo(x)]
L1: ...
int x; Assembler construct for this if-statement:
int y;
int help;
if ( x > y ) sethi %hi(x), %l0
{ // Swap x and y ld [%l0 + %lo(x)], %l2 // reg l2 = x
help = x; sethi %hi(y), %l1
x = y; ld [%l1 + %lo(y)], %l3 // reg l3 = y
y = help; cmp %l2, %l3 // comp x against y
} ble L1 // Skip if x <= y
nop // Remember !!!
st %l2, [%l1 + %lo(y)] // y = x value in l0
st %l3, [%l0 + %lo(x)] // x = y value in l1
L1: ...
Assemble program with: as255s sparc-if