目录

如何查看 go 执行的汇编语言

简介

查看代码执行的汇编语言

// 示例代码:main.go
package main

func add(a, b int) int {
    return a + b
}

func main() {
    _ = add(42, 100)
}

一、方式一:手动查询

go build -gcflags -S main.go 2>&1

mac@fiveyoboy learn-go % go build -gcflags -S main.go 2>&1 
# command-line-arguments
main.add STEXT size=16 args=0x10 locals=0x0 funcid=0x0 align=0x0 leaf
        0x0000 00000 (/Users/ty******/git/go/src/it******.com/learn-go/main.go:3)       TEXT    main.add(SB), LEAF|NOFRAME|ABIInternal, $0-16
        0x0000 00000 (/Users/ty******/git/go/src/it******.com/learn-go/main.go:3)       FUNCDATA        $0, gclocals·g2BeySu+wFnoycgXfElmcg==(SB)
        0x0000 00000 (/Users/ty******/git/go/src/it******.com/learn-go/main.go:3)       FUNCDATA        $1, gclocals·g2BeySu+wFnoycgXfElmcg==(SB)
        0x0000 00000 (/Users/ty******/git/go/src/it******.com/learn-go/main.go:3)       FUNCDATA        $5, main.add.arginfo1(SB)
        0x0000 00000 (/Users/ty******/git/go/src/it******.com/learn-go/main.go:3)       FUNCDATA        $6, main.add.argliveinfo(SB)
        0x0000 00000 (/Users/ty******/git/go/src/it******.com/learn-go/main.go:3)       PCDATA  $3, $1
        0x0000 00000 (/Users/ty******/git/go/src/it******.com/learn-go/main.go:4)       ADD     R1, R0, R0
        0x0004 00004 (/Users/ty******/git/go/src/it******.com/learn-go/main.go:4)       RET     (R30)
        0x0000 00 00 01 8b c0 03 5f d6 00 00 00 00 00 00 00 00  ......_.........

二、方式二:在线工具

查看 go 执行的汇编语言: Compiler Explorer (godbolt.org)

查看汇编:

用起来也是比较简单(网站地址已贴在文后)

  1. 编写源代码
  2. 将源代码复制到编译网站
  3. 右键执行 Compile
  4. 即可查看 汇编过程

如下图:

/img/go-assembly-language/1.png
1