34 lines
1.2 KiB
Plaintext
34 lines
1.2 KiB
Plaintext
|
## Math_Test
|
||
|
A library of commonly used mathematical functions.
|
||
|
|
||
|
**Note:** Jack compilers implement multiplication and division using OS method calls.
|
||
|
|
||
|
### function void init() {
|
||
|
Initializes the library.
|
||
|
### function int abs(int x)
|
||
|
Returns the absolute value of x.
|
||
|
### function int multiply(int x, int y)
|
||
|
Returns the product of x and y. When a Jack compiler detects the multiplication operator `"*"` in the program's code, it handles it by invoking this method. In other words, the Jack expressions x*y and multiply(x,y) return the same value.
|
||
|
### function int divide(int x, int y)
|
||
|
Returns the integer part of x/y. When a Jack compiler detects the multiplication operator `"/"` in the program's code, it handles it by invoking this method. In other words, the Jack expressions x/y and divide(x,y) return the same value.
|
||
|
### function int sqrt(int x)
|
||
|
Returns the integer part of the square root of x.
|
||
|
### function int max(int a, int b)
|
||
|
Returns the greater number.
|
||
|
### function int min(int a, int b)
|
||
|
Returns the smaller number.
|
||
|
***
|
||
|
## Project
|
||
|
* Implement `Math.jack`
|
||
|
* Test in simulation:
|
||
|
```
|
||
|
$ cd 00_Math_Test
|
||
|
$ make
|
||
|
$ cd ../00_HACK
|
||
|
$ apio clean
|
||
|
$ apio sim
|
||
|
```
|
||
|
|
||
|
* Compare the content of special function register DEBUG0--DEBUG4.
|
||
|
![](math.png)
|