package main
func add(a, b int32) (int32, bool) {
return a+b, true
}
func main() {
add(10, 32)
}
自己动手汇编
GOOS=linux GOARCH=amd64 go tool compile -S mainf.go
"".add t=1 size=32 value=0 args=0x10 locals=0x0
0x0000 00000 (mainf.go:4) TEXT "".add(SB), $0-16
0x0000 00000 (mainf.go:5) MOVL "".a+8(FP), BX
0x0004 00004 (mainf.go:5) MOVL "".b+12(FP), BP
0x0008 00008 (mainf.go:5) ADDL BP, BX
0x000a 00010 (mainf.go:5) MOVL BX, "".~r2+16(FP)
0x000e 00014 (mainf.go:5) MOVB $1, "".~r3+20(FP)
0x0013 00019 (mainf.go:5) RET
"".main t=1 size=64 value=0 args=0x0 locals=0x10
0x0000 00000 (mainf.go:8) TEXT "".main(SB), $16-0
0x0000 00000 (mainf.go:8) MOVQ (TLS), CX
0x0009 00009 (mainf.go:8) CMPQ SP, 16(CX)
0x000d 00013 (mainf.go:8) JLS 44
0x000f 00015 (mainf.go:8) SUBQ $16, SP
0x0013 00019 (mainf.go:9) MOVL $10, (SP)
0x001a 00026 (mainf.go:9) MOVL $32, 4(SP)
0x0022 00034 (mainf.go:9) CALL "".add(SB)
0x0027 00039 (mainf.go:10) ADDQ $16, SP
0x002b 00043 (mainf.go:10) RET
0x002c 00044 (mainf.go:8) CALL runtime.morestack_noctxt(SB)
0x0031 00049 (mainf.go:8) JMP 0
栈帧如下:
main.main saved frame-pointer(BP)
~r3 (bool) = true
~r2 (int32) = 42
b (int32) = 32
a (int32) = 10
return_addr_main.main+0x30