From 3ca82be16cb95edc5d7c89ecc426c7eb0e233ba8 Mon Sep 17 00:00:00 2001 From: Michael McLoughlin Date: Fri, 11 Jan 2019 10:57:38 -0800 Subject: [PATCH] examples/sum: use Comment (#41) --- README.md | 18 +++++++++++++----- examples/sum/README.md | 10 +++++----- examples/sum/asm.go | 10 +++++----- examples/sum/sum.s | 8 ++++++++ 4 files changed, 31 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index b8f7ed8..8aa2536 100644 --- a/README.md +++ b/README.md @@ -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() @@ -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 diff --git a/examples/sum/README.md b/examples/sum/README.md index 15b2098..0c554b3 100644 --- a/examples/sum/README.md +++ b/examples/sum/README.md @@ -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() diff --git a/examples/sum/asm.go b/examples/sum/asm.go index 1fd72e0..e8e8c4c 100644 --- a/examples/sum/asm.go +++ b/examples/sum/asm.go @@ -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() diff --git a/examples/sum/sum.s b/examples/sum/sum.s index 35928d7..61ca50b 100644 --- a/examples/sum/sum.s +++ b/examples/sum/sum.s @@ -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