This is a creation in Article, where the information may have evolved or changed.
In this blog, the Quick Guide to Golang compilation, it briefly introduces the simple syntax and the special feature of the compilation in Golang. The following is a compilation of the built-in functions and related operating code in Golang, which can be added and practiced as part of the previous blog post.
The parameters of the procedure call in the Assembly are passed through the stack, and the layout on the stack is as follows:
参数3参数2参数1 <-FP保存PC <-SP......
Built-in functions: New, make, append
package mainimport ( "fmt")type new_int intvar ( gobal_1 = "this is global var")func main() { auto_1 := "this is auto_1" s0 := new(new_int) s1 := make([]int, 10) s2 := make([]int, 10) append(s2, 9999) fmt.Println(s1, s2) fmt.Println(s0)}
Here is the assembly code:
"". Main t=1 size=1936 value=0 args=0x0 locals=0x128//define function main, stack frame size is 296 bytes, 0 byte parameter (no parameter) 0x0000 00000 (builtin.go:13) TEXT "". Main+0 (SB), $296-0//threads local Storage (thread locally storage) is transferred to CX 0x0000 00000 (builtin.go:13) movq (TLS), CX//Below is the check that the stack frame size is larger than the currently allocated small 0x0009 00009 (builtin.go:13) LEAQ-168 (SP), AX 0x0011 00017 (builtin.go:13) CM PQ ax,16 (CX) 0x0015 00021 (builtin.go:13) Jhi, 30//If more than call Runtime.morestack_noctxt 0x0017 00023 (builtin.g O:13) Call, Runtime.morestack_noctxt (SB) 0x001c 00028 (builtin.go:13) JMP, 0//enlarge stack frame 0x001e 00030 (built in.go:13) Subq $296,sp 0x0025 00037 (builtin.go:13) funcdata $0,gclocals e14c7473fe07b0ccdc0fdfa1a770087b+0 (SB) 0x0025 00037 (builtin.go:13) funcdata $1,gclocals 7a70fcb413ec620f2a7a8c3ba5f394c1+0 (SB)//local variable Auto_1 0x0025 00037 (builtin.go:14) Leaq go.string. " This is Auto_1 "+0 (SB), BX//String in Golang is composed of the data itself and its length//the address of the data is moved to the BP 0x002c 00044 (builtin.go:14) movq (BX), BP//move BP to stack pointer 152 byte position 0x002f 00047 (builtin.go:14) movq BP, "". auto_1+152 (SP) Move the string length to the BP 0x0037 00055 (builtin.go:14) Movq 8 (BX), BP//move BP to the stack pointer 160 byte position 0x003b 00059 (builtin.go:14 Movq BP, "". Initialization of the auto_1+160 (SP)//New_int type//move the type itself to BX 0x0043 00067 (builtin.go:17) movq $type. " ". New_int+0 (SB), BX//Will BX move to the top of the stack 0x004a 00074 (builtin.go:17) movq BX, (SP) 0x004e 00078 (builtin.go:17) PC DATA $0,$1//Calls Runtime.newobject 0x004e 00078 (builtin.go:17) call, Runtime.newobject (SB)//moves the returned result to BX 0x0053 00083 (builtin.go:17) Movq 8 (SP), BX//move BP to stack pointer 80 byte position 0x0058 00088 (builtin.go:17) movq BX, "" . s1+80 (SP) 0x005d 00093 (builtin.go:17) NOP,//Create slice S1//move type to BX 0x005d 00093 (builtin.go:18) MOV Q $type. []int+0 (SB), BX//Will BX (type) moved to the top 0x0064 00100 (builtin.go:18) movq BX, (SP)//The length parameter Len moved to a position relative to the top of the stack 8 bytes 0x006 8 00104 (builtIN.GO:18) movq $10,8 (SP)//move capacity parameter cap to position relative to stack Top 16 bytes 0x0071 00113 (builtin.go:18) movq $10,16 (SP) 0x00 7a 00122 (builtin.go:18) PCDATA $0,$2//Invoke Runtime.makeslice 0x007a 00122 (builtin.go:18) call, runtime.ma Keslice (SB)//created S1 parameters are stored in relative stack Top 24, 32, 40 byte position 0x007f 00127 (builtin.go:18) movq (SP), DX 0x0084 00132 ( builtin.go:18) Movq (SP), CX 0x0089 00137 (builtin.go:18) movq (SP), BX//CX are saved in slice len parameter 0x008 E 00142 (builtin.go:20) movq CX, "". S2_len+64 (SP) 0x0093 00147 (builtin.go:20) NOP,//will slice the array, Len, C The AP is routed to S2 0x0093 00147 (builtin.go:21) movq DX, "". s2+168 (SP) 0x009b 00155 (builtin.go:21) movq CX, "". s2+17 6 (SP) 0x00a3 00163 (builtin.go:21) movq BX, "". s2+184 (SP) 0x00ab 00171 (builtin.go:21) movq Bx,ax//From BX minus CX value, results saved in BX 0x00ae 00174 (builtin.go:21) subq cx,bx 0x00b1 00177 (builtin.go:21) CMPQ bx,$1 0x0 0b5 00181 (builtin.go:21) Jge, 262//Transfer type to stack top 0x00b7 00183 (builtin.go:21) movq $type. []int+0 (SB), BX 0x00be 00190 (builtin.go:21) movq bx, (SP)//Transfer the old slice to the second parameter of the stack 0x00c2 00194 (builtin.go:21) Movq DX, "". autotmp_0001+240 (SP) 0x00ca 00202 (builtin.go:21) movq dx,8 (SP)//Transfer length to the third parameter on the stack (length of s2) 0 X00CF 00207 (builtin.go:21) movq CX, "". autotmp_0001+248 (SP) 0x00d7 00215 (builtin.go:21) movq cx,16 (SP) The value of the BX of 33 rows is re-saved to the (SP), which is the S2 array 0x00dc 00220 (builtin.go:21) movq AX, "". autotmp_0001+256 (SP) 0x00e4 00228 (b uiltin.go:21) movq ax,24 (SP)//Set the length of the S2 to 1 0x00e9 00233 (builtin.go:21) movq $1,32 (SP)//Call Growslice 0x00f2 00242 (builtin.go:21) PCDATA $0,$2 0x00f2 00242 (builtin.go:21) call, Runtime.growslice (SB)//3 return value (slicestruct) 0x00f7 00247 (builtin.go:21) movq (sp), DX 0X00FC 00252 (builtin.go:21) movq x (SP), C X 0x0101 00257 (builtin.go:21) movq (SP), AX//increased structure returnedLen parameter length, increased 1 0x0106 00262 (builtin.go:21) movq cx,si 0x0109 00265 (builtin.go:21) INCQ, SI//address, DX +cx*8 0x010c 00268 (builtin.go:21) Leaq (DX) (cx*8), BX//will immediately number 9999 pass to (BX) the memory location 0x0110 00272 (builtin.go:21) Movq $9999, (BX) 0x0117 00279 (builtin.go:21) NOP, 0x0117 00279 (builtin.go:21) movq DX, "". autotmp_00 01+240 (SP) 0x011f 00287 (builtin.go:21) movq SI, "". autotmp_0001+248 (SP) 0x0127 00295 (builtin.go:21) movq AX, "". autotmp_0001+256 (SP)//Pass the parameters of the new slice to S2 0x012f 00303 (builtin.go:21) movq DX, "". s2+168 (SP) 0x0137 00311 (builtin.go:21) movq SI, "". s2+176 (SP) 0x013f 00319 (builtin.go:21) movq AX, "". s2+184 (sp) 0x0147 00 327 (builtin.go:21) NOP,
Built-in functions: Copy
package mainimport ( "fmt")func main() { s1 := make([]int, 10) s2 := make([]int, 10) copy(s1, s2) fmt.Println(s1, s2)}
Here is the assembly code:
"". Main t=1 size=624 value=0 args=0x0 locals=0x108 0x0000 00000 (builtin.go:7) TEXT "". Main+0 (SB), $264-0 0x0000 0 0000 (builtin.go:7) movq (TLS), CX 0x0009 00009 (builtin.go:7) LEAQ-136 (SP), AX 0x0011 00017 (builtin.go:7) CMP Q ax,16 (CX) 0x0015 00021 (builtin.go:7) Jhi, 0x0017 00023 (builtin.go:7) call, Runtime.morestack_noctxt (SB) 0x001c 00028 (builtin.go:7) JMP, 0 0x001e 00030 (builtin.go:7) subq $264,sp 0x0025 00037 (builtin.go:7) FUNCD ATA $0,gclocals 7c13896baab3273e10662a9a37b348ce+0 (SB) 0x0025 00037 (builtin.go:7) Funcdata $1,gclocals·978d33e77 17760c8870a69548dc9a2fb+0 (SB)//Create slice S1 0x0025 00037 (builtin.go:8) movq $type. []int+0 (SB), BX 0x002c 00044 (builtin.go:8) movq bx, (SP) 0x0030 00048 (builtin.go:8) movq $10,8 (sp) 0x0039 0 0057 (Builtin.go:8) movq $10,16 (SP) 0x0042 00066 (builtin.go:8) PCDATA $0,$0 0x0042 00066 (builtin.go:8) call , Runtime.makeslice (SB) 0x0047 00071 (builtin.go:8) MOVQ (SP), DX 0x004c 00076 (builtin.go:8) movq (sp), CX 0x0051 00081 (builtin.go:8) movq (sp), AX 0x0056 00086 (builtin.go:8) movq DX, "". s1+88 (SP) 0x005b 00091 (builtin.go:8) movq CX, "". s1+96 (SP) 0x0060 00096 (Buil Tin.go:8) movq AX, "". s1+104 (SP)//create slice S2 0x0065 00101 (builtin.go:9) movq $type. []int+0 (SB), BX 0x006c 00108 (builtin.go:9) movq bx, (SP) 0x0070 00112 (builtin.go:9) movq $10,8 (sp) 0x0079 0 0121 (builtin.go:9) movq $10,16 (SP) 0x0082 00130 (builtin.go:9) PCDATA $0,$1 0x0082 00130 (builtin.go:9) call , Runtime.makeslice (SB) 0x0087 00135 (builtin.go:9) movq (sp), DX 0x008c 00140 (builtin.go:9) movq (sp), CX 0x0091 00145 (Builtin.go:9) movq (SP), AX//save S1 data to Si, BP, BX 0x0096 00150 (builtin.go:11) movq "". S 1+88 (SP), SI 0x009b 00155 (builtin.go:11) movq "". s1+96 (SP), BP 0x00a0 00160 (builtin.go:11) movq "". s1+10 4 (SP), BX//Save BX to 224 (SP) 0x00a5 00165 (builtin. go:11) Movq BX, "". autotmp_0005+224 (SP)//Save the S2 data on the stack 0X00AD 00173 (builtin.go:11) movq DX, "". s2+64 (SP) 0X00B2 00178 (builtin.go:11) movq CX, "". s2+72 (SP) 0x00b7 00183 (builtin.go:11) movq AX, "". s2+80 (SP) Save Ax to (SP) 0X00BC 00188 (builtin.go:11) movq ax, "". autotmp_0006+152 (SP)//Save BP to 216 (sp) 0x00c4 00196 (builtin.go:11) MOVQ BP, "". autotmp_0000+216 (SP)//from 204 to 220 is a comparison of the length of S1 and S2//which is larger just use it as parameter//copy BP to Ax, BP saved is S1 len 0x00cc 0020 4 (builtin.go:11) movq bp,ax//Save CX to 144 (SP) 0x00cf 00207 (builtin.go:11) movq cx, "". autotmp_0006+144 (S P)//Compare the length of S2 (CX) and S1 (BP)//If less than 0 jump to 223//otherwise perform the 0x00d7 00215 (builtin.go:11) CMPQ cx,bp 0x00da 00218 (builtin.go:11) jge, 223 0x00dc 00220 (builtin.go:11) movq Cx,ax//s1 array as the first parameter 0X00DF 0 0223 (builtin.go:11) movq si, "". autotmp_0000+208 (SP) 0x00e7 00231 (builtin.go:11) movq SI, (SP)//Will S2 AR Ray madeFor the second parameter 0x00eb 00235 (builtin.go:11) movq dx, "". autotmp_0006+136 (SP) 0x00f3 00243 (builtin.go:11) movq DX , 8 (SP)//AX is the length of the S2 0x00f8 00248 (builtin.go:11) movq ax,bx//SHL left shift operation, left shift 3-bit 0X00FB 00251 (builtin. GO:11) SHLQ $3,BX//BX as a third parameter 0x00ff 00255 (builtin.go:11) movq bx,16 (SP) 0x0104 00260 (builtin.go: One) PCDATA $0,$2 0x0104 00260 (builtin.go:11) call, Runtime.memmove (SB)
Function call
package mainimport ( "fmt")func f1(a int) int { fmt.Println(a) return a + 123}func main() { b := f1(123) fmt.Println(b)}
Here is the assembly code:
"". F1 t=1 size=240 value=0 args=0x10 locals=0x70 0x0000 00000 (builtin.3.go:7) TEXT "". F1+0 (SB), $112-16 0x0000 00000 (Builtin.3.go:7) movq (TLS), CX 0x0009 00009 (builtin.3.go:7) CMPQ sp,16 (CX) 0x000d 00013 (builtin.3. Go:7) Jhi, 0x000f 00015 (builtin.3.go:7) call, Runtime.morestack_noctxt (SB) 0x0014 00020 (Builtin.3.go:7) JMP, 0 0x0016 00022 (builtin.3.go:7) subq $112,sp 0x001a 00026 (builtin.3.go:7) funcdata $0,gclocals 480 B8f52e76ecc554a5236babfd9a6e5+0 (SB) 0x001a 00026 (builtin.3.go:7) funcdata $1,gclocals 403a8d79fd24b295e8557f69704 97aa3+0 (SB)//Because the size of the stack frame is 112 bytes//So the first parameter is saved at (FP) 0x001a 00026 (builtin.3.go:8) movq "". a+120 (FP), BX/ /The following code converts a to interface type//because FMT. PRINTLN requires an empty interface type parameter 0x001f 00031 (builtin.3.go:8) movq BX, "". autotmp_0001+48 (SP) 0x0024 00036 (Builtin.3.go:8) Leaq "". autotmp_0000+72 (SP), BX 0x0029 00041 (builtin.3.go:8) Movq $, (BX) 0x0030 00048 (builtin.3.go:8) Movq $0,8 (BX) 0x0038 00056 (builtin.3.go:8) Leaq "". autotmp_0000+72 (SP), BX 0x003d 00061 (builtin.3.go:8 ) CMPQ bx,$0 0x0041 00065 (builtin.3.go:8) JEQ $1,226 0x0047 00071 (builtin.3.go:8) movq $1,dx 0x004e 00078 (builtin.3.go:8) movq $1,cx 0x0055 00085 (builtin.3.go:8) movq BX, "". autotmp_0002+88 (SP) 0x005a 000 (builtin.3.go:8) movq DX, "". autotmp_0002+96 (SP) 0x005f 00095 (builtin.3.go:8) movq CX, "". autotmp_0002+104 ( SP) 0x0064 00100 (builtin.3.go:8) movq $type. int+0 (SB), BX 0x006b 00107 (builtin.3.go:8) movq BX, (SP) 0x 006f 00111 (builtin.3.go:8) Leaq "". autotmp_0001+48 (SP), BX 0x0074 00116 (builtin.3.go:8) movq bx,8 (SP) 0x0 079 00121 (builtin.3.go:8) PCDATA $0,$1 0x0079 00121 (builtin.3.go:8) call, runtime.convt2e (SB) 0x007e 00126 (Builtin.3.go:8) MOVQ (SP), CX 0x0083 00131 (builtin.3.go:8) movq (sp), AX 0x0088 00136 (builtin.3.go:8) movq "". Autot Mp_0002+88 (SP), BX 0x008d 00141 (builtin.3.go:8) movq BX, (sp) 0x0091 00145 (builtin.3.go:8) movq CX, "". Autotmp_ 0004+56 (SP) 0x0096 00150 (builtin.3.go:8) movq cx,8 (SP) 0x009b 00155 (builtin.3.go:8) movq AX, "". Autotmp_0 004+64 (SP) 0x00a0 00160 (builtin.3.go:8) movq ax,16 (SP) 0x00a5 00165 (builtin.3.go:8) PCDATA $0,$1 0x00a5 00165 (builtin.3.go:8) call, Runtime.writebarrieriface (SB)//pass parameters to FMT. Println, and call 0x00aa 00170 (builtin.3.go:8) movq "". autotmp_0002+88 (SP), BX 0x00af 00175 (builtin.3.go:8) movq BX, (SP) 0x00b3 00179 (builtin.3.go:8) movq "". autotmp_0002+96 (SP), BX 0x00b8 00184 (builtin.3.go:8) movq bx,8 (SP) 0X00BD 00189 (builtin.3.go:8) movq "". autotmp_0002+104 (SP), BX 0x00c2 00194 (builtin.3.go:8) movq bx,16 (SP) 0x00c7 00199 (builtin.3.go:8) PCDATA $0,$2 0x00c7 00199 (builtin.3.go:8) call, FMT. PRINTLN (SB)//Adds a plus 123 and returns 0x00cc 00204 (Builtin.3.go:9) movq "". a+120(FP), BX 0x00d1 00209 (builtin.3.go:9) addq $123,bx 0x00d5 00213 (builtin.3.go:9) movq BX, "". ~r1+128 (FP) 0X00DD 00221 (builtin.3.go:9) addq $112,sp 0x00e1 00225 (builtin.3.go:9) RET, 0x00e2 00226 (Builtin.3.go:8) Movl AX, (BX) 0x00e4 00228 (builtin.3.go:8) JMP, "". Main t=1 size=240 value=0 args=0x0 locals=0x70 0x0000 00000 (builtin.3.go:12) TEXT "". Main+0 (SB), $112-0 0x0000 00000 (builtin.3.go:12) movq (TLS), CX 0x 0009 00009 (builtin.3.go:12) CMPQ sp,16 (CX) 0x000d 00013 (builtin.3.go:12) Jhi, 0x000f 00015 (builtin.3.go:1 2) Call, Runtime.morestack_noctxt (SB) 0x0014 00020 (builtin.3.go:12) JMP, 0///SP minus 112 (stack size), stack down 0x0016 0 0022 (builtin.3.go:12) subq $112,sp 0x001a 00026 (builtin.3.go:12) funcdata $0,gclocals 73423680ca5f2d7df4fe760 A82d507fb+0 (SB) 0x001a 00026 (builtin.3.go:12) funcdata $1,gclocals 403a8d79fd24b295e8557f6970497aa3+0 (SB)//Will 1 23 placed on top of the stack 0x001a 00026 (builtin.3.go:13) Movq $123, (SP) 0x0022 00034 (builtin.3.go:13) PCDATA $0,$0//Call F1 0x0022 00034 (builtin.3 . go:13) Call, "". F1 (SB)//Results 8 (SP) is deposited into BX 0x0027 00039 (builtin.3.go:13) Movq 8 (SP), BX 0x002c 00044 (builti N.3.GO:13) NOP,//will be stored in the BX (SP), is a temporary variable, BX is saved in the F1 call result 0x002c 00044 (builtin.3.go:14) movq BX, "". autotmp_0010 +48 (SP)