0%

gasm与nasm比较

gasm与nasm比较

gasm: AT&T语法
nasm: Intel语法

原始c文件

note02_21_assembly.c

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
#include <stdlib.h>
int global = 100;
int main(int argc, char const *argv[])
{
int stack;
static int BSS = 100;
int *p = (int *)malloc(2 * sizeof(int));
p[1] = 100;
p[0] = 0;
printf("Good");
return 0;
}

gasm汇编c得到s文件

gasm汇编c得到s文件

1
2
3
4
5
dassein@pad:~/nasm_pjt$ gcc -E note02_21_assembly.c > note02_21_assembly.i
dassein@pad:~/nasm_pjt$ gcc -S note02_21_assembly.i
dassein@pad:~/nasm_pjt$ gcc -c note02_21_assembly.s
dassein@pad:~/nasm_pjt$ gcc note02_21_assembly.o -o note02_21_assembly
dassein@pad:~/nasm_pjt$ ./note02_21_assembly

note02_21_assembly.s

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
	.file	"note02_21_assembly.c"
.globl global
.data
.align 4
.type global, @object
.size global, 4
global:
.long 100
.section .rodata
.LC0:
.string "Good"
.text
.globl main
.type main, @function
main:
mov ebp, esp; for correct debugging
mov rbp, rsp; for correct debugging
mov ebp, esp; for correct debugging
movq %rsp, %rbp #for correct debugging
movl %esp, %ebp #for correct debugging
.LFB2:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $32, %rsp
movl %edi, -20(%rbp)
movq %rsi, -32(%rbp)
movl $8, %edi
call malloc@PLT
movq %rax, -8(%rbp)
movq -8(%rbp), %rax
addq $4, %rax
movl $100, (%rax)
movq -8(%rbp), %rax
movl $0, (%rax)
leaq .LC0(%rip), %rdi
movl $0, %eax
call printf@PLT
movl $0, %eax
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE2:
.size main, .-main
.data
.align 4
.type BSS.2711, @object
.size BSS.2711, 4
BSS.2711:
.long 100
.ident "GCC: (Ubuntu 6.2.0-5ubuntu12) 6.2.0 20161005"
.section .note.GNU-stack,"",@progbits

nasm反汇编程序文件得到asm文件

先把objconv程序放到/bin中, 供cmd使用obconv
disassemble to NASM syntax .asm file

1
objconv -fnasm note02_21_assembly note02_21_assembly.asm

note02_21_assembly.asm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
	.file	"note02_21_assembly.c"
.intel_syntax noprefix
.globl global
.data
.align 4
.type global, @object
.size global, 4
global:
.long 100
.section .rodata
.LC0:
.string "Good"
.text
.globl main
.type main, @function
main:
.LFB2:
.cfi_startproc
push rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
mov rbp, rsp
.cfi_def_cfa_register 6
sub rsp, 32
mov DWORD PTR -20[rbp], edi
mov QWORD PTR -32[rbp], rsi
mov edi, 8
call malloc@PLT
mov QWORD PTR -8[rbp], rax
mov rax, QWORD PTR -8[rbp]
add rax, 4
mov DWORD PTR [rax], 100
mov rax, QWORD PTR -8[rbp]
mov DWORD PTR [rax], 0
lea rdi, .LC0[rip]
mov eax, 0
call printf@PLT
mov eax, 0
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE2:
.size main, .-main
.data
.align 4
.type BSS.2711, @object
.size BSS.2711, 4
BSS.2711:
.long 100
.ident "GCC: (Ubuntu 6.2.0-5ubuntu12) 6.2.0 20161005"
.section .note.GNU-stack,"",@progbits

objdump反汇编程序文件

1
$objdump -d note02_21_assembly > note02_21_assembly.txt # 简单反汇编, 信息传给note02_21_obj.txt

note02_21_obj.txt
可以看到main段几乎和intel gasm的asm文件完全一致

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198

