doc,examples/sum: improve doc on sum example

Updates #13
This commit is contained in:
Michael McLoughlin
2019-01-06 18:06:29 -08:00
parent efb45b6500
commit ff7d8afefe
3 changed files with 55 additions and 0 deletions

View File

@@ -12,15 +12,25 @@ func main() {
Doc("Sum returns the sum of the elements in xs.")
ptr := Load(Param("xs").Base(), GP64())
n := Load(Param("xs").Len(), GP64())
// Initialize sum register to zero.
s := GP64()
XORQ(s, s)
// Loop until zero bytes remain.
Label("loop")
CMPQ(n, Imm(0))
JE(LabelRef("done"))
// Load from pointer and add to running sum.
ADDQ(Mem{Base: ptr}, s)
// Advance pointer, decrement byte count.
ADDQ(Imm(8), ptr)
DECQ(n)
JMP(LabelRef("loop"))
// Store sum to return value.
Label("done")
Store(s, ReturnIndex(0))
RET()