Files
avo/examples/sum/asm.go

39 lines
770 B
Go
Raw Normal View History

//go:build ignore
package main
import (
. "github.com/mmcloughlin/avo/build"
. "github.com/mmcloughlin/avo/operand"
)
func main() {
TEXT("Sum", NOSPLIT, "func(xs []uint64) uint64")
2018-12-27 23:09:44 -08:00
Doc("Sum returns the sum of the elements in xs.")
ptr := Load(Param("xs").Base(), GP64())
n := Load(Param("xs").Len(), GP64())
2019-01-11 10:57:38 -08:00
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"))
2019-01-11 10:57:38 -08:00
Comment("Load from pointer and add to running sum.")
ADDQ(Mem{Base: ptr}, s)
2019-01-11 10:57:38 -08:00
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()
}