gpt4 book ai didi

go - golang汇编中的jmp指令

转载 作者:IT王子 更新时间:2023-10-29 02:10:09 26 4
gpt4 key购买 nike

我构建了 golang 代码并输出了汇编代码。代码和输出在上面。JMP 指令是否正确:0x000f 00015 (math.go:17) JMP 23指向0x0017(十进制23)?在 JMP 指令后看到十进制值让我有点困惑。

代码:

(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) }

构建:

go tool compile -S -m math.go

程序集:

"".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

最佳答案

JMP 23到线 0x0017 00023对于 i < b , CMPQ DX, CX . JLT 17到线 0x0011 00017对于 i++ , INCQ DX .例如,

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
$

关于go - golang汇编中的jmp指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49103204/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com