defer 具名 匿名 汇编 objdump

 

 

 

 

func increaseA() int {
	var i int
	defer func() {
		i += 256
	}()
	return i
}

func increaseB() (r int) {
	defer func() {
		r += 256
	}()
	return r
}

  

func increaseA() int {
	var i int
	defer func() {
		i += 0x1234
	}()
	return i
}

func increaseB() (r int) {
	defer func() {
		r += 0x1234
	}()
	return r
}

func main() {
	i := increaseA()
	j := increaseB()
	if i != 0 {
		panic(i)
	}
	if j != 0x1234 {
		panic(j)
	}
}

  

 

package main

import "fmt"

func fDefer(i int) int {
	defer func() {
		i += 6789 // 0x1a85
	}()
	defer func() {
		i += 5678 // 0x162e
	}()
	i += 1234 // 0x4d2
	i += 2345 // 0x929
	// 3579 0xdfb
	return i
}
func main() {
	i := fDefer(16) // 3595 0xe0b
	fmt.Print(i)
}

 

go tool compile -S main.go > tmp2

main.fDefer STEXT size=239 args=0x8 locals=0x48 funcid=0x0 align=0x0
	0x0000 00000 (main.go:3)	TEXT	main.fDefer(SB), ABIInternal, $72-8
	0x0000 00000 (main.go:3)	CMPQ	SP, 16(R14)
	0x0004 00004 (main.go:3)	PCDATA	$0, $-2
	0x0004 00004 (main.go:3)	JLS	216
	0x000a 00010 (main.go:3)	PCDATA	$0, $-1
	0x000a 00010 (main.go:3)	SUBQ	$72, SP
	0x000e 00014 (main.go:3)	MOVQ	BP, 64(SP)
	0x0013 00019 (main.go:3)	LEAQ	64(SP), BP
	0x0018 00024 (main.go:3)	MOVUPS	X15, 48(SP)
	0x001e 00030 (main.go:3)	FUNCDATA	$0, gclocals·J5F+7Qw7O7ve2QcWC7DpeQ==(SB)
	0x001e 00030 (main.go:3)	FUNCDATA	$1, gclocals·ySXA9Y8PBdcDMJxYV/AD1w==(SB)
	0x001e 00030 (main.go:3)	FUNCDATA	$2, main.fDefer.stkobj(SB)
	0x001e 00030 (main.go:3)	FUNCDATA	$5, main.fDefer.arginfo1(SB)
	0x001e 00030 (main.go:3)	FUNCDATA	$4, main.fDefer.opendefer(SB)
	0x001e 00030 (main.go:3)	MOVB	$0, main..autotmp_6+7(SP)
	0x0023 00035 (main.go:3)	MOVQ	AX, main.i+80(SP)
	0x0028 00040 (main.go:3)	MOVQ	$0, main.~r0+8(SP)
	0x0031 00049 (main.go:4)	MOVUPS	X15, main..autotmp_3+32(SP)
	0x0037 00055 (main.go:4)	LEAQ	main.fDefer.func1(SB), AX
	0x003e 00062 (main.go:4)	MOVQ	AX, main..autotmp_3+32(SP)
	0x0043 00067 (main.go:4)	LEAQ	main.i+80(SP), AX
	0x0048 00072 (main.go:4)	MOVQ	AX, main..autotmp_3+40(SP)
	0x004d 00077 (main.go:4)	LEAQ	main..autotmp_3+32(SP), CX
	0x0052 00082 (main.go:4)	MOVQ	CX, main..autotmp_7+56(SP)
	0x0057 00087 (main.go:4)	MOVB	$1, main..autotmp_6+7(SP)
	0x005c 00092 (main.go:7)	MOVUPS	X15, main..autotmp_5+16(SP)
	0x0062 00098 (main.go:7)	LEAQ	main.fDefer.func2(SB), CX
	0x0069 00105 (main.go:7)	MOVQ	CX, main..autotmp_5+16(SP)
	0x006e 00110 (main.go:7)	MOVQ	AX, main..autotmp_5+24(SP)
	0x0073 00115 (main.go:7)	LEAQ	main..autotmp_5+16(SP), AX
	0x0078 00120 (main.go:7)	MOVQ	AX, main..autotmp_8+48(SP)
	0x007d 00125 (main.go:7)	MOVB	$3, main..autotmp_6+7(SP)
	0x0082 00130 (main.go:10)	MOVQ	main.i+80(SP), AX
	0x0087 00135 (main.go:11)	ADDQ	$3579, AX
	0x008d 00141 (main.go:11)	MOVQ	AX, main.i+80(SP)
	0x0092 00146 (main.go:13)	MOVQ	AX, main.~r0+8(SP)
	0x0097 00151 (main.go:13)	MOVB	$1, main..autotmp_6+7(SP)
	0x009c 00156 (main.go:13)	MOVQ	main..autotmp_8+48(SP), DX
	0x00a1 00161 (main.go:13)	MOVQ	(DX), AX
	0x00a4 00164 (main.go:13)	PCDATA	$1, $1
	0x00a4 00164 (main.go:13)	CALL	AX
	0x00a6 00166 (main.go:13)	MOVB	$0, main..autotmp_6+7(SP)
	0x00ab 00171 (main.go:13)	MOVQ	main..autotmp_7+56(SP), DX
	0x00b0 00176 (main.go:13)	MOVQ	(DX), AX
	0x00b3 00179 (main.go:13)	CALL	AX
	0x00b5 00181 (main.go:13)	MOVQ	main.~r0+8(SP), AX
	0x00ba 00186 (main.go:13)	MOVQ	64(SP), BP
	0x00bf 00191 (main.go:13)	ADDQ	$72, SP
	0x00c3 00195 (main.go:13)	RET
	0x00c4 00196 (main.go:13)	CALL	runtime.deferreturn(SB)
	0x00c9 00201 (main.go:13)	MOVQ	main.~r0+8(SP), AX
	0x00ce 00206 (main.go:13)	MOVQ	64(SP), BP
	0x00d3 00211 (main.go:13)	ADDQ	$72, SP
	0x00d7 00215 (main.go:13)	RET
	0x00d8 00216 (main.go:13)	NOP
	0x00d8 00216 (main.go:3)	PCDATA	$1, $-1
	0x00d8 00216 (main.go:3)	PCDATA	$0, $-2
	0x00d8 00216 (main.go:3)	MOVQ	AX, 8(SP)
	0x00dd 00221 (main.go:3)	NOP
	0x00e0 00224 (main.go:3)	CALL	runtime.morestack_noctxt(SB)
	0x00e5 00229 (main.go:3)	MOVQ	8(SP), AX
	0x00ea 00234 (main.go:3)	PCDATA	$0, $-1
	0x00ea 00234 (main.go:3)	JMP	0
	0x0000 49 3b 66 10 0f 86 ce 00 00 00 48 83 ec 48 48 89  I;f.......H..HH.
	0x0010 6c 24 40 48 8d 6c 24 40 44 0f 11 7c 24 30 c6 44  l$@H.l$@D..|$0.D
	0x0020 24 07 00 48 89 44 24 50 48 c7 44 24 08 00 00 00  $..H.D$PH.D$....
	0x0030 00 44 0f 11 7c 24 20 48 8d 05 00 00 00 00 48 89  .D..|$ H......H.
	0x0040 44 24 20 48 8d 44 24 50 48 89 44 24 28 48 8d 4c  D$ H.D$PH.D$(H.L
	0x0050 24 20 48 89 4c 24 38 c6 44 24 07 01 44 0f 11 7c  $ H.L$8.D$..D..|
	0x0060 24 10 48 8d 0d 00 00 00 00 48 89 4c 24 10 48 89  $.H......H.L$.H.
	0x0070 44 24 18 48 8d 44 24 10 48 89 44 24 30 c6 44 24  D$.H.D$.H.D$0.D$
	0x0080 07 03 48 8b 44 24 50 48 05 fb 0d 00 00 48 89 44  ..H.D$PH.....H.D
	0x0090 24 50 48 89 44 24 08 c6 44 24 07 01 48 8b 54 24  $PH.D$..D$..H.T$
	0x00a0 30 48 8b 02 ff d0 c6 44 24 07 00 48 8b 54 24 38  0H.....D$..H.T$8
	0x00b0 48 8b 02 ff d0 48 8b 44 24 08 48 8b 6c 24 40 48  H....H.D$.H.l$@H
	0x00c0 83 c4 48 c3 e8 00 00 00 00 48 8b 44 24 08 48 8b  ..H......H.D$.H.
	0x00d0 6c 24 40 48 83 c4 48 c3 48 89 44 24 08 0f 1f 00  l$@H..H.H.D$....
	0x00e0 e8 00 00 00 00 48 8b 44 24 08 e9 11 ff ff ff     .....H.D$......
	rel 58+4 t=14 main.fDefer.func1+0
	rel 101+4 t=14 main.fDefer.func2+0
	rel 164+0 t=10 +0
	rel 179+0 t=10 +0
	rel 197+4 t=7 runtime.deferreturn+0
	rel 225+4 t=7 runtime.morestack_noctxt+0
main.fDefer.func2 STEXT nosplit size=12 args=0x0 locals=0x0 funcid=0x0 align=0x0
	0x0000 00000 (main.go:7)	TEXT	main.fDefer.func2(SB), NOSPLIT|NEEDCTXT|ABIInternal, $0-0
	0x0000 00000 (main.go:7)	FUNCDATA	$0, gclocals·g2BeySu+wFnoycgXfElmcg==(SB)
	0x0000 00000 (main.go:7)	FUNCDATA	$1, gclocals·g2BeySu+wFnoycgXfElmcg==(SB)
	0x0000 00000 (main.go:7)	MOVQ	8(DX), AX
	0x0004 00004 (main.go:8)	ADDQ	$5678, (AX)
	0x000b 00011 (main.go:9)	RET
	0x0000 48 8b 42 08 48 81 00 2e 16 00 00 c3              H.B.H.......
main.fDefer.func1 STEXT nosplit size=12 args=0x0 locals=0x0 funcid=0x0 align=0x0
	0x0000 00000 (main.go:4)	TEXT	main.fDefer.func1(SB), NOSPLIT|NEEDCTXT|ABIInternal, $0-0
	0x0000 00000 (main.go:4)	FUNCDATA	$0, gclocals·g2BeySu+wFnoycgXfElmcg==(SB)
	0x0000 00000 (main.go:4)	FUNCDATA	$1, gclocals·g2BeySu+wFnoycgXfElmcg==(SB)
	0x0000 00000 (main.go:4)	MOVQ	8(DX), AX
	0x0004 00004 (main.go:5)	ADDQ	$6789, (AX)
	0x000b 00011 (main.go:6)	RET
	0x0000 48 8b 42 08 48 81 00 85 1a 00 00 c3              H.B.H.......
main.main STEXT size=78 args=0x0 locals=0x18 funcid=0x0 align=0x0
	0x0000 00000 (main.go:15)	TEXT	main.main(SB), ABIInternal, $24-0
	0x0000 00000 (main.go:15)	CMPQ	SP, 16(R14)
	0x0004 00004 (main.go:15)	PCDATA	$0, $-2
	0x0004 00004 (main.go:15)	JLS	71
	0x0006 00006 (main.go:15)	PCDATA	$0, $-1
	0x0006 00006 (main.go:15)	SUBQ	$24, SP
	0x000a 00010 (main.go:15)	MOVQ	BP, 16(SP)
	0x000f 00015 (main.go:15)	LEAQ	16(SP), BP
	0x0014 00020 (main.go:15)	FUNCDATA	$0, gclocals·g2BeySu+wFnoycgXfElmcg==(SB)
	0x0014 00020 (main.go:15)	FUNCDATA	$1, gclocals·g2BeySu+wFnoycgXfElmcg==(SB)
	0x0014 00020 (main.go:16)	MOVL	$16, AX
	0x0019 00025 (main.go:16)	PCDATA	$1, $0
	0x0019 00025 (main.go:16)	CALL	main.fDefer(SB)
	0x001e 00030 (main.go:16)	NOP
	0x0020 00032 (main.go:17)	CMPQ	AX, $3595
	0x0026 00038 (main.go:17)	JNE	50
	0x0028 00040 (main.go:20)	PCDATA	$1, $-1
	0x0028 00040 (main.go:20)	MOVQ	16(SP), BP
	0x002d 00045 (main.go:20)	ADDQ	$24, SP
	0x0031 00049 (main.go:20)	RET
	0x0032 00050 (main.go:18)	PCDATA	$1, $0
	0x0032 00050 (main.go:18)	CALL	runtime.convT64(SB)
	0x0037 00055 (main.go:18)	MOVQ	AX, BX
	0x003a 00058 (main.go:18)	LEAQ	type.int(SB), AX
	0x0041 00065 (main.go:18)	CALL	runtime.gopanic(SB)
	0x0046 00070 (main.go:18)	XCHGL	AX, AX
	0x0047 00071 (main.go:18)	NOP
	0x0047 00071 (main.go:15)	PCDATA	$1, $-1
	0x0047 00071 (main.go:15)	PCDATA	$0, $-2
	0x0047 00071 (main.go:15)	CALL	runtime.morestack_noctxt(SB)
	0x004c 00076 (main.go:15)	PCDATA	$0, $-1
	0x004c 00076 (main.go:15)	JMP	0
	0x0000 49 3b 66 10 76 41 48 83 ec 18 48 89 6c 24 10 48  I;f.vAH...H.l$.H
	0x0010 8d 6c 24 10 b8 10 00 00 00 e8 00 00 00 00 66 90  .l$...........f.
	0x0020 48 3d 0b 0e 00 00 75 0a 48 8b 6c 24 10 48 83 c4  H=....u.H.l$.H..
	0x0030 18 c3 e8 00 00 00 00 48 89 c3 48 8d 05 00 00 00  .......H..H.....
	0x0040 00 e8 00 00 00 00 90 e8 00 00 00 00 eb b2        ..............
	rel 2+0 t=23 type.int+0
	rel 26+4 t=7 main.fDefer+0
	rel 51+4 t=7 runtime.convT64+0
	rel 61+4 t=14 type.int+0
	rel 66+4 t=7 runtime.gopanic+0
	rel 72+4 t=7 runtime.morestack_noctxt+0

  

go build -o a;./a;objdump -d a  > tmp1

0000000000457c80 <main.fDefer>:
  457c80:	49 3b 66 10          	cmp    0x10(%r14),%rsp
  457c84:	0f 86 ce 00 00 00    	jbe    457d58 <main.fDefer+0xd8>
  457c8a:	48 83 ec 48          	sub    $0x48,%rsp
  457c8e:	48 89 6c 24 40       	mov    %rbp,0x40(%rsp)
  457c93:	48 8d 6c 24 40       	lea    0x40(%rsp),%rbp
  457c98:	44 0f 11 7c 24 30    	movups %xmm15,0x30(%rsp)
  457c9e:	c6 44 24 07 00       	movb   $0x0,0x7(%rsp)
  457ca3:	48 89 44 24 50       	mov    %rax,0x50(%rsp)
  457ca8:	48 c7 44 24 08 00 00 	movq   $0x0,0x8(%rsp)
  457caf:	00 00 
  457cb1:	44 0f 11 7c 24 20    	movups %xmm15,0x20(%rsp)
  457cb7:	48 8d 05 e2 00 00 00 	lea    0xe2(%rip),%rax        # 457da0 <main.fDefer.func1>
  457cbe:	48 89 44 24 20       	mov    %rax,0x20(%rsp)
  457cc3:	48 8d 44 24 50       	lea    0x50(%rsp),%rax
  457cc8:	48 89 44 24 28       	mov    %rax,0x28(%rsp)
  457ccd:	48 8d 4c 24 20       	lea    0x20(%rsp),%rcx
  457cd2:	48 89 4c 24 38       	mov    %rcx,0x38(%rsp)
  457cd7:	c6 44 24 07 01       	movb   $0x1,0x7(%rsp)
  457cdc:	44 0f 11 7c 24 10    	movups %xmm15,0x10(%rsp)
  457ce2:	48 8d 0d 97 00 00 00 	lea    0x97(%rip),%rcx        # 457d80 <main.fDefer.func2>
  457ce9:	48 89 4c 24 10       	mov    %rcx,0x10(%rsp)
  457cee:	48 89 44 24 18       	mov    %rax,0x18(%rsp)
  457cf3:	48 8d 44 24 10       	lea    0x10(%rsp),%rax
  457cf8:	48 89 44 24 30       	mov    %rax,0x30(%rsp)
  457cfd:	c6 44 24 07 03       	movb   $0x3,0x7(%rsp)
  457d02:	48 8b 44 24 50       	mov    0x50(%rsp),%rax
  457d07:	48 05 fb 0d 00 00    	add    $0xdfb,%rax
  457d0d:	48 89 44 24 50       	mov    %rax,0x50(%rsp)
  457d12:	48 89 44 24 08       	mov    %rax,0x8(%rsp)
  457d17:	c6 44 24 07 01       	movb   $0x1,0x7(%rsp)
  457d1c:	48 8b 54 24 30       	mov    0x30(%rsp),%rdx
  457d21:	48 8b 02             	mov    (%rdx),%rax
  457d24:	ff d0                	call   *%rax
  457d26:	c6 44 24 07 00       	movb   $0x0,0x7(%rsp)
  457d2b:	48 8b 54 24 38       	mov    0x38(%rsp),%rdx
  457d30:	48 8b 02             	mov    (%rdx),%rax
  457d33:	ff d0                	call   *%rax
  457d35:	48 8b 44 24 08       	mov    0x8(%rsp),%rax
  457d3a:	48 8b 6c 24 40       	mov    0x40(%rsp),%rbp
  457d3f:	48 83 c4 48          	add    $0x48,%rsp
  457d43:	c3                   	ret    
  457d44:	e8 f7 47 fd ff       	call   42c540 <runtime.deferreturn>
  457d49:	48 8b 44 24 08       	mov    0x8(%rsp),%rax
  457d4e:	48 8b 6c 24 40       	mov    0x40(%rsp),%rbp
  457d53:	48 83 c4 48          	add    $0x48,%rsp
  457d57:	c3                   	ret    
  457d58:	48 89 44 24 08       	mov    %rax,0x8(%rsp)
  457d5d:	0f 1f 00             	nopl   (%rax)
  457d60:	e8 5b cc ff ff       	call   4549c0 <runtime.morestack_noctxt.abi0>
  457d65:	48 8b 44 24 08       	mov    0x8(%rsp),%rax
  457d6a:	e9 11 ff ff ff       	jmp    457c80 <main.fDefer>
  457d6f:	cc                   	int3   
  457d70:	cc                   	int3   
  457d71:	cc                   	int3   
  457d72:	cc                   	int3   
  457d73:	cc                   	int3   
  457d74:	cc                   	int3   
  457d75:	cc                   	int3   
  457d76:	cc                   	int3   
  457d77:	cc                   	int3   
  457d78:	cc                   	int3   
  457d79:	cc                   	int3   
  457d7a:	cc                   	int3   
  457d7b:	cc                   	int3   
  457d7c:	cc                   	int3   
  457d7d:	cc                   	int3   
  457d7e:	cc                   	int3   
  457d7f:	cc                   	int3   

0000000000457d80 <main.fDefer.func2>:
  457d80:	48 8b 42 08          	mov    0x8(%rdx),%rax
  457d84:	48 81 00 2e 16 00 00 	addq   $0x162e,(%rax)
  457d8b:	c3                   	ret    
  457d8c:	cc                   	int3   
  457d8d:	cc                   	int3   
  457d8e:	cc                   	int3   
  457d8f:	cc                   	int3   
  457d90:	cc                   	int3   
  457d91:	cc                   	int3   
  457d92:	cc                   	int3   
  457d93:	cc                   	int3   
  457d94:	cc                   	int3   
  457d95:	cc                   	int3   
  457d96:	cc                   	int3   
  457d97:	cc                   	int3   
  457d98:	cc                   	int3   
  457d99:	cc                   	int3   
  457d9a:	cc                   	int3   
  457d9b:	cc                   	int3   
  457d9c:	cc                   	int3   
  457d9d:	cc                   	int3   
  457d9e:	cc                   	int3   
  457d9f:	cc                   	int3   

0000000000457da0 <main.fDefer.func1>:
  457da0:	48 8b 42 08          	mov    0x8(%rdx),%rax
  457da4:	48 81 00 85 1a 00 00 	addq   $0x1a85,(%rax)
  457dab:	c3                   	ret    
  457dac:	cc                   	int3   
  457dad:	cc                   	int3   
  457dae:	cc                   	int3   
  457daf:	cc                   	int3   
  457db0:	cc                   	int3   
  457db1:	cc                   	int3   
  457db2:	cc                   	int3   
  457db3:	cc                   	int3   
  457db4:	cc                   	int3   
  457db5:	cc                   	int3   
  457db6:	cc                   	int3   
  457db7:	cc                   	int3   
  457db8:	cc                   	int3   
  457db9:	cc                   	int3   
  457dba:	cc                   	int3   
  457dbb:	cc                   	int3   
  457dbc:	cc                   	int3   
  457dbd:	cc                   	int3   
  457dbe:	cc                   	int3   
  457dbf:	cc                   	int3   

0000000000457dc0 <main.main>:
  457dc0:	49 3b 66 10          	cmp    0x10(%r14),%rsp
  457dc4:	76 41                	jbe    457e07 <main.main+0x47>
  457dc6:	48 83 ec 18          	sub    $0x18,%rsp
  457dca:	48 89 6c 24 10       	mov    %rbp,0x10(%rsp)
  457dcf:	48 8d 6c 24 10       	lea    0x10(%rsp),%rbp
  457dd4:	b8 10 00 00 00       	mov    $0x10,%eax
  457dd9:	e8 a2 fe ff ff       	call   457c80 <main.fDefer>
  457dde:	66 90                	xchg   %ax,%ax
  457de0:	48 3d 0b 0e 00 00    	cmp    $0xe0b,%rax
  457de6:	75 0a                	jne    457df2 <main.main+0x32>
  457de8:	48 8b 6c 24 10       	mov    0x10(%rsp),%rbp
  457ded:	48 83 c4 18          	add    $0x18,%rsp
  457df1:	c3                   	ret    
  457df2:	e8 c9 11 fb ff       	call   408fc0 <runtime.convT64>
  457df7:	48 89 c3             	mov    %rax,%rbx
  457dfa:	48 8d 05 bf 42 00 00 	lea    0x42bf(%rip),%rax        # 45c0c0 <type.*+0x40c0>
  457e01:	e8 7a 53 fd ff       	call   42d180 <runtime.gopanic>
  457e06:	90                   	nop
  457e07:	e8 b4 cb ff ff       	call   4549c0 <runtime.morestack_noctxt.abi0>
  457e0c:	eb b2                	jmp    457dc0 <main.main>

  

 

Go每日一题 今日(2022-03-25) - Go语言中文网 - Golang中文社区 https://studygolang.com/interview/question

func increaseA() int {
	var i int
	defer func() {
		i++
	}()
	return i
}

func increaseB() (r int) {
	defer func() {
		r++
	}()
	return r
}

func main() {
	fmt.Println(increaseA())
	fmt.Println(increaseB())
}

  

 

posted @ 2021-08-09 16:50  papering  阅读(36)  评论(0编辑  收藏  举报