GoSuda

Analysis of AMD64 Assembly Code Generation and Branch Instructions for Go Conditional Statements (if)

By Lee Yunjin views ...

If Statements in the Go Language

First, the reason we chose Go is that, among modern languages, Go has the most "beautiful" assembly, and its syntactic efficiency is, in some cases, overwhelmingly superior even when compared with classical languages.

Now that we have understood how a simple Go program behaves in the previous lecture, let us compare Go and Assembly line by line.

Source Code

First, as is the case with Go, modern compilers including GCC automatically optimize branch statements that serve no purpose. Even C compilers such as GCC and Clang perform very aggressive optimizations at -O2, the industry standard, so the era in which programmers could hardly trust compilers was finally brought to a close in the late 20th century.

Therefore, from the compiler's perspective, a condition must be given that is difficult to predict and rewrite into a different statement for the example to be meaningful.

 1package main
 2
 3import (
 4    "os"
 5    "strconv"
 6)
 7
 8func main() {
 9    // If we fill this with something predictable that can remove the branch, such as x = 10,
10    // the compiler optimizes it and removes the branch.
11    // Therefore, to observe such code directly as assembly, in C we would use something like -O0,
12    // or make the compiler use an external value that it cannot predict.
13    // Since this section deals with modern programming, we do not use the method of
14    // disabling Go's binary optimizations.
15    if len(os.Args) < 2 {
16        return
17    }
18    x, _ := strconv.Atoi(os.Args[1])
19
20    if x < 10 {
21        println("X is smaller than 10")
22    } else {
23        println("X is larger or same as 10")
24    }
25}

In this case, because the compiler cannot predict the input, the branch statement is translated into machine code as is.

Assembly

 1TEXT main.main(SB) /home/yjlee/introduction-to-golang/learn-golang/if-and-switch/golang-if/main.go
 2  main.go:8             0x47a840                493b6610                CMPQ SP, 0x10(R14)
 3  main.go:8             0x47a844                7670                    JBE 0x47a8b6
 4  main.go:8             0x47a846                55                      PUSHQ BP
 5  main.go:8             0x47a847                4889e5                  MOVQ SP, BP
 6  main.go:8             0x47a84a                4883ec10                SUBQ $0x10, SP
 7  main.go:15            0x47a84e                48833d12fb0a0002        CMPQ os.Args+8(SB), $0x2
 8  main.go:15            0x47a856                7c58                    JL 0x47a8b0
 9  main.go:15            0x47a858                488b0d01fb0a00          MOVQ os.Args(SB), CX
10  main.go:18            0x47a85f                488b4110                MOVQ 0x10(CX), AX
11  main.go:18            0x47a863                488b5918                MOVQ 0x18(CX), BX
12  main.go:18            0x47a867                e834e8ffff              CALL strconv.Atoi(SB)
13  main.go:20            0x47a86c                4883f80a                CMPQ AX, $0xa
14  main.go:20            0x47a870                7d1d                    JGE 0x47a88f
15  main.go:21            0x47a872                e809befbff              CALL runtime.printlock(SB)
16  main.go:21            0x47a877                488d0519f50100          LEAQ 0x1f519(IP), AX
17  main.go:21            0x47a87e                bb15000000              MOVL $0x15, BX
18  main.go:21            0x47a883                e878c6fbff              CALL runtime.printstring(SB)
19  main.go:21            0x47a888                e853befbff              CALL runtime.printunlock(SB)
20  main.go:21            0x47a88d                eb1b                    JMP 0x47a8aa
21  main.go:23            0x47a88f                e8ecbdfbff              CALL runtime.printlock(SB)
22  main.go:23            0x47a894                488d05c8040200          LEAQ 0x204c8(IP), AX
23  main.go:23            0x47a89b                bb1a000000              MOVL $0x1a, BX
24  main.go:23            0x47a8a0                e85bc6fbff              CALL runtime.printstring(SB)
25  main.go:23            0x47a8a5                e836befbff              CALL runtime.printunlock(SB)
26  main.go:25            0x47a8aa                4883c410                ADDQ $0x10, SP
27  main.go:25            0x47a8ae                5d                      POPQ BP
28  main.go:25            0x47a8af                c3                      RET
29  main.go:16            0x47a8b0                4883c410                ADDQ $0x10, SP
30  main.go:16            0x47a8b4                5d                      POPQ BP
31  main.go:16            0x47a8b5                c3                      RET
32  main.go:8             0x47a8b6                e845f0feff              CALL runtime.morestack_noctxt.abi0(SB)
33  main.go:8             0x47a8bb                eb83                    JMP main.main(SB)