note02_21_assembly: file format elf64-x86-64


Disassembly of section .init:

0000000000000570 <_init>:
570: 48 83 ec 08 sub $0x8,%rsp
574: 48 8b 05 5d 0a 20 00 mov 0x200a5d(%rip),%rax # 200fd8 <_GLOBAL_OFFSET_TABLE_+0x30>
57b: 48 85 c0 test %rax,%rax
57e: 74 02 je 582 <_init+0x12>
580: ff d0 callq *%rax
582: 48 83 c4 08 add $0x8,%rsp
586: c3 retq

Disassembly of section .plt:

0000000000000590 <.plt>:
590: ff 35 1a 0a 20 00 pushq 0x200a1a(%rip) # 200fb0 <_GLOBAL_OFFSET_TABLE_+0x8>
596: ff 25 1c 0a 20 00 jmpq *0x200a1c(%rip) # 200fb8 <_GLOBAL_OFFSET_TABLE_+0x10>
59c: 0f 1f 40 00 nopl 0x0(%rax)

Disassembly of section .plt.got:

00000000000005a0 <.plt.got>:
5a0: ff 25 22 0a 20 00 jmpq *0x200a22(%rip) # 200fc8 <_GLOBAL_OFFSET_TABLE_+0x20>
5a6: 66 90 xchg %ax,%ax
5a8: ff 25 32 0a 20 00 jmpq *0x200a32(%rip) # 200fe0 <_GLOBAL_OFFSET_TABLE_+0x38>
5ae: 66 90 xchg %ax,%ax
5b0: ff 25 42 0a 20 00 jmpq *0x200a42(%rip) # 200ff8 <_GLOBAL_OFFSET_TABLE_+0x50>
5b6: 66 90 xchg %ax,%ax

Disassembly of section .text:

00000000000005c0 <_start>:
5c0: 31 ed xor %ebp,%ebp
5c2: 49 89 d1 mov %rdx,%r9
5c5: 5e pop %rsi
5c6: 48 89 e2 mov %rsp,%rdx
5c9: 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
5cd: 50 push %rax
5ce: 54 push %rsp
5cf: 4c 8d 05 da 01 00 00 lea 0x1da(%rip),%r8 # 7b0 <__libc_csu_fini>
5d6: 48 8d 0d 63 01 00 00 lea 0x163(%rip),%rcx # 740 <__libc_csu_init>
5dd: 48 8d 3d 0c 01 00 00 lea 0x10c(%rip),%rdi # 6f0 <main>
5e4: ff 15 e6 09 20 00 callq *0x2009e6(%rip) # 200fd0 <_GLOBAL_OFFSET_TABLE_+0x28>
5ea: f4 hlt
5eb: 0f 1f 44 00 00 nopl 0x0(%rax,%rax,1)

00000000000005f0 <deregister_tm_clones>:
5f0: 48 8d 3d 21 0a 20 00 lea 0x200a21(%rip),%rdi # 201018 <__TMC_END__>
5f7: 48 8d 05 21 0a 20 00 lea 0x200a21(%rip),%rax # 20101f <__TMC_END__+0x7>
5fe: 55 push %rbp
5ff: 48 29 f8 sub %rdi,%rax
602: 48 89 e5 mov %rsp,%rbp
605: 48 83 f8 0e cmp $0xe,%rax
609: 76 15 jbe 620 <deregister_tm_clones+0x30>
60b: 48 8b 05 ae 09 20 00 mov 0x2009ae(%rip),%rax # 200fc0 <_GLOBAL_OFFSET_TABLE_+0x18>
612: 48 85 c0 test %rax,%rax
615: 74 09 je 620 <deregister_tm_clones+0x30>
617: 5d pop %rbp
618: ff e0 jmpq *%rax
61a: 66 0f 1f 44 00 00 nopw 0x0(%rax,%rax,1)
620: 5d pop %rbp
621: c3 retq
622: 0f 1f 40 00 nopl 0x0(%rax)
626: 66 2e 0f 1f 84 00 00 nopw %cs:0x0(%rax,%rax,1)
62d: 00 00 00

