mirror of
https://github.com/pkivolowitz/asm_book.git
synced 2026-06-21 03:36:49 +08:00
finished for
This commit is contained in:
parent
35e49fbcd2
commit
1cdc7f4c92
1 changed files with 64 additions and 6 deletions
|
|
@ -81,13 +81,15 @@ Now let's add a `continue` to the code block, dividing it in two.
|
|||
|
||||
```c
|
||||
for (long i = 0; i < 10; i++) {
|
||||
// CODE BLOCK "A"
|
||||
if (i == 5)
|
||||
continue;
|
||||
// CODE BLOCK "B"
|
||||
// CODE BLOCK "A"
|
||||
if (i == 5)
|
||||
continue;
|
||||
// CODE BLOCK "B"
|
||||
}
|
||||
```
|
||||
|
||||
Upon encountering a `continue`, control branches to the post step of a `for`, or with a `while` to the decision test to keep going.
|
||||
|
||||
Here is what we would need to write to support a `continue` if the "conventional" ordering were used with the decision evaluation at the top:
|
||||
|
||||
```asm
|
||||
|
|
@ -147,8 +149,64 @@ Once again, the code moving the post step and decision evaluation to the bottom
|
|||
|
||||
## Implementing a `break`
|
||||
|
||||
LEFT OFF HERE
|
||||
The implementation of `break` is very similar to that of `continue`. Upon encountering a `break`, control branches to the instruction beyond the bottom of the loop. There is no difference in implementation of `break` for `while` loops and `for` loops.
|
||||
|
||||
```c
|
||||
for (long i = 0; i < 10; i++) {
|
||||
// CODE BLOCK "A"
|
||||
if (i == 5)
|
||||
break;
|
||||
// CODE BLOCK "B"
|
||||
}
|
||||
```
|
||||
|
||||
In assembly language, a `break` is implemented by a branch to beyond the end of the loop (either `for` or `while`).
|
||||
|
||||
```asm
|
||||
// Assume i is implemented using x0 // 1
|
||||
// 2
|
||||
mov x0, xzr // 3
|
||||
b 3f // 4
|
||||
// 5
|
||||
1: // 6
|
||||
// 7
|
||||
// CODE BLOCK "A" // 8
|
||||
// 9
|
||||
// if (i == 5) // 10
|
||||
// continue // 11
|
||||
// 12
|
||||
cmp x0, 5 // 13
|
||||
beq 4f // 14
|
||||
// 15
|
||||
// CODE BLOCK "B" // 16
|
||||
// 17
|
||||
2: add x0, x0, 1 // 18
|
||||
3: cmp x0, 10 // 19
|
||||
blt 1b // 20
|
||||
// 21
|
||||
4: // 22
|
||||
```
|
||||
|
||||
Compare `line 14` of the `break` example to the same line in the `continue` example.
|
||||
|
||||
## Summary
|
||||
|
||||
`for` loops typically contain code ordering different from what one might expect. This is done to save an instruction within the loop. While this doesn't sound like much, consider the case where the loop is executed billions of times. In this case, saving one instruction per loop prevents the execution of a billion instructions. The shorter the code block is, the more important it is to save one instruction from within the loop.
|
||||
`for` loops typically contain code ordering different from what one might expect. This is done to save an instruction within the loop. While this doesn't sound like much, consider the case where the loop is executed billions of times. In this case, saving one instruction per loop prevents the execution of a billion instructions.
|
||||
|
||||
The shorter the code block is, the more important it is to save one instruction from within the loop.
|
||||
|
||||
The discussion of `break` and `continue` is found here, as `for` loops are slightly more complicated than `while` loops due to the post step. However, the ideas presented here translate to the `while` in a straight forward manner,
|
||||
|
||||
## Questions
|
||||
|
||||
### 1
|
||||
|
||||
(T | F) Given that both the `while` and `for` are based on `if` statements and branches, any `for` loop can be converted to a `while` and vice versa?
|
||||
|
||||
Answer: True - `for` and `while` are interchangeable.
|
||||
|
||||
### 2
|
||||
|
||||
Is there a rule of thumb that will tell you when to use a `while` and when to use a `for`? If so, what is it?
|
||||
|
||||
Answer: Yes! When you know in advance how many times you will loop, use a `for`. If you don't know in advance, use a `while`.
|
||||
|
|
|
|||
Loading…
Reference in a new issue