Skip to content

Commit

Permalink
examples/sum: use Comment (mmcloughlin#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmcloughlin committed Jan 11, 2019
1 parent 8543268 commit 3ca82be
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 15 deletions.
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,24 +102,24 @@ func main() {
ptr := Load(Param("xs").Base(), GP64())
n := Load(Param("xs").Len(), GP64())
// Initialize sum register to zero.
Comment("Initialize sum register to zero.")
s := GP64()
XORQ(s, s)
// Loop until zero bytes remain.
Comment("Loop until zero bytes remain.")
Label("loop")
CMPQ(n, Imm(0))
JE(LabelRef("done"))
// Load from pointer and add to running sum.
Comment("Load from pointer and add to running sum.")
ADDQ(Mem{Base: ptr}, s)
// Advance pointer, decrement byte count.
Comment("Advance pointer, decrement byte count.")
ADDQ(Imm(8), ptr)
DECQ(n)
JMP(LabelRef("loop"))
// Store sum to return value.
Comment("Store sum to return value.")
Label("done")
Store(s, ReturnIndex(0))
RET()
Expand All @@ -139,16 +139,24 @@ The result from this code generator is:
TEXT ·Sum(SB), NOSPLIT, $0-32
MOVQ xs_base(FP), AX
MOVQ xs_len+8(FP), CX
// Initialize sum register to zero.
XORQ DX, DX
// Loop until zero bytes remain.
loop:
CMPQ CX, $0x00
JE done
// Load from pointer and add to running sum.
ADDQ (AX), DX
// Advance pointer, decrement byte count.
ADDQ $0x08, AX
DECQ CX
JMP loop
// Store sum to return value.
done:
MOVQ DX, ret+24(FP)
RET
Expand Down
10 changes: 5 additions & 5 deletions examples/sum/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@ func main() {
ptr := Load(Param("xs").Base(), GP64())
n := Load(Param("xs").Len(), GP64())

// Initialize sum register to zero.
Comment("Initialize sum register to zero.")
s := GP64()
XORQ(s, s)

// Loop until zero bytes remain.
Comment("Loop until zero bytes remain.")
Label("loop")
CMPQ(n, Imm(0))
JE(LabelRef("done"))

// Load from pointer and add to running sum.
Comment("Load from pointer and add to running sum.")
ADDQ(Mem{Base: ptr}, s)

// Advance pointer, decrement byte count.
Comment("Advance pointer, decrement byte count.")
ADDQ(Imm(8), ptr)
DECQ(n)
JMP(LabelRef("loop"))

// Store sum to return value.
Comment("Store sum to return value.")
Label("done")
Store(s, ReturnIndex(0))
RET()
Expand Down
10 changes: 5 additions & 5 deletions examples/sum/asm.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,24 @@ func main() {
ptr := Load(Param("xs").Base(), GP64())
n := Load(Param("xs").Len(), GP64())

// Initialize sum register to zero.
Comment("Initialize sum register to zero.")
s := GP64()
XORQ(s, s)

// Loop until zero bytes remain.
Comment("Loop until zero bytes remain.")
Label("loop")
CMPQ(n, Imm(0))
JE(LabelRef("done"))

// Load from pointer and add to running sum.
Comment("Load from pointer and add to running sum.")
ADDQ(Mem{Base: ptr}, s)

// Advance pointer, decrement byte count.
Comment("Advance pointer, decrement byte count.")
ADDQ(Imm(8), ptr)
DECQ(n)
JMP(LabelRef("loop"))

// Store sum to return value.
Comment("Store sum to return value.")
Label("done")
Store(s, ReturnIndex(0))
RET()
Expand Down
8 changes: 8 additions & 0 deletions examples/sum/sum.s
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,24 @@
TEXT ·Sum(SB), NOSPLIT, $0-32
MOVQ xs_base(FP), AX
MOVQ xs_len+8(FP), CX

// Initialize sum register to zero.
XORQ DX, DX

// Loop until zero bytes remain.
loop:
CMPQ CX, $0x00
JE done

// Load from pointer and add to running sum.
ADDQ (AX), DX

// Advance pointer, decrement byte count.
ADDQ $0x08, AX
DECQ CX
JMP loop

// Store sum to return value.
done:
MOVQ DX, ret+24(FP)
RET

0 comments on commit 3ca82be

Please sign in to comment.