a few surprisingly nuanced tweaks to task 8
Thanks sejo for the feedback.
This commit is contained in:
parent
b16ece5ae8
commit
189bd674a0
32
mu.md
32
mu.md
|
@ -101,17 +101,20 @@ Other miscellaneous restrictions:
|
||||||
by being written to a memory location. When you need that sort of thing,
|
by being written to a memory location. When you need that sort of thing,
|
||||||
use a `handle` instead.
|
use a `handle` instead.
|
||||||
|
|
||||||
## Primitive statement types
|
## Operations on simple types
|
||||||
|
|
||||||
These usually operate on variables with 32-bit types, with some restrictions
|
We'll now survey a long list of statement forms that operate on 32-bit types.
|
||||||
noted below. Most instructions with multiple args require types to match.
|
Most of these are primitives, but some are also implemented as functions
|
||||||
|
(which have slightly different rules as mentioned up top). Most instructions
|
||||||
Some notation for describing statement forms:
|
with multiple args require types to match. Various operations have other
|
||||||
- `var/reg` indicates a variable in some register. Sometimes we require a
|
restrictions which we'll note below, using the following notation:
|
||||||
variable in a specific register, e.g. `var/eax`.
|
- `var/reg` indicates a variable in some register. Where we require a
|
||||||
- `var/xreg` indicates a variable in some floating-point register.
|
variable in a specific register, we'll mention it explicitly. E.g.
|
||||||
- `var` without a `reg` indicates either a variable on the stack or
|
`var/eax`.
|
||||||
|
- `var/xreg` indicates a variable in some floating-point register `xmm_`.
|
||||||
|
- `var` without a `reg` indicates either a variable on the stack, or
|
||||||
dereferencing a variable in a (non-floating-point) register: `*var/reg`.
|
dereferencing a variable in a (non-floating-point) register: `*var/reg`.
|
||||||
|
- `var: type` indicates a variable that must satisfy some type constraint.
|
||||||
- `n` indicates a literal integer. There are no floating-point literals.
|
- `n` indicates a literal integer. There are no floating-point literals.
|
||||||
|
|
||||||
### Moving values around
|
### Moving values around
|
||||||
|
@ -154,7 +157,8 @@ there's a few ways to convert non-float values in general-purpose registers.
|
||||||
var/xreg <- convert *var2/reg2
|
var/xreg <- convert *var2/reg2
|
||||||
```
|
```
|
||||||
|
|
||||||
Correspondingly, there are ways to convert floats into integers.
|
Correspondingly, there are ways to convert floats into integers, with and
|
||||||
|
without rounding.
|
||||||
|
|
||||||
```
|
```
|
||||||
var/reg <- convert var2/xreg2
|
var/reg <- convert var2/xreg2
|
||||||
|
@ -170,15 +174,13 @@ Still, the absence of fractional literals is an annoyance. Mu provides some
|
||||||
helpers to mitigate it somewhat:
|
helpers to mitigate it somewhat:
|
||||||
|
|
||||||
```
|
```
|
||||||
result/xmm0 <- rational nr: int, dr: int
|
result/xmm0 <- rational numerator: int, denominator: int
|
||||||
fill-in-rational out: (addr float), nr: int, dr: int
|
fill-in-rational out: (addr float), numerator: int, denominator: int
|
||||||
```
|
```
|
||||||
|
|
||||||
These are functions, so the inouts have fewer restrictions while the outputs
|
These are functions, so the inouts have fewer restrictions while the outputs
|
||||||
have more. The inouts can be registers, or memory, or even literals. The
|
have more. The inouts can be registers, or memory, or even literals. The
|
||||||
output for `rational` _must_ be in register `xmm0`. The `:` notation describes
|
output for `rational` _must_ be in register `xmm0`.
|
||||||
type constraints; `nr` and `dr` are required to be integers, while `out` must
|
|
||||||
be an address to a float.
|
|
||||||
|
|
||||||
### Comparing values
|
### Comparing values
|
||||||
|
|
||||||
|
|
|
@ -287,20 +287,20 @@ Run `translate` (or `translate_emulated`) as usual. Use your runbook from Task
|
||||||
|
|
||||||
All our variables so far have had type `int` (integer), but there are limits
|
All our variables so far have had type `int` (integer), but there are limits
|
||||||
to what you can do with just whole integers. For example, here's the formula
|
to what you can do with just whole integers. For example, here's the formula
|
||||||
a visitor to the US will require to convert a distance on a road sign to
|
a visitor to the US will require to convert distances mentioned on road signs
|
||||||
kilometers:
|
from miles to kilometers:
|
||||||
|
|
||||||
```
|
```
|
||||||
distance * 1.609
|
distance * 1.609
|
||||||
```
|
```
|
||||||
|
|
||||||
Write a function to perform this conversion. Some starting points:
|
Write a function to perform this conversion. Some starting points:
|
||||||
- reread [the section on variables and registers](https://github.com/akkartik/mu/blob/main/mu.md#variables-registers-and-memory)
|
* Reread [the section on variables and registers](https://github.com/akkartik/mu/blob/main/mu.md#variables-registers-and-memory)
|
||||||
with special attention to the `float` type.
|
with special attention to the `float` type.
|
||||||
- read [the section on floating-point arithmetic](https://github.com/akkartik/mu/blob/main/mu.md#floating-point-arithmetic).
|
* Read [the section on floating-point arithmetic](https://github.com/akkartik/mu/blob/main/mu.md#floating-point-arithmetic).
|
||||||
- One wrinkle is that the x86 instruction set doesn't permit literal
|
* One wrinkle is that the x86 instruction set doesn't permit literal
|
||||||
fractional arguments. So you'll need to _create_ 1.609 somehow. Relevant is
|
fractional arguments. So you'll need to _create_ 1.609 somehow. See the
|
||||||
[the section on moving values around](https://github.com/akkartik/mu/blob/main/mu.md#moving-values-around).
|
sub-section on moving values around under [operations on simple types](https://github.com/akkartik/mu/blob/main/mu.md#operations-on-simple-types).
|
||||||
|
|
||||||
This task has four source files in the repo that reveal more and more of the
|
This task has four source files in the repo that reveal more and more of the
|
||||||
answer. Start from the first, and bump down if you need a hint.
|
answer. Start from the first, and bump down if you need a hint.
|
||||||
|
|
Loading…
Reference in New Issue