From 51931ee6739078b31e769c4f3ec9f90e4f9511c4 Mon Sep 17 00:00:00 2001 From: Michael McLoughlin Date: Tue, 1 Jan 2019 16:04:37 -0800 Subject: [PATCH] doc: README for sum example Updates #14 --- examples/README.md | 6 +++++- examples/sum/README.md | 26 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 examples/sum/README.md diff --git a/examples/README.md b/examples/README.md index 36d720b..9f48cff 100644 --- a/examples/README.md +++ b/examples/README.md @@ -1,7 +1,11 @@ # Examples -Feature demonstrations: +Simple functions: * **[add](add):** Add two numbers. The "Hello World!" of `avo`. +* **[sum](sum):** Sum an array of numbers. + +Features: + * **[args](args):** Loading function arguments. * **[complex](complex):** Working with `complex{64,128}` types. diff --git a/examples/sum/README.md b/examples/sum/README.md new file mode 100644 index 0000000..69c03ae --- /dev/null +++ b/examples/sum/README.md @@ -0,0 +1,26 @@ +# sum + +Sum an array of `uint64`s. + +[embedmd]:# (asm.go go /func main/ /^}/) +```go +func main() { + TEXT("Sum", "func(xs []uint64) uint64") + Doc("Sum returns the sum of the elements in xs.") + ptr := Load(Param("xs").Base(), GP64v()) + n := Load(Param("xs").Len(), GP64v()) + s := GP64v() + XORQ(s, s) + LABEL("loop") + CMPQ(n, operand.Imm(0)) + JE(operand.LabelRef("done")) + ADDQ(operand.Mem{Base: ptr}, s) + ADDQ(operand.Imm(8), ptr) + DECQ(n) + JMP(operand.LabelRef("loop")) + LABEL("done") + Store(s, ReturnIndex(0)) + RET() + Generate() +} +```