Easy to read Golang assembly output?

后端 未结 5 1773
猫巷女王i
猫巷女王i 2020-12-24 06:13

I\'m interested in examining the x86 assembly output of the standard Go compiler to see if my code is really being converted into reasonably efficient assembly code; hopeful

5条回答
  •  臣服心动
    2020-12-24 06:46

    I had problems with the other answers as the assembly produced provided much more information than I wanted and still not enough details. Let me explain: it provided the assembly for all libraries imported by go internally and did not provide the lines of where my code was (my code was all at the bottom of the file)

    Here is what I found from the official docs:

    $ GOOS=linux GOARCH=amd64 go tool compile -S x.go # or: go build -gcflags -S x.go

    File:

    package main
    
    func main() {
        println(3)
    }
    

    Produces:

    --- prog list "main" ---
    0000 (x.go:3) TEXT    main+0(SB),$8-0
    0001 (x.go:3) FUNCDATA $0,gcargs·0+0(SB)
    0002 (x.go:3) FUNCDATA $1,gclocals·0+0(SB)
    0003 (x.go:4) MOVQ    $3,(SP)
    0004 (x.go:4) PCDATA  $0,$8
    0005 (x.go:4) CALL    ,runtime.printint+0(SB)
    0006 (x.go:4) PCDATA  $0,$-1
    0007 (x.go:4) PCDATA  $0,$0
    0008 (x.go:4) CALL    ,runtime.printnl+0(SB)
    0009 (x.go:4) PCDATA  $0,$-1
    0010 (x.go:5) RET     ,
    

    So what I did was basically:

    go tool compile -S hello.go > hello.s

    and it got the result I wanted!

提交回复
热议问题