Skip to content

Instantly share code, notes, and snippets.

@tigercallme
Last active August 12, 2016 09:18
Show Gist options
  • Save tigercallme/77673b447b7e2c7e2bf75767dd249954 to your computer and use it in GitHub Desktop.
Save tigercallme/77673b447b7e2c7e2bf75767dd249954 to your computer and use it in GitHub Desktop.
package main
func rangeCopy() {
x := [2]int{0x11, 0x22}
for i, n := range x {
x[0], x[1] = 0x100, 0x200
println(i, n)
}
}
func main() {
rangeCopy()
}
####################################################################################
# go build && go tool objdump -s "main\.rangeCopy" test
0x0000000000002053 <+19>: sub rsp,0x40 // 分配栈帧空间
0x0000000000002057 <+23>: mov QWORD PTR [rsp+0x18],0x0
0x0000000000002060 <+32>: mov QWORD PTR [rsp+0x20],0x0 // 初始化数组零值
0x0000000000002069 <+41>: mov QWORD PTR [rsp+0x18],0x11
0x0000000000002072 <+50>: mov QWORD PTR [rsp+0x20],0x22 // 数组赋值
0x000000000000207b <+59>: movups xmm0,XMMWORD PTR [rsp+0x18]
0x0000000000002080 <+64>: movups XMMWORD PTR [rsp+0x28],xmm0
0x0000000000002085 <+69>: xor eax,eax // eax 赋0值
0x0000000000002087 <+71>: lea rcx,[rsp+0x28]
0x000000000000208c <+76>: mov QWORD PTR [rsp+0x10],rax
0x0000000000002091 <+81>: mov QWORD PTR [rsp+0x38],rcx
0x0000000000002096 <+86>: cmp rax,0x2
0x000000000000209a <+90>: jge 0x2108 <main.rangeCopy+200>
0x000000000000209c <+92>: mov rdx,QWORD PTR [rcx]
0x000000000000209f <+95>: mov QWORD PTR [rsp+0x8],rdx
0x00000000000020a4 <+100>: mov QWORD PTR [rsp+0x18],0x100
0x00000000000020ad <+109>: mov QWORD PTR [rsp+0x20],0x200 // 数组赋值
0x00000000000020b6 <+118>: call 0x22850 <runtime.printlock>
0x00000000000020bb <+123>: mov rax,QWORD PTR [rsp+0x10]
0x00000000000020c0 <+128>: mov QWORD PTR [rsp],rax
0x00000000000020c4 <+132>: call 0x22ee0 <runtime.printint>
0x00000000000020c9 <+137>: call 0x22a30 <runtime.printsp>
0x00000000000020ce <+142>: mov rax,QWORD PTR [rsp+0x8]
0x00000000000020d3 <+147>: mov QWORD PTR [rsp],rax
0x00000000000020d7 <+151>: call 0x22ee0 <runtime.printint>
0x00000000000020dc <+156>: call 0x22a80 <runtime.printnl>
0x00000000000020e1 <+161>: call 0x228d0 <runtime.printunlock>
0x00000000000020e6 <+166>: mov rdx,QWORD PTR [rsp+0x38]
0x00000000000020eb <+171>: lea rcx,[rdx+0x8]
0x00000000000020ef <+175>: mov rdx,QWORD PTR [rsp+0x10]
0x00000000000020f4 <+180>: lea rax,[rdx+0x1] // rax 增加为1
0x00000000000020f8 <+184>: mov QWORD PTR [rsp+0x10],rax
0x00000000000020fd <+189>: mov QWORD PTR [rsp+0x38],rcx
0x0000000000002102 <+194>: cmp rax,0x2
0x0000000000002106 <+198>: jl 0x209c <main.rangeCopy+92>
0x0000000000002108 <+200>: add rsp,0x40 // 收缩栈帧空间
0x000000000000210c <+204>: ret
# go build -gcflags "-N -l" && go tool objdump -s "main\.rangeCopy" test
0x0000000000002053 <+19>: sub rsp,0x58
0x0000000000002057 <+23>: mov QWORD PTR [rsp+0x30],0x0
0x0000000000002060 <+32>: mov QWORD PTR [rsp+0x38],0x0
0x0000000000002069 <+41>: mov QWORD PTR [rsp+0x30],0x11
0x0000000000002072 <+50>: mov QWORD PTR [rsp+0x38],0x22
0x000000000000207b <+59>: movups xmm0,XMMWORD PTR [rsp+0x30]
0x0000000000002080 <+64>: movups XMMWORD PTR [rsp+0x40],xmm0
0x0000000000002085 <+69>: mov QWORD PTR [rsp+0x28],0x0
0x000000000000208e <+78>: mov QWORD PTR [rsp+0x20],0x2
0x0000000000002097 <+87>: lea rax,[rsp+0x40]
0x000000000000209c <+92>: mov QWORD PTR [rsp+0x50],rax
0x00000000000020a1 <+97>: jmp 0x20a3 <main.rangeCopy+99>
0x00000000000020a3 <+99>: mov rax,QWORD PTR [rsp+0x28]
0x00000000000020a8 <+104>: mov rcx,QWORD PTR [rsp+0x20]
0x00000000000020ad <+109>: cmp rax,rcx // cmp 0x0 0x2
0x00000000000020b0 <+112>: jl 0x20b7 <main.rangeCopy+119>
0x00000000000020b2 <+114>: jmp 0x213e <main.rangeCopy+254>
0x00000000000020b7 <+119>: mov rax,QWORD PTR [rsp+0x50]
0x00000000000020bc <+124>: jmp 0x20be <main.rangeCopy+126>
0x00000000000020be <+126>: mov rax,QWORD PTR [rax]
0x00000000000020c1 <+129>: mov QWORD PTR [rsp+0x18],rax
0x00000000000020c6 <+134>: mov rax,QWORD PTR [rsp+0x28]
0x00000000000020cb <+139>: mov QWORD PTR [rsp+0x10],rax // 0x0
0x00000000000020d0 <+144>: mov rax,QWORD PTR [rsp+0x18]
0x00000000000020d5 <+149>: mov QWORD PTR [rsp+0x8],rax // 0x2
0x00000000000020da <+154>: mov QWORD PTR [rsp+0x30],0x100
0x00000000000020e3 <+163>: mov QWORD PTR [rsp+0x38],0x200
0x00000000000020ec <+172>: call 0x22890 <runtime.printlock>
0x00000000000020f1 <+177>: mov rax,QWORD PTR [rsp+0x10]
0x00000000000020f6 <+182>: mov QWORD PTR [rsp],rax
0x00000000000020fa <+186>: call 0x22f20 <runtime.printint>
0x00000000000020ff <+191>: call 0x22a70 <runtime.printsp>
0x0000000000002104 <+196>: mov rax,QWORD PTR [rsp+0x8]
0x0000000000002109 <+201>: mov QWORD PTR [rsp],rax
0x000000000000210d <+205>: call 0x22f20 <runtime.printint>
0x0000000000002112 <+210>: call 0x22ac0 <runtime.printnl>
0x0000000000002117 <+215>: call 0x22910 <runtime.printunlock>
0x000000000000211c <+220>: jmp 0x211e <main.rangeCopy+222>
0x000000000000211e <+222>: mov rax,QWORD PTR [rsp+0x50]
0x0000000000002123 <+227>: add rax,0x8
0x0000000000002127 <+231>: mov QWORD PTR [rsp+0x50],rax
0x000000000000212c <+236>: mov rax,QWORD PTR [rsp+0x28]
0x0000000000002131 <+241>: inc rax // 自增1
0x0000000000002134 <+244>: mov QWORD PTR [rsp+0x28],rax
0x0000000000002139 <+249>: jmp 0x20a3 <main.rangeCopy+99>
0x000000000000213e <+254>: add rsp,0x58
0x0000000000002142 <+258>: ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment