From 5fe0bc16884c0f16f173db55a7f59a9738e67f02 Mon Sep 17 00:00:00 2001 From: Kartik Agaram Date: Mon, 15 Jun 2020 18:05:23 -0700 Subject: 6531 --- apps/arith.mu | 42 +++++++++++++++++------------------------- 1 file changed, 17 insertions(+), 25 deletions(-) (limited to 'apps') diff --git a/apps/arith.mu b/apps/arith.mu index e5d1be18..6d0307ee 100644 --- a/apps/arith.mu +++ b/apps/arith.mu @@ -1,39 +1,31 @@ -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 main -> exit-status/ebx: int { + var val/eax: int <- num + print-int32-to-screen val + print-string "\n" + exit-status <- copy 0 } -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 +fn num -> result/eax: int { var out/edi: int <- copy 0 { - compare i, len - break-if->= + var c/eax: byte <- read-key + # if (c == EOF) break + compare c, 0xffffffff # EOF marker + break-if-= + # if (c == ' ') break + compare c, 0x20 # space + break-if-= + # if (c == '\n') break + compare c, 0xa # newline + 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 - } - # + # out += digit(c) var digit/eax: int <- to-decimal-digit c out <- add digit - i <- increment loop } result <- copy out -- cgit 1.4.1-2-gfad0