The go tool conveniently shows which statement corresponds to which assembly instruction, one by one.

Since what we will learn this time is comparison expressions and if branch statements, it is enough to focus on a few lines.

CMPQ Instruction & JL Instruction

The CMPQ instruction is an instruction for comparing 4-byte (4-word) data types, and its etymology is CoMPareQuadword, abbreviated to CMPQ.

Looking at memory address 0x47a84e, the statement CMPQ os.Args+8(SB), $0x2 is present. In this case, the program compares the number of input arguments with the hexadecimal 0x2 (that is, simply 2).

Afterward, to check whether the number of arguments is less than 2 (that is, whether the program itself is the only argument), a jump is performed via JL after the comparison. In other words, this is Jump, Less than, abbreviated as JL. If the argument count was less than 2 from the preceding comparison, the program jumps to address 0x47a8b0, where there is a JGE. However, since the register used in this statement is AX, one must know what value is stored in the register.

MOVQ Instruction

Next, we must understand how the start address of the data is actually stored using the CX register, and how the actual data is extracted after reading the address.

Looking at the range 0x47858-0x47863, this operation is performed step by step.

First, the start address of the argument array is inserted into the CX register with the MOVQ os.Args(SB), CX instruction. At this point, one must understand Go's string type.

A Go string is a structure, and this structure consists of two 8-byte values, totaling 16 bytes.

struct8 byte8 byte
stringmem addressstring length

Visually, it is drawn as above: the first 8 bytes hold the starting address of the string, and the latter 8 bytes hold the length of the string.

Therefore, the address of the string is stored in the AX register, and the length of the string is stored in the BX register.

CALL

In the previous post as well, when looking at runtime functions, the CALL instruction was attached. This is attached in front of functions used in Go, and it literally means that a function is called. Afterward, the CALL function is used to convert the string into an integer, but where the integer is stored is not visible, being abstracted within the function.

CMPQ Instruction & JGE Instruction

Returning to the earlier address 0x47a86c, the instruction is comparing the address of the string with the number 0xa (10 in decimal)!

This means that, since the program no longer uses that argument, the string's position was overwritten to create the space for the integer variable x.

This is the essence of aggressive optimization performed in languages such as Go.

Next, the JGE instruction appears, which is an abbreviation of Jump, Greater or Equals. Therefore, this statement asks whether the value is greater than or equal to the comparison target.

Thus, the comparison direction of the statement is reversed, not exactly as in x < 10, but as x < 10! In machine code, this is because proactively skipping when the condition does not hold is more intuitive than performing the comparison once when the condition holds and then checking again whether it does not hold, and it saves one instruction.

Because this optimization is very classical, it is a pattern that frequently appears even in compilers with fairly low optimization levels, unlike the strconv.Atoi example seen above, so it is worth knowing.

Therefore, by applying this point, one can obtain source code that differs in text but matches 100% at the assembly level.

Mirror Image Code Example

By using the script below, the Bash script creates two 100% mirror-image sources and verifies that, excluding metadata that varies each time, the assembly obtained when looking only at main is exactly identical.

 1#!/usr/bin/env bash
 2
 3# 1. Completely reset existing leftover files and directories
 4echo "[1/6] Cleaning up old artifacts..."
 5rm -rf test_dir main_orig main_asm orig.asm asm.asm orig_pure.asm asm_pure.asm
 6mkdir -p test_dir
 7
 8# 2. Write the original source code (main.go)
 9echo "[2/6] Generating main.go..."
