task: conditional execution

This commit is contained in:
Kartik K. Agaram 2021-10-26 23:57:25 -07:00
parent a3f73278b1
commit 20cca772b8
3 changed files with 67 additions and 0 deletions

View File

@ -421,3 +421,34 @@ answer. Start from the first, and bump down if you need a hint.
* tutorial/task10-hint1.mu
* tutorial/task10-hint2.mu
* tutorial/task10-hint3.mu
## Task 11: conditionally executing statements
Here's a fragment of Mu code:
```
{
compare x, 0
break-if-<
x <- copy 0
}
```
The combination of `compare` and `break` results in the variable `x` being
assigned 0 _if and only if_ its value was less than 0 at the beginning. The
`break` family of instructions is used to jump to the end of an enclosing `{}`
block, skipping all intervening instructions.
To prepare for this task, read the sections in the Mu reference on
[`compare`](https://github.com/akkartik/mu/blob/main/mu.md#comparing-values)
and [branches](https://github.com/akkartik/mu/blob/main/mu.md#branches).
Now make the tests pass in `tutorial/task11.mu`. The goal is to implement our
colloquial understanding of the &ldquo;difference&rdquo; between two numbers.
In lay English, we say the difference between the first-place and third-place
runner in a race is two places. This answer doesn't depend on the order in
which we mention the runners; the difference between third and first is also
two.
The section on [integer arithmetic](https://github.com/akkartik/mu/blob/main/mu.md#integer-arithmetic)
is again worth referring to when working on this task.

View File

@ -0,0 +1,22 @@
fn difference a: int, b: int -> _/eax: int {
var result/eax: int <- copy a
result <- subtract b
compare result, 0
{
break-if->=
result <- negate
}
return result
}
fn test-difference {
var result/eax: int <- difference 5, 3
check-ints-equal result, 2, "F - difference works"
result <- difference 3, 5
check-ints-equal result, 2, "F - difference is always positive"
result <- difference 6, 6
check-ints-equal result, 0, "F - difference can be 0"
}
fn main screen: (addr screen) {
}

14
tutorial/task11.mu Normal file
View File

@ -0,0 +1,14 @@
fn difference a: int, b: int -> _/eax: int {
}
fn test-difference {
var result/eax: int <- difference 5, 3
check-ints-equal result, 2, "F - difference works"
result <- difference 3, 5
check-ints-equal result, 2, "F - difference is always positive"
result <- difference 6, 6
check-ints-equal result, 0, "F - difference can be 0"
}
fn main screen: (addr screen) {
}