CS255 Syllabus
The simple if statement

• Program control of the if statement:
```   if ( condition )   ---->            |
statement                        |
+--------------+  FALSE
|  condition   |--------+
+--------------+	|
|		|
| TRUE		|
|		|
V		|
statement		|
|		|
|		|
V		|
+<---------------+
|
V
```

• Assembler construct that realizes the control flow of the if statement is:
```      Evaluate "condition" (CMP)
FALSE
Branch on the FALSE outcome of "condition" to here (A:) ----+
|						  |
| TRUE						  |
|						  |
V						  |
Translate "statement" into assembler code			  |
|						  |
|						  |
V						  |
A:		+<------------------------------------------------+
|
V
```

• Example 1: find the absolute value

```   int x;               Assembler construct for this if-statement:

if ( x < 0 )		MOVE.L x, D0
x = -x;		CMP.L  #0, D0	Compares x against 0
BGE    L1	Skip over "x=-x" when x >= 0

MOVE.L x, D0	(you can omit this, because D0
NEG.L  D0
MOVE.L D0, x

L1: ...
```

The flow chart of the program is:

• Example 2: making sure that y is the largest number

```   int x;               Assembler construct for this if-statement:
int y;
int help;

if ( x > y )		MOVE.L x, D0
{  // Swap x and y	CMP.L  y, D0	Compares x against y
help = x;		BLE    L1	Skip when x <= y
x = y;
y = help;		MOVE.L y, x	Note: we don't need a "help"
}			MOVE.L D0, y	variable to achieve swapping
The data reg. D0 is
the "help" variable...
L1: ...
```

The flow chart of the program is: