diff options
author | Kartik Agaram <vc@akkartik.com> | 2020-06-15 17:38:28 -0700 |
---|---|---|
committer | Kartik Agaram <vc@akkartik.com> | 2020-06-15 17:38:28 -0700 |
commit | 538805df510df440b5396a15a5ed8869ea8f5df7 (patch) | |
tree | bb2525f7e06ba52e1134e9d9e329ea8531772f2d /apps | |
parent | d292196ff5ab9f103364e05c42a1edc03dc1b5ab (diff) | |
download | mu-538805df510df440b5396a15a5ed8869ea8f5df7.tar.gz |
6530
New prototype: a simple 4-operator calculator. Inspired (yet again) by Crenshaw.
Diffstat (limited to 'apps')
-rw-r--r-- | apps/arith.mu | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/apps/arith.mu b/apps/arith.mu new file mode 100644 index 00000000..e5d1be18 --- /dev/null +++ b/apps/arith.mu @@ -0,0 +1,40 @@ +fn main args: (addr array (addr array byte)) -> exit-status/ebx: int { + # var input/esi: (addr array byte) = args[1] {{{ + var input/esi: (addr array byte) <- copy 0 + { + var args-in-reg/eax: (addr array (addr array byte)) <- copy args + var input-in-eax/eax: (addr addr array byte) <- index args-in-reg, 1 + input <- copy *input-in-eax + } + # }}} + var result/eax: int <- parse-int input + exit-status <- copy result # only LSB makes it out +} + +fn parse-int _in: (addr array byte) -> result/eax: int { + var in/esi: (addr array byte) <- copy _in + var len/edx: int <- length in + var i/ecx: int <- copy 0 + var out/edi: int <- copy 0 + { + compare i, len + break-if->= + # out *= 10 + { + var ten/eax: int <- copy 0xa + out <- multiply ten + } + # c = in[i] + var c/eax: byte <- copy 0 + { + var tmp/ebx: (addr byte) <- index in, i + c <- copy-byte *tmp + } + # + var digit/eax: int <- to-decimal-digit c + out <- add digit + i <- increment + loop + } + result <- copy out +} |