简书链接:汇编与c代码比较以及idalinux调试感受笔记
文章字数:1540,阅读全文大约需要6分钟

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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void myFunctiontrue1()
{
}
void myFunctionfalse1()
{
}
void myFunctiontrue2()
{
}
void myFunctionfalse2()
{
}
void myFunctiontrue3()
{
}
void myFunctionfalse3()
{
}
void myFunctiontrue4()
{
}
void myFunctionfalse4()
{
}
void myFunctiontrue5()
{
}
void myFunctionfalse5()
{
}
int myfunc6True()
{
int random = rand() % time(0);
if (random > 10)
{
return 20;
}
return 1;
}

int main()
{
int random = rand() % time(0);
printf("hello random %d\n", random);
int a = random;
int b = 2;
int c = 5;
a:
if (a - 2 > 2)
{
myFunctiontrue1();
}
else
{
myFunctionfalse1();
}
int jumpBool = a - 2 > 2;
if (jumpBool)
{
myFunctiontrue2();
}
else
{
myFunctionfalse2();
}
jumpBool = a - 2 > 2;
if (jumpBool > 5)
{
myFunctiontrue3();
}
else
{
myFunctionfalse3();
}
jumpBool = a - 2 > 2;
if (jumpBool < 5)
{
myFunctiontrue4();
}
else
{
myFunctionfalse4();
}

if (jumpBool > 5 || jumpBool < 2)
{
myFunctiontrue5();
}

int bb=myfunc6True();

myb:
c++;

if (c < 10)
{
printf("%d<10, gotb ++\n", c);
goto myb;
}
else
{
if (c < 15)
{
printf("%d<15, gotb ++ \n", c);
goto myb;
}
}
printf(" end %d" ,bb);

return 0;
}
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
	.file	"arm.c"
.text
.globl myFunctiontrue1
.type myFunctiontrue1, @function
myFunctiontrue1:
.LFB2:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
nop
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE2:
.size myFunctiontrue1, .-myFunctiontrue1
.globl myFunctionfalse1
.type myFunctionfalse1, @function
myFunctionfalse1:
.LFB3:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
nop
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE3:
.size myFunctionfalse1, .-myFunctionfalse1
.globl myFunctiontrue2
.type myFunctiontrue2, @function
myFunctiontrue2:
.LFB4:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
nop
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE4:
.size myFunctiontrue2, .-myFunctiontrue2
.globl myFunctionfalse2
.type myFunctionfalse2, @function
myFunctionfalse2:
.LFB5:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
nop
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE5:
.size myFunctionfalse2, .-myFunctionfalse2
.globl myFunctiontrue3
.type myFunctiontrue3, @function
myFunctiontrue3:
.LFB6:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
nop
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE6:
.size myFunctiontrue3, .-myFunctiontrue3
.globl myFunctionfalse3
.type myFunctionfalse3, @function
myFunctionfalse3:
.LFB7:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
nop
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE7:
.size myFunctionfalse3, .-myFunctionfalse3
.globl myFunctiontrue4
.type myFunctiontrue4, @function
myFunctiontrue4:
.LFB8:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
nop
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE8:
.size myFunctiontrue4, .-myFunctiontrue4
.globl myFunctionfalse4
.type myFunctionfalse4, @function
myFunctionfalse4:
.LFB9:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
nop
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE9:
.size myFunctionfalse4, .-myFunctionfalse4
.globl myFunctiontrue5
.type myFunctiontrue5, @function
myFunctiontrue5:
.LFB10:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
nop
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE10:
.size myFunctiontrue5, .-myFunctiontrue5
.globl myFunctionfalse5
.type myFunctionfalse5, @function
myFunctionfalse5:
.LFB11:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
nop
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE11:
.size myFunctionfalse5, .-myFunctionfalse5
.globl myfunc6True
.type myfunc6True, @function
myfunc6True:
.LFB12:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
pushq %rbx
subq $24, %rsp
.cfi_offset 3, -24
call rand
movslq %eax, %rbx
movl $0, %edi
call time
movq %rax, %rcx
movq %rbx, %rax
cqto
idivq %rcx
movq %rdx, %rax
movl %eax, -20(%rbp)
cmpl $10, -20(%rbp)
jle .L12
movl $20, %eax
jmp .L13
.L12:
movl $1, %eax
.L13:
addq $24, %rsp
popq %rbx
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE12:
.size myfunc6True, .-myfunc6True
.section .rodata
.LC0:
.string "hello random %d\n"
.LC1:
.string "%d<10, gotb ++\n"
.LC2:
.string "%d<15, gotb ++ \n"
.LC3:
.string " end %d"
.text
.globl main
.type main, @function
main:
.LFB13:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
pushq %rbx
subq $40, %rsp
.cfi_offset 3, -24
call rand
movslq %eax, %rbx
movl $0, %edi
call time
movq %rax, %rcx
movq %rbx, %rax
cqto
idivq %rcx
movq %rdx, %rax
movl %eax, -36(%rbp)
movl -36(%rbp), %eax
movl %eax, %esi
movl $.LC0, %edi
movl $0, %eax
call printf
movl -36(%rbp), %eax
movl %eax, -32(%rbp)
movl $2, -28(%rbp)
movl $5, -40(%rbp)
.L15:
movl -32(%rbp), %eax
subl $2, %eax
cmpl $2, %eax
jle .L16
movl $0, %eax
call myFunctiontrue1
jmp .L17 //直接执行.L17 .L17是主程序的部分代码
.L16:
movl $0, %eax
call myFunctionfalse1 //s顺序执行
.L17:
movl -32(%rbp), %eax
subl $2, %eax
cmpl $2, %eax
setg %al
movzbl %al, %eax
movl %eax, -24(%rbp)
cmpl $0, -24(%rbp)
je .L18 //成立就跳转到 L18 下面 不会走
movl $0, %eax
call myFunctiontrue2
jmp .L19
.L18:
movl $0, %eax
call myFunctionfalse2
.L19:
movl -32(%rbp), %eax
subl $2, %eax
cmpl $2, %eax
setg %al
movzbl %al, %eax
movl %eax, -24(%rbp)
cmpl $5, -24(%rbp)
jle .L20
movl $0, %eax
call myFunctiontrue3
jmp .L21
.L20:
movl $0, %eax
call myFunctionfalse3
.L21:
movl -32(%rbp), %eax
subl $2, %eax
cmpl $2, %eax
setg %al
movzbl %al, %eax
movl %eax, -24(%rbp)
cmpl $4, -24(%rbp)
jg .L22
movl $0, %eax
call myFunctiontrue4
jmp .L23
.L22:
movl $0, %eax
call myFunctionfalse4
.L23:
cmpl $5, -24(%rbp)
jg .L24
cmpl $1, -24(%rbp)
jg .L25
.L24:
movl $0, %eax
call myFunctiontrue5
.L25:
movl $0, %eax
call myfunc6True
movl %eax, -20(%rbp) //取返回值
.L26:
addl $1, -40(%rbp)
cmpl $9, -40(%rbp)
jg .L27
movl -40(%rbp), %eax
movl %eax, %esi
movl $.LC1, %edi
movl $0, %eax
call printf
jmp .L26
.L27:
cmpl $14, -40(%rbp)
jg .L28
movl -40(%rbp), %eax
movl %eax, %esi
movl $.LC2, %edi
movl $0, %eax
call printf
jmp .L26
.L28:
movl -20(%rbp), %eax
movl %eax, %esi
movl $.LC3, %edi
movl $0, %eax
call printf
movl $0, %eax
addq $40, %rsp
popq %rbx
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE13:
.size main, .-main
.ident "GCC: (Ubuntu 5.4.0-6ubuntu1~16.04.11) 5.4.0 20160609"
.section .note.GNU-stack,"",@progbits

