if ( condition )
statement1;
else
statement2;
|
Meaning:
|
Flow chart of the if-else statement
if ( condition ) ----> |
statement1; V
else +--------------+ FALSE
statement2; | condition |--------+
+--------------+ |
| |
| TRUE |
| |
V |
statement1 |
| |
+---------+ |
| |
| +<---------------+
| |
| V
| statement2
| |
| |
+-------->+
|
|
V
|
Evaluate "condition" (with cmp)
FALSE
Branch on the FALSE outcome of "condition" to label A: ----+
| |
| (TRUE) |
| |
V |
"statement1" assembler code |
|
Branch (always) to label B: --------+ |
| |
| |
A: "statement2" assembler code <--------------------------+
| |
| |
B: +<--------------------------+
|
V
|
The following program fragment will store the larger value of x and y in the variable max:
int x;
int y;
int max;
if ( x >= y )
max = x;
else
max = y;
|
The flow chart of the above program is:
|
The assembler code that implements this high level program is:
/* --------------------------------------------------
if ( x >= y )
max = x;
else
max = y;
-------------------------------------------------- */
.text
main:
movw r0, #:lower16:x
movt r0, #:upper16:x // r0 = addr(x)
ldr r0, [r0] // r0 = x
movw r1, #:lower16:y
movt r1, #:upper16:y // r1 = addr(y)
ldr r1, [r1] // r1 = y
cmp r0, r1 // compare x ? y
blt else // Branch to "else" if x < y
// x >= y ---> max = x
movw r2, #:lower16:max
movt r2, #:upper16:max // r2 = addr(max)
str r0, [r2] // max = x
b ifEnd // Skip over the else part !!!
else:
// x < y ---> max = y
movw r2, #:lower16:max
movt r2, #:upper16:max // r2 = addr(max)
str r1, [r2] // max = y
ifEnd:
/* --------------------------------------------------
Begin of the permanent program variables
-------------------------------------------------- */
.data
x: .4byte 7 // Try a value > y
y: .4byte 10
max: .4byte 0
.end
|
How to run the program:
|