computer engineering.

profileAlkhawaa
lctr180.pdf

3/20/2020 Lctr 18 for CEC 320 p. 1

4.1.4 The if-then-else construct

Note: due to the existence of the compound OR expression, the conditional execution approach (Asm Prog 2 above) is not so advantageous. Hence, we will only use the conditional branch approach.

The if-then-else construct with compound OR expression

3/20/2020 Lctr 18 for CEC 320 p. 2

4.2 The for loop to master in both C and assembly

Note: 1. The condition_expression is tested before the block of the intended code is executed,

which means that the block of code may not have a chance to run. We need to update the loop-counting variable in each pass after executing the block of code.

2. We prefer to use Approach 2 due to its clean structure. Yet, Approach 1 is slightly more efficient.

3. Approach 1 is similar to the do-while loop we will see soon.

3/20/2020 Lctr 18 for CEC 320 p. 3

4.3 The while loop to master in both C and assembly

Note: the condition_expression for the while loop is also tested before executing the block of intended code, in the same way as the for loop. We also need to update the loop-counting variable in each pass after executing the block of the intended code.

We can see from Tasks 5 and 7 of 170_conditional_execution that the implementation of for loop and a while loop can be exactly the same in assembly.

3/20/2020 Lctr 18 for CEC 320 p. 4

4.4 The do-while loop to master in both C and assembly

Note: the condition_expression is tested after executing the block of intended code, which means that the block of code will be executed at least once.

The do-while loop

The while loop

Note that the assembly code for the do-while loop is slightly more compact/efficient than that of the while loop.

3/20/2020 Lctr 18 for CEC 320 p. 5

4.5 The continue and break statements to master in C and to know in assembly

The continue and break statements affect the flow of a loop in different ways: • Continue = jump to the end of the loop and continue the next pass of the loop • Break = break (jump out of) the loop

3/20/2020 Lctr 18 for CEC 320 p. 6

3/20/2020 Lctr 18 for CEC 320 p. 7

4.6 The switch statement to master in C

The switch statement can be seen as a special case of the if-then-else statement, with the following caution:

The ARM Cortex-M processors use a special Branch Table to handle the addresses of the case blocks. See the textbook for details. We just need to be informed of the BranchTable approach; no need to master them at this stage of learning. As will be seen in a later chapter, we can always call C code, such as the switch statement, in the assembly code.

3/20/2020 Lctr 18 for CEC 320 p. 8

4.7 The IT (If-Then) instruction to know in assembly

The IT instruction can handle a block of code of "then" and "else". The number of instructions in the block is at most 4 using the following syntax:

Notes: * While the IT construct is important for the GNU assembly, it is not as important for the ARM assembly---the latter can add this construct automatically. * The IT version of the instructions may be more compact than the non-IT version. See the example of ADD in line 196 and ADDEQ in line 202 above.