#include
int global;
int func_op(void) { return 0; }
void func(void)
{
unsigned long long val64 = 0;
val64 = 0xffffeeeeddddcccc; // 7
global = 0x5555; // 8
}
#define MAX_WORD 16
int main(void)
{
unsigned int i = 0;
char words[MAX_WORD] = “Hello World”;
char word;
int (*func_pointer)(void) = &func_op;
i = 0xabcd; // 1
if (i != 0x1234) // 2
i = 0; // 3
while (i == 0) // 4
{
i++; // 5
}
func(); // 6
i = func_pointer(); // 9
for (i = 0; i < MAX_WORD - 1; i++) // 10
word = words[i]; // 11
return 0; // 12
}
$ gcc -Wall -o0 assemble.c -o assemble
⨯
assemble.c: In function ‘func’:
assemble.c:8:21: warning: variable ‘val64’ set but not used [-Wunused-but-set-variable]
8 | unsigned long long val64 = 0;
| ^~~~~
assemble.c: In function ‘main’:
assemble.c:19:7: warning: variable ‘word’ set but not used [-Wunused-but-set-variable]
19 | char word;
| ^~~~
$ objdump -d --no-show-raw-insn assemble
–no-show-raw-insn 使其不输出机器语言
assemble: file format elf64-x86-64
Disassembly of section .init:
…
0000000000001130 :
1130: push %rbp
1131: mov %rsp,%rbp
1134: movq $0x0,-0x8(%rbp)
113c: movabs $0xffffeeeeddddcccc,%rax # val64 = 0xffffeeeeddddcccc; // 7
1146: mov %rax,-0x8(%rbp)
114a: movl $0x5555,0x2ed8(%rip) # 402c global = 0x5555; // 8
1154: nop
1155: pop %rbp
1156: ret
0000000000001157 :
1157: push %rbp
1158: mov %rsp,%rbp # 初始化栈,对应()
115b: sub $0x30,%rsp
115f: movl $0x0,-0x4(%rbp)
1166: movabs $0x6f57206f6c6c6548,%rax
1170: mov $0x646c72,%edx
1175: mov %rax,-0x30(%rbp)
1179: mov %rdx,-0x28(%rbp)
117d: lea -0x5f(%rip),%rax # 1125
1184: mov %rax,-0x10(%rbp)
# 设置变量的值
1188: movl $0xabcd,-0x4(%rbp) # i = 0xabcd; // 1
# if比较
118f: cmpl $0x1234,-0x4(%rbp) # if (i != 0x1234) // 2
1196: je 11a5 # 如果相等
1198: movl $0x0,-0x4(%rbp) # i = 0; // 3
119f: jmp 11a5 # 跳转 // 2
# while
11a1: addl $0x1,-0x4(%rbp) # i++; // 5
11a5: cmpl $0x0,-0x4(%rbp)
11a9: je 11a1
# 函数调用call
11ab: call 1130 # func(); // 6
11b0: mov -0x10(%rbp),%rax
# 函数指针调用
11b4: call *%rax # i = func_pointer(); // 9
11b6: mov %eax,-0x4(%rbp)
# for
11b9: movl $0x0,-0x4(%rbp) # word = words[i]; // 11
11c0: jmp 11d1
11c2: mov -0x4(%rbp),%eax
# 数组操作:movzbl
11c5: movzbl -0x30(%rbp,%rax,1),%eax
11ca: mov %al,-0x11(%rbp)
11cd: addl $0x1,-0x4(%rbp)
11d1: cmpl $0xe,-0x4(%rbp)
11d5: jbe 11c2
# 返回值设置,返回值放到eax
11d7: mov $0x0,%eax # return 0; // 12
11dc: leave
11dd: ret
11de: xchg %ax,%ax