gpt4 book ai didi

go - 如何使用go tool编译?

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

简单代码recursion and tails

package main

import "fmt"

func TailRecursive(number int, product int) int {

product = product + number

if number == 1 {

return product
}

return TailRecursive(number-1, product)
}

func main() {

answer := TailRecursive(5, 0)
fmt.Printf("Recursive: %d\n", answer)
}

当我尝试工具编译时

go tool compile 6g -S ./g9.go > assembly.asm

我明白了

 cat assembly.asm 
6g:0:0: open 6g: no such file or directory

我的内核架构

x86_64 x86_64 x86_64 GNU/Linux

如何使用go tool compile得到正确的汇编输出?

最佳答案

删除 6g(现在称为 compile)

go tool compile -S ./g9.go > assembly.asm

输出

"".TailRecursive STEXT size=107 args=0x18 locals=0x20
0x0000 00000 (g9.go:5) TEXT "".TailRecursive(SB), $32-24
0x0000 00000 (g9.go:5) MOVQ (TLS), CX
0x0009 00009 (g9.go:5) CMPQ SP, 16(CX)
0x000d 00013 (g9.go:5) JLS 100
0x000f 00015 (g9.go:5) SUBQ $32, SP
0x0013 00019 (g9.go:5) MOVQ BP, 24(SP)
0x0018 00024 (g9.go:5) LEAQ 24(SP), BP
0x001d 00029 (g9.go:5) FUNCDATA $0, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
0x001d 00029 (g9.go:5) FUNCDATA $1, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
0x001d 00029 (g9.go:5) FUNCDATA $3, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
0x001d 00029 (g9.go:7) PCDATA $2, $0
0x001d 00029 (g9.go:7) PCDATA $0, $0
0x001d 00029 (g9.go:7) MOVQ "".number+40(SP), AX
0x0022 00034 (g9.go:7) MOVQ "".product+48(SP), CX
0x0027 00039 (g9.go:7) ADDQ AX, CX
0x002a 00042 (g9.go:9) CMPQ AX, $1
0x002e 00046 (g9.go:9) JNE 63
0x0030 00048 (g9.go:11) MOVQ CX, "".~r2+56(SP)
0x0035 00053 (g9.go:11) MOVQ 24(SP), BP
0x003a 00058 (g9.go:11) ADDQ $32, SP
0x003e 00062 (g9.go:11) RET
...

关于go - 如何使用go tool编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54286085/

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