Files
avo/examples/sum/README.md
Michael McLoughlin 943d5f0ced examples/sum: attempt to avoid asmfmt mismatch
Updates #41
Updates #8
2019-01-11 11:06:10 -08:00

37 lines
732 B
Markdown

# sum
Sum a slice of `uint64`s.
[embedmd]:# (asm.go go /func main/ /^}/)
```go
func main() {
TEXT("Sum", NOSPLIT, "func(xs []uint64) uint64")
Doc("Sum returns the sum of the elements in xs.")
ptr := Load(Param("xs").Base(), GP64())
n := Load(Param("xs").Len(), GP64())
Comment("Initialize sum register to zero.")
s := GP64()
XORQ(s, s)
Label("loop")
Comment("Loop until zero bytes remain.")
CMPQ(n, Imm(0))
JE(LabelRef("done"))
Comment("Load from pointer and add to running sum.")
ADDQ(Mem{Base: ptr}, s)
Comment("Advance pointer, decrement byte count.")
ADDQ(Imm(8), ptr)
DECQ(n)
JMP(LabelRef("loop"))
Label("done")
Comment("Store sum to return value.")
Store(s, ReturnIndex(0))
RET()
Generate()
}
```