diff options
| author | 2026-11-08 06:49:14 +0000 | |
|---|---|---|
| committer | 2026-11-08 06:49:14 +0000 | |
| commit | 0e856ca15d8aa1499edc33c7302d4d97be404290 (patch) | |
| tree | d2bb840d13c57aa17f643e6039af370255e38379 /src/double.ms | |
| download | lab-04-master.tar.gz | |
Diffstat (limited to 'src/double.ms')
| -rw-r--r-- | src/double.ms | 33 |
1 files changed, 33 insertions, 0 deletions
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 | ||
