mirror of
https://github.com/pkivolowitz/asm_book.git
synced 2026-06-21 01:56:47 +08:00
modifications to pi project
This commit is contained in:
parent
9861cfd8e4
commit
9ec7bbcb17
2 changed files with 68 additions and 7 deletions
|
|
@ -61,8 +61,11 @@ the 64 bit ARM Instruction Set Architecture (ISA).
|
|||
| .... a | [.... Alignment](./section_1/structs/alignment.md) |
|
||||
| .... b | [.... Defining](./section_1/structs/defining.md) |
|
||||
| .... c | [.... Using](./section_1/structs/using.md) |
|
||||
| 8 | [`const`](./section_1/const/README.md)
|
||||
| 9 | [Casting](./section_1/casting/README.md) |
|
||||
| 8 | [`const`](./section_1/const/README.md)
|
||||
| 9 | [Casting](./section_1/casting/README.md) |
|
||||
| 10 | Floating Point |
|
||||
| .... a | [ .... What Are Floating Point Numbers? ](./section1/float/what.md)
|
||||
|
||||
|
||||
## Section 2 - Stuff
|
||||
|
||||
|
|
|
|||
|
|
@ -24,11 +24,11 @@ must multiply by 4 at the end.
|
|||
This will come from the command line as in:
|
||||
|
||||
```text
|
||||
user@comporg:~/pi $ ./a.out 100000
|
||||
~/pi $ ./a.out 100000
|
||||
Executing: 100000 iterations.
|
||||
Hits: 78443
|
||||
Approximation: 3.137720
|
||||
user@comporg:~/pi $
|
||||
~/pi $
|
||||
```
|
||||
|
||||
If the command line argument is *not* given, use a default of 100000.
|
||||
|
|
@ -39,7 +39,8 @@ Remember that all AARCH64 instructions are 32 bits long. An implication of this
|
|||
mov x0, 1000000
|
||||
```
|
||||
|
||||
Since the constant cannot fit along with op codes into four bytes. A way to get around this that comes readily to mind
|
||||
Since the constant cannot fit along with op codes into four bytes.
|
||||
A way to get around this that comes readily to mind
|
||||
is to put the constant in RAM and `ldr` it into a register.
|
||||
|
||||
## Vetting the command line argument
|
||||
|
|
@ -69,12 +70,12 @@ You must seed the RNG in this way. But in assembly language.
|
|||
C and C++ you would do:
|
||||
|
||||
```c++
|
||||
// Produces result between 0 and very very close to 1.
|
||||
// Produces result between 0 and 1.
|
||||
float v = float(rand()) / float(RAND_MAX);
|
||||
```
|
||||
|
||||
You must do this in your program. What value is RAND_MAX? Write a tiny C++ program on the ARM and print out the value. Or, put
|
||||
`RANDMAX` into an IDE and ask the IDE to locate the definition.
|
||||
`RANDMAX` into an IDE and ask the IDE to locate its definition.
|
||||
|
||||
You must write a subroutine (function) which returns a random number in the right range. Call it `randf` so I can find it easily.
|
||||
|
||||
|
|
@ -97,6 +98,63 @@ restored in functions.
|
|||
|
||||
You'll use instructions like `fmul`, `fdiv`, `fsqrt`, `fmov` and `fcmp`.
|
||||
|
||||
## Aliases For Registers
|
||||
|
||||
We have previously advised you to create a "bible" documenting
|
||||
which registers are used for what. Here is another way of aiding
|
||||
you to:
|
||||
|
||||
* better understand your code, and
|
||||
|
||||
* avoid using the wrong register in an instruction.
|
||||
|
||||
The idea is to give specific registers aliases, symbolic names
|
||||
that mean something to your code rather than just a letter and
|
||||
number. Here is an example:
|
||||
|
||||
```asm
|
||||
LOOP_MX .req x19
|
||||
LOOP_CT .req x20
|
||||
HITS .req x21
|
||||
|
||||
RND_MF .req d20
|
||||
DTMP .req d21
|
||||
```
|
||||
|
||||
## Printing FP
|
||||
|
||||
`printf` will be your friend. Like always, the format string address goes in `x0`. A `%f` found in the format string tells `printf` to look in the FP registers starting with `d0` as the first value.
|
||||
|
||||
## Added Challenge
|
||||
|
||||
The program as specified above will print its result only at
|
||||
the end of its computation. If you specify a very large number
|
||||
of loops, this can take a long time. The shell prompt will
|
||||
just sit and you won't know if your program is working or
|
||||
not.
|
||||
|
||||
As an added challenge, add functionality that tests the
|
||||
loop counter and from time to time, such as every 2048
|
||||
loops, prints the results so far.
|
||||
|
||||
Note that this can produce a lot of lines of output. To
|
||||
deal with this, create a single line format string (for
|
||||
`printf()`) that also prints some *old school* cursor
|
||||
control sequences.
|
||||
|
||||
Add this to your single line intermediate results:
|
||||
|
||||
```text
|
||||
\033[1;1H\033[2J
|
||||
```
|
||||
|
||||
These characters will be interpreted by the terminal
|
||||
(console) as meaning:
|
||||
|
||||
* Move the cursor to line 1 column 1, and
|
||||
|
||||
* Erase everything below this position.
|
||||
|
||||
`\033` is the ESCAPE character represented by its
|
||||
value in *octal*. *Octal* is indicated by the leading
|
||||
bash (back slash) and the leading 0.
|
||||
|
|
|
|||
Loading…
Reference in a new issue