From 0e856ca15d8aa1499edc33c7302d4d97be404290 Mon Sep 17 00:00:00 2001 From: Martin Michalec Date: Sun, 8 Nov 2026 09:49:14 +0300 Subject: publish --- src/double.ms | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/double.ms (limited to 'src/double.ms') 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 @@ +; double.ms --- input double print -*- MiLan-VM -*- +; Double integer with a function call. +; Args and result are in callers stack frame, thus the +; caller makes space for result and frees args after the call. + +; BEGIN +; LOCALS n; +0: SHIFT 1 ; make space for local variable `n' +; n := READ; +1: INPUT ; read number to double +2: STOREB 0 ; store read number to the first local variable `n' +; n := double (n); +3: LOADB 0 ; load read number from the first local variable `n' +4: NOP ; SHIFT 1, if there are no args and call returns +5: CALL 10 ; call double +6: STOREB 0 ; store returned value to first local variable `n' +; WRITE (n); +7: LOADB 0 ; load doubled number from the first local variable `n' +8: PRINT ; print double number +; END +9: STOP ; stop + +; FUNC double (n) RETURNS +; BEGIN +10: NOP ; SHIFT N, where N is number of local variables +; RETURN (n + n); +11: LOADB -3 ; load first, and only, argument +12: LOADB -3 ; load first, and only, argument +13: ADD ; add it +14: STOREB -3 ; write the result into (implicit) first arg +15: RETURN 0 ; return, poping N - RETURNS args +; END +; double.ms ends here -- cgit v1.3.1