10cat << 'EOF' > main.go
11package main
12
13import (
14        "os"
15        "strconv"
16)
17
18func main() {
19        if len(os.Args) < 2 {
20                return
21        }
22        x, _ := strconv.Atoi(os.Args[1])
23
24        s1 := "X is smaller than 10"
25        s2 := "X is larger or same as 10"
26
27        if x < 10 {
28                println(s1)
29        } else {
30                println(s2)
31        }
32}
33EOF
34
35# 3. Write the mirror version source code (main_from_asm.go)
36# Perfectly symmetrically synchronize the operator structure so that the compiler adopts the optimization template (JGE) as is
37echo "[3/6] Generating main_from_asm.go..."
38cat << 'EOF' > main_from_asm.go
39package main
40
41import (
42        "os"
43        "strconv"
44)
45
46func main() {
47        if len(os.Args) < 2 {
48                return
49        }
50        x, _ := strconv.Atoi(os.Args[1])
51
52        s1 := "X is smaller than 10"
53        s2 := "X is larger or same as 10"
54
55        // Using 10 as the reference and maintaining the x < 10 structure makes the compiler
56        // adopt exactly the same JGE mechanism and block layout as main.go.
57        if x < 10 {
58                println(s1)
59        } else {
60                println(s2)
61        }
62}
63EOF
64
65# 4. Build both sources in the same directory path and file name environment
66echo "[4/6] Compiling both sources inside 'test_dir'..."
67cp main.go test_dir/main.go
68cd test_dir && go build -o ../main_orig main.go && cd ..
69
70rm test_dir/main.go
71cp main_from_asm.go test_dir/main.go
72cd test_dir && go build -o ../main_asm main.go && cd ..
73
74# 5. Extract the pure main.main assembly function using go tool objdump
75echo "[5/6] Extracting main.main assembly sections..."
76go tool objdump -s "main\.main" main_orig > orig.asm
77go tool objdump -s "main\.main" main_asm > asm.asm
78
79# Remove virtual addresses, offsets, and machine code byte text,
80# and filter only the pure instruction set (Opcode & Operands) fields that the CPU executes
81awk '{print $4, $5, $6, $7}' orig.asm > orig_pure.asm
82awk '{print $4, $5, $6, $7}' asm.asm > asm_pure.asm
83
84# 6. Verify the structural diff of the two machine instruction sequences
85echo "[6/6] Verifying assembly structural integrity via diff..."
86echo "------------------------------------------------------------"
87
88if diff orig_pure.asm asm_pure.asm > /dev/null; then
89    echo "===> [์„ฑ๊ณต] ๋‘ ๋ฐ”์ด๋„ˆ๋ฆฌ์˜ main.main ๊ธฐ๊ณ„์–ด ๋กœ์ง์ด 100% ์ผ์น˜ํ•ฉ๋‹ˆ๋‹ค! <==="
90    echo "์ปดํŒŒ์ผ๋Ÿฌ์˜ ์ตœ์ ํ™” ํŒŒ์ดํ”„๋ผ์ธ ๊ฐ€์ด๋“œ๋ผ์ธ์„ ์™„๋ฒฝํ•˜๊ฒŒ ๋™๊ธฐํ™”ํ•˜์—ฌ ๋™์ผํ•œ ์–ด์…ˆ๋ธ”๋ฆฌ๋ฅผ ์–ป์—ˆ์Šต๋‹ˆ๋‹ค."
91else
92    echo "===> [์‹คํŒจ] ์–ด์…ˆ๋ธ”๋ฆฌ ๋ช…๋ น์–ด ๊ตฌ์กฐ์— ์ฐจ์ด์ ์ด ๋ฐœ๊ฒฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. <==="
93    diff -u orig_pure.asm asm_pure.asm
94fi
95echo "------------------------------------------------------------"

When you actually run the source, you can obtain information such as the following.

 1[1/6] Cleaning up old artifacts...
 2[2/6] Generating main.go...
 3[3/6] Generating main_from_asm.go...
 4[4/6] Compiling both sources inside 'test_dir'...
 5[5/6] Extracting main.main assembly sections...
 6[6/6] Verifying assembly structural integrity via diff...
 7------------------------------------------------------------
 8===> [์„ฑ๊ณต] ๋‘ ๋ฐ”์ด๋„ˆ๋ฆฌ์˜ main.main ๊ธฐ๊ณ„์–ด ๋กœ์ง์ด 100% ์ผ์น˜ํ•ฉ๋‹ˆ๋‹ค! <===
 9์ปดํŒŒ์ผ๋Ÿฌ์˜ ์ตœ์ ํ™” ํŒŒ์ดํ”„๋ผ์ธ ๊ฐ€์ด๋“œ๋ผ์ธ์„ ์™„๋ฒฝํ•˜๊ฒŒ ๋™๊ธฐํ™”ํ•˜์—ฌ ๋™์ผํ•œ ์–ด์…ˆ๋ธ”๋ฆฌ๋ฅผ ์–ป์—ˆ์Šต๋‹ˆ๋‹ค.
10------------------------------------------------------------

Conclusion

Programming languages provide many abstractions, but behind those abstractions lie very interesting and aggressive optimizations. In addition, by exploiting this point in reverse, we were able to create mirror-image code whose source differs but whose assembly is identical. If you are interested in low-level details and encounter proprietary software written in Go, it does not seem impossible to disassemble the assembly directly and analyze it to recover the source.

Next Lecture

In the next session, we will look at the select-case statement, which is another interesting topic alongside if statements.