0000000000000630 <register_tm_clones>:
630: 48 8d 3d e1 09 20 00 lea 0x2009e1(%rip),%rdi # 201018 <__TMC_END__>
637: 48 8d 35 da 09 20 00 lea 0x2009da(%rip),%rsi # 201018 <__TMC_END__>
63e: 55 push %rbp
63f: 48 29 fe sub %rdi,%rsi
642: 48 89 e5 mov %rsp,%rbp
645: 48 c1 fe 03 sar $0x3,%rsi
649: 48 89 f0 mov %rsi,%rax
64c: 48 c1 e8 3f shr $0x3f,%rax
650: 48 01 c6 add %rax,%rsi
653: 48 d1 fe sar %rsi
656: 74 18 je 670 <register_tm_clones+0x40>
658: 48 8b 05 91 09 20 00 mov 0x200991(%rip),%rax # 200ff0 <_GLOBAL_OFFSET_TABLE_+0x48>
65f: 48 85 c0 test %rax,%rax
662: 74 0c je 670 <register_tm_clones+0x40>
664: 5d pop %rbp
665: ff e0 jmpq *%rax
667: 66 0f 1f 84 00 00 00 nopw 0x0(%rax,%rax,1)
66e: 00 00
670: 5d pop %rbp
671: c3 retq
672: 0f 1f 40 00 nopl 0x0(%rax)
676: 66 2e 0f 1f 84 00 00 nopw %cs:0x0(%rax,%rax,1)
67d: 00 00 00

0000000000000680 <__do_global_dtors_aux>:
680: 80 3d 91 09 20 00 00 cmpb $0x0,0x200991(%rip) # 201018 <__TMC_END__>
687: 75 27 jne 6b0 <__do_global_dtors_aux+0x30>
689: 48 83 3d 67 09 20 00 cmpq $0x0,0x200967(%rip) # 200ff8 <_GLOBAL_OFFSET_TABLE_+0x50>
690: 00
691: 55 push %rbp
692: 48 89 e5 mov %rsp,%rbp
695: 74 0c je 6a3 <__do_global_dtors_aux+0x23>
697: 48 8b 3d 6a 09 20 00 mov 0x20096a(%rip),%rdi # 201008 <__dso_handle>
69e: e8 0d ff ff ff callq 5b0 <_init+0x40>
6a3: e8 48 ff ff ff callq 5f0 <deregister_tm_clones>
6a8: 5d pop %rbp
6a9: c6 05 68 09 20 00 01 movb $0x1,0x200968(%rip) # 201018 <__TMC_END__>
6b0: f3 c3 repz retq
6b2: 0f 1f 40 00 nopl 0x0(%rax)
6b6: 66 2e 0f 1f 84 00 00 nopw %cs:0x0(%rax,%rax,1)
6bd: 00 00 00

00000000000006c0 <frame_dummy>:
6c0: 48 8d 3d 19 07 20 00 lea 0x200719(%rip),%rdi # 200de0 <__JCR_END__>
6c7: 48 83 3f 00 cmpq $0x0,(%rdi)
6cb: 75 0b jne 6d8 <frame_dummy+0x18>
6cd: e9 5e ff ff ff jmpq 630 <register_tm_clones>
6d2: 66 0f 1f 44 00 00 nopw 0x0(%rax,%rax,1)
6d8: 48 8b 05 09 09 20 00 mov 0x200909(%rip),%rax # 200fe8 <_GLOBAL_OFFSET_TABLE_+0x40>
6df: 48 85 c0 test %rax,%rax
6e2: 74 e9 je 6cd <frame_dummy+0xd>
6e4: 55 push %rbp
6e5: 48 89 e5 mov %rsp,%rbp
6e8: ff d0 callq *%rax
6ea: 5d pop %rbp
6eb: e9 40 ff ff ff jmpq 630 <register_tm_clones>

