diff options
| author | 2026-11-08 06:49:14 +0000 | |
|---|---|---|
| committer | 2026-11-08 06:49:14 +0000 | |
| commit | 0e856ca15d8aa1499edc33c7302d4d97be404290 (patch) | |
| tree | d2bb840d13c57aa17f643e6039af370255e38379 /src | |
| download | lab-04-master.tar.gz | |
Diffstat (limited to 'src')
| -rw-r--r-- | src/double.mil | 18 | ||||
| -rw-r--r-- | src/double.ms | 33 | ||||
| -rw-r--r-- | src/fac-tail.mil | 19 | ||||
| -rw-r--r-- | src/fac.mil | 13 | ||||
| -rw-r--r-- | src/fib-tail.mil | 20 | ||||
| -rw-r--r-- | src/fib.mil | 13 | ||||
| -rw-r--r-- | src/gcd.mil | 18 |
7 files changed, 134 insertions, 0 deletions
diff --git a/src/double.mil b/src/double.mil new file mode 100644 index 0000000..aea5387 --- /dev/null +++ b/src/double.mil | |||
| @@ -0,0 +1,18 @@ | |||
| 1 | /* double.mil --- read double write -*- MiLan -*- */ | ||
| 2 | /* function declarations */ | ||
| 3 | FUNC double (1) RETURNS; | ||
| 4 | |||
| 5 | /* program */ | ||
| 6 | BEGIN | ||
| 7 | LOCALS n; | ||
| 8 | n := READ; | ||
| 9 | n := double (n); | ||
| 10 | WRITE (n) | ||
| 11 | END | ||
| 12 | |||
| 13 | /* function definitions */ | ||
| 14 | FUNC double (n) RETURNS | ||
| 15 | BEGIN | ||
| 16 | RETURN (n + n) | ||
| 17 | END | ||
| 18 | /* double.mil ends here */ | ||
diff --git a/src/double.ms b/src/double.ms new file mode 100644 index 0000000..2aa3145 --- /dev/null +++ b/src/double.ms | |||
| @@ -0,0 +1,33 @@ | |||
| 1 | ; double.ms --- input double print -*- MiLan-VM -*- | ||
| 2 | ; Double integer with a function call. | ||
| 3 | ; Args and result are in callers stack frame, thus the | ||
| 4 | ; caller makes space for result and frees args after the call. | ||
| 5 | |||
| 6 | ; BEGIN | ||
| 7 | ; LOCALS n; | ||
| 8 | 0: SHIFT 1 ; make space for local variable `n' | ||
| 9 | ; n := READ; | ||
| 10 | 1: INPUT ; read number to double | ||
| 11 | 2: STOREB 0 ; store read number to the first local variable `n' | ||
| 12 | ; n := double (n); | ||
| 13 | 3: LOADB 0 ; load read number from the first local variable `n' | ||
| 14 | 4: NOP ; SHIFT 1, if there are no args and call returns | ||
| 15 | 5: CALL 10 ; call double | ||
| 16 | 6: STOREB 0 ; store returned value to first local variable `n' | ||
| 17 | ; WRITE (n); | ||
| 18 | 7: LOADB 0 ; load doubled number from the first local variable `n' | ||
| 19 | 8: PRINT ; print double number | ||
| 20 | ; END | ||
| 21 | 9: STOP ; stop | ||
| 22 | |||
| 23 | ; FUNC double (n) RETURNS | ||
| 24 | ; BEGIN | ||
| 25 | 10: NOP ; SHIFT N, where N is number of local variables | ||
| 26 | ; RETURN (n + n); | ||
| 27 | 11: LOADB -3 ; load first, and only, argument | ||
| 28 | 12: LOADB -3 ; load first, and only, argument | ||
| 29 | 13: ADD ; add it | ||
| 30 | 14: STOREB -3 ; write the result into (implicit) first arg | ||
| 31 | 15: RETURN 0 ; return, poping N - RETURNS args | ||
| 32 | ; END | ||
| 33 | ; double.ms ends here | ||
diff --git a/src/fac-tail.mil b/src/fac-tail.mil new file mode 100644 index 0000000..a97705d --- /dev/null +++ b/src/fac-tail.mil | |||
| @@ -0,0 +1,19 @@ | |||
| 1 | /* fac-tail.mil --- compute factorial of a number using tail recursion -*- MiLan -*- */ | ||
| 2 | FUNC fac (1) RETURNS; | ||
| 3 | FUNC fac (2) RETURNS; | ||
| 4 | |||
| 5 | BEGIN | ||
| 6 | WRITE (fac (READ)) | ||
| 7 | END | ||
| 8 | |||
| 9 | FUNC fac (n) RETURNS | ||
| 10 | BEGIN | ||
| 11 | RETURN fac (n, 1) | ||
| 12 | END | ||
| 13 | |||
| 14 | FUNC fac (n acc) RETURNS | ||
| 15 | BEGIN | ||
| 16 | IF n = 0 THEN RETURN acc FI; | ||
| 17 | RETURN fac (n - 1, n*acc) | ||
| 18 | END | ||
| 19 | /* fac-tail.mil end here */ | ||
diff --git a/src/fac.mil b/src/fac.mil new file mode 100644 index 0000000..3e19aa8 --- /dev/null +++ b/src/fac.mil | |||
| @@ -0,0 +1,13 @@ | |||
| 1 | /* fac.mil --- compute factorial of a number -*- MiLan -*- */ | ||
| 2 | FUNC fac (1) RETURNS; | ||
| 3 | |||
| 4 | BEGIN | ||
| 5 | WRITE (fac (READ)) | ||
| 6 | END | ||
| 7 | |||
| 8 | FUNC fac (n) RETURNS | ||
| 9 | BEGIN | ||
| 10 | IF n = 0 THEN RETURN 1 FI; | ||
| 11 | RETURN (n*fac (n - 1)) | ||
| 12 | END | ||
| 13 | /* fac.mil end here */ | ||
diff --git a/src/fib-tail.mil b/src/fib-tail.mil new file mode 100644 index 0000000..13655fd --- /dev/null +++ b/src/fib-tail.mil | |||
| @@ -0,0 +1,20 @@ | |||
| 1 | /* fib-tail.mil --- compute fibonacci number using tail recursion -*- MiLan -*- */ | ||
| 2 | FUNC fib (1) RETURNS; | ||
| 3 | FUNC fib (3) RETURNS; | ||
| 4 | |||
| 5 | BEGIN | ||
| 6 | WRITE (fib (READ)) | ||
| 7 | END | ||
| 8 | |||
| 9 | FUNC fib (n) RETURNS | ||
| 10 | BEGIN | ||
| 11 | RETURN fib (0, 1, n) | ||
| 12 | END | ||
| 13 | |||
| 14 | FUNC fib (a b n) RETURNS | ||
| 15 | BEGIN | ||
| 16 | IF n = 0 THEN RETURN a FI; | ||
| 17 | IF n = 1 THEN RETURN b FI; | ||
| 18 | RETURN fib (b, a + b, n - 1) | ||
| 19 | END | ||
| 20 | /* fib-tail.mil end here*/ | ||
diff --git a/src/fib.mil b/src/fib.mil new file mode 100644 index 0000000..6b14ca0 --- /dev/null +++ b/src/fib.mil | |||
| @@ -0,0 +1,13 @@ | |||
| 1 | /* fib.mil --- compute fibonacci number -*- MiLan -*- */ | ||
| 2 | FUNC fib (1) RETURNS; | ||
| 3 | |||
| 4 | BEGIN | ||
| 5 | WRITE (fib (READ)) | ||
| 6 | END | ||
| 7 | |||
| 8 | FUNC fib (n) RETURNS | ||
| 9 | BEGIN | ||
| 10 | IF n < 2 THEN RETURN (n) FI; | ||
| 11 | RETURN (fib (n - 1) + fib (n - 2)) | ||
| 12 | END | ||
| 13 | /* fib.mil ends here */ | ||
diff --git a/src/gcd.mil b/src/gcd.mil new file mode 100644 index 0000000..baefbee --- /dev/null +++ b/src/gcd.mil | |||
| @@ -0,0 +1,18 @@ | |||
| 1 | /* gcd.mil -- compute greatest common divisor -*- MiLan -*- */ | ||
| 2 | FUNC gcd (0); | ||
| 3 | |||
| 4 | BEGIN | ||
| 5 | a := READ; | ||
| 6 | b := READ; | ||
| 7 | gcd (); | ||
| 8 | WRITE (c) | ||
| 9 | END | ||
| 10 | |||
| 11 | FUNC gcd () | ||
| 12 | BEGIN | ||
| 13 | WHILE a != b DO | ||
| 14 | IF a < b THEN b := b - a | ||
| 15 | ELSE a := a - b FI OD; | ||
| 16 | c := a | ||
| 17 | END | ||
| 18 | /* gcd.mil ends here */ | ||
