Reputation: 2449
I built golang code and output assembly code. Code and output are above.
Am I correct, that JMP instruction in:
0x000f 00015 (math.go:17) JMP 23
is pointing to 0x0017 (decimal 23)? I am a bit confused to see a decimal value after JMP instruction.
Code:
(math.go:15) func multiplyLoop(a, b int) int {
(math.go:16) var res int
(math.go:17) for i := 0; i < b; i++ {
(math.go:18) res += a
(math.go:19) }
(math.go:20) return res
(math.go:21) }
Build:
go tool compile -S -m math.go
Assembly:
"".multiplyLoop STEXT nosplit size=34 args=0x18 locals=0x0
0x0000 00000 (math.go:15) TEXT "".multiplyLoop(SB), NOSPLIT, $0-24
0x0000 00000 (math.go:15) FUNCDATA $0, gclocals·54241e171da8af6ae173d69da0236748(SB)
0x0000 00000 (math.go:15) FUNCDATA $1, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
0x0000 00000 (math.go:15) MOVQ "".a+8(SP), AX
0x0005 00005 (math.go:15) MOVQ "".b+16(SP), CX
0x000a 00010 (math.go:15) MOVL $0, DX
0x000c 00012 (math.go:15) MOVQ DX, BX
0x000f 00015 (math.go:17) JMP 23
0x0011 00017 (math.go:17) INCQ DX
0x0014 00020 (math.go:18) ADDQ AX, BX
0x0017 00023 (math.go:17) CMPQ DX, CX
0x001a 00026 (math.go:17) JLT 17
0x001c 00028 (math.go:20) MOVQ BX, "".~r2+24(SP)
0x0021 00033 (math.go:20) RET
Upvotes: 1
Views: 328
Reputation: 166598
JMP 23
to line 0x0017 00023
for i < b
, CMPQ DX, CX
. JLT 17
to line 0x0011 00017
for i++
, INCQ DX
. For example,
package multiply
func multiplyLoop(a, b int) int {
var res int
for i := 0; i < b; i++ {
res += a
}
return res
}
$ go tool compile -S multiply.go
"".multiplyLoop STEXT nosplit size=34 args=0x18 locals=0x0
0x0000 00000 (multiply.go:3) TEXT "".multiplyLoop(SB), NOSPLIT, $0-24
0x0000 00000 (multiply.go:3) FUNCDATA $0, gclocals·54241e171da8af6ae173d69da0236748(SB)
0x0000 00000 (multiply.go:3) FUNCDATA $1, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
0x0000 00000 (multiply.go:3) MOVQ "".a+8(SP), AX
0x0005 00005 (multiply.go:3) MOVQ "".b+16(SP), CX
0x000a 00010 (multiply.go:3) XORL DX, DX
0x000c 00012 (multiply.go:3) MOVQ DX, BX
0x000f 00015 (multiply.go:5) JMP 23
0x0011 00017 (multiply.go:5) INCQ DX
0x0014 00020 (multiply.go:6) ADDQ AX, BX
0x0017 00023 (multiply.go:5) CMPQ DX, CX
0x001a 00026 (multiply.go:5) JLT 17
0x001c 00028 (multiply.go:8) MOVQ BX, "".~r2+24(SP)
0x0021 00033 (multiply.go:8) RET
0x0000 48 8b 44 24 08 48 8b 4c 24 10 31 d2 48 89 d3 eb H.D$.H.L$.1.H...
0x0010 06 48 ff c2 48 01 c3 48 39 ca 7c f5 48 89 5c 24 .H..H..H9.|.H.\$
0x0020 18 c3 ..
go.info."".multiplyLoop SDWARFINFO size=78
0x0000 02 22 22 2e 6d 75 6c 74 69 70 6c 79 4c 6f 6f 70 ."".multiplyLoop
0x0010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x0020 00 01 9c 00 00 00 00 01 0e 61 00 00 03 00 00 00 .........a......
0x0030 00 01 9c 0e 62 00 00 03 00 00 00 00 02 91 08 0e ....b...........
0x0040 7e 72 32 00 01 03 00 00 00 00 02 91 10 00 ~r2...........
rel 17+8 t=1 "".multiplyLoop+0
rel 25+8 t=1 "".multiplyLoop+34
rel 35+4 t=29 gofile../home/peter/Dropbox/gopath/src/so/multiply.go+0
rel 45+4 t=28 go.info.int+0
rel 56+4 t=28 go.info.int+0
rel 70+4 t=28 go.info.int+0
go.range."".multiplyLoop SDWARFRANGE size=0
gclocals·54241e171da8af6ae173d69da0236748 SRODATA dupok size=9
0x0000 01 00 00 00 03 00 00 00 00 .........
gclocals·33cdeccccebe80329f1fdbee7f5874cb SRODATA dupok size=8
0x0000 01 00 00 00 00 00 00 00
$
Upvotes: 1