00000000000006f0 <main>:
6f0: 55 push %rbp
6f1: 48 89 e5 mov %rsp,%rbp
6f4: 48 83 ec 20 sub $0x20,%rsp
6f8: 89 7d ec mov %edi,-0x14(%rbp)
6fb: 48 89 75 e0 mov %rsi,-0x20(%rbp)
6ff: bf 08 00 00 00 mov $0x8,%edi
704: e8 9f fe ff ff callq 5a8 <_init+0x38>
709: 48 89 45 f8 mov %rax,-0x8(%rbp)
70d: 48 8b 45 f8 mov -0x8(%rbp),%rax
711: 48 83 c0 04 add $0x4,%rax
715: c7 00 64 00 00 00 movl $0x64,(%rax)
71b: 48 8b 45 f8 mov -0x8(%rbp),%rax
71f: c7 00 00 00 00 00 movl $0x0,(%rax)
725: 48 8d 3d 98 00 00 00 lea 0x98(%rip),%rdi # 7c4 <_IO_stdin_used+0x4>
72c: b8 00 00 00 00 mov $0x0,%eax
731: e8 6a fe ff ff callq 5a0 <_init+0x30>
736: b8 00 00 00 00 mov $0x0,%eax
73b: c9 leaveq
73c: c3 retq
73d: 0f 1f 00 nopl (%rax)

0000000000000740 <__libc_csu_init>:
740: 41 57 push %r15
742: 41 56 push %r14
744: 41 89 ff mov %edi,%r15d
747: 41 55 push %r13
749: 41 54 push %r12
74b: 4c 8d 25 7e 06 20 00 lea 0x20067e(%rip),%r12 # 200dd0 <__frame_dummy_init_array_entry>
752: 55 push %rbp
753: 48 8d 2d 7e 06 20 00 lea 0x20067e(%rip),%rbp # 200dd8 <__init_array_end>
75a: 53 push %rbx
75b: 49 89 f6 mov %rsi,%r14
75e: 49 89 d5 mov %rdx,%r13
761: 4c 29 e5 sub %r12,%rbp
764: 48 83 ec 08 sub $0x8,%rsp
768: 48 c1 fd 03 sar $0x3,%rbp
76c: e8 ff fd ff ff callq 570 <_init>
771: 48 85 ed test %rbp,%rbp
774: 74 20 je 796 <__libc_csu_init+0x56>
776: 31 db xor %ebx,%ebx
778: 0f 1f 84 00 00 00 00 nopl 0x0(%rax,%rax,1)
77f: 00
780: 4c 89 ea mov %r13,%rdx
783: 4c 89 f6 mov %r14,%rsi
786: 44 89 ff mov %r15d,%edi
789: 41 ff 14 dc callq *(%r12,%rbx,8)
78d: 48 83 c3 01 add $0x1,%rbx
791: 48 39 dd cmp %rbx,%rbp
794: 75 ea jne 780 <__libc_csu_init+0x40>
796: 48 83 c4 08 add $0x8,%rsp
79a: 5b pop %rbx
79b: 5d pop %rbp
79c: 41 5c pop %r12
79e: 41 5d pop %r13
7a0: 41 5e pop %r14
7a2: 41 5f pop %r15
7a4: c3 retq
7a5: 90 nop
7a6: 66 2e 0f 1f 84 00 00 nopw %cs:0x0(%rax,%rax,1)
7ad: 00 00 00

00000000000007b0 <__libc_csu_fini>:
7b0: f3 c3 repz retq

Disassembly of section .fini:

00000000000007b4 <_fini>:
7b4: 48 83 ec 08 sub $0x8,%rsp
7b8: 48 83 c4 08 add $0x8,%rsp
7bc: c3 retq