image.png

image.png

image.png
image.png

image.png

image.png
image.png

image.png
image.png

大于跳``` jge```大于等于跳
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
```jmp```无条件跳
```call```调用方法
```bne``` 不等于跳转
```jle```小于等于跳
```JNB al, bl ```;al里的内容不等于bl时跳转
```JBE al, bl ```;al里的内容小于或等于bl时跳转
;A(above)大于,B(below)小于,E(equal)等于,用于比较无符号数
;G(great)大于,L(less than)小于, E(equal)等于,用于比较带符号数

cmp 比较
jnz 不等于
jz 等于

有符号
jg 大于 great
jl 小于 less
无符号
ja 大于
jb 小于
n 夹在中间 表示否定
e 跟到后面 表示肯定

jge jg 是大于 jge大于等于 great equal
jle 小于等于 less equal
jnb 不小于 //大于等于 无符号
jbe 小于等于 无符号

![image.png](https://upload-images.jianshu.io/upload_images/2815884-1b9baa2f711454da.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


--debug

汇编语言里 eax, ebx, ecx, edx, esi, edi, ebp, esp这些都是什么意思啊?
赋值操作常用 ,而 edi 通常是字符串赋值。如本程序的``` move edi,offset aD15Gotb;```就是把地址偏移这里的内容赋值给```edi```
https://blog.csdn.net/gettogetto/article/details/76793553


c代码演示

main(){
int a,b,c;
int i;
int ib;
int ic;
int k=0;
scanf(“%d %d %d”,&a,&b,&c);//取了地址则代表从内存中申请内存而不是从cpu,寄存器用si之类的。
for(i=0;i<3;i++){
ib=(i+1)%3;//确保不会超过index 2
b=(i+2)%3;//确定不会超过2
if((&a+1)>=(&a+ib)+*($a+ic)){
k=1;
break;
//没有用到 b ,c这里是用地址操作获取到b和 c,然后for循环 先a bc 然后 b c a 然后 c a b ???

}    

}
if(k){
printf(“ 三角形成立”);// 任意一边大于等于其他两边之和就是三角形。
}else{
printf(“warn”);
//
}