teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

5

コードの追加(最後に追加)

2017/11/22 01:47

投稿

takuan_no_hito
takuan_no_hito

スコア27

title CHANGED
File without changes
body CHANGED
@@ -334,4 +334,259 @@
334
334
 
335
335
 
336
336
  .subsections_via_symbols
337
+ ```
338
+
339
+ コードの追加
340
+ ---
341
+ ちなみに、-4(%ebp)の部分を詰めて、例えばsample3.sを以下のように書き換えても、プログラムは動きます。ここでは%espを引く値も変えていますが、変えなくても当然動きます。
342
+
343
+ ```アセンブリ言語
344
+ #sample2.s
345
+ .section __TEXT,__text,regular,pure_instructions
346
+ .macosx_version_min 10, 12
347
+ .globl _main
348
+ .p2align 4, 0x90
349
+ _main: ## @main
350
+ ## BB#0:
351
+ pushl %ebp
352
+ movl %esp, %ebp
353
+ subl $12, %esp
354
+ movl $2, -4(%ebp)
355
+ movl $3, -8(%ebp)
356
+ movl $4, -12(%ebp)
357
+ movl -4(%ebp), %eax
358
+ addl -8(%ebp), %eax
359
+ addl -12(%ebp), %eax
360
+ addl $12, %esp
361
+ popl %ebp
362
+ retl
363
+
364
+
365
+ .subsections_via_symbols
366
+ ```
367
+
368
+ 新たにsample5~sample8を公開します。
369
+ ```C
370
+ //sample5.c
371
+ #include <stdio.h>
372
+
373
+
374
+
375
+ int main(void){
376
+ int a = 1;
377
+ int b = 2;
378
+ int c = 3;
379
+ int d = 4;
380
+ int e = 5;
381
+ return a+b+c+d+e;
382
+ }
383
+ ```
384
+
385
+ ```
386
+ #sample5.s
387
+ .section __TEXT,__text,regular,pure_instructions
388
+ .macosx_version_min 10, 12
389
+ .globl _main
390
+ .p2align 4, 0x90
391
+ _main: ## @main
392
+ ## BB#0:
393
+ pushl %ebp
394
+ movl %esp, %ebp
395
+ subl $24, %esp
396
+ movl $0, -4(%ebp)
397
+ movl $1, -8(%ebp)
398
+ movl $2, -12(%ebp)
399
+ movl $3, -16(%ebp)
400
+ movl $4, -20(%ebp)
401
+ movl $5, -24(%ebp)
402
+ movl -8(%ebp), %eax
403
+ addl -12(%ebp), %eax
404
+ addl -16(%ebp), %eax
405
+ addl -20(%ebp), %eax
406
+ addl -24(%ebp), %eax
407
+ addl $24, %esp
408
+ popl %ebp
409
+ retl
410
+
411
+
412
+ .subsections_via_symbols
413
+
414
+ ```
415
+
416
+ ```C
417
+ //sample6.c
418
+ #include <stdio.h>
419
+
420
+
421
+
422
+ int main(void){
423
+ int a = 1;
424
+ int b = 2;
425
+ int c = 3;
426
+ int d = 4;
427
+ int e = 5;
428
+ int f = 6;
429
+ return a+b+c+d+e+f;
430
+ }
431
+ ```
432
+
433
+ ```
434
+ #sample7.s
435
+ .section __TEXT,__text,regular,pure_instructions
436
+ .macosx_version_min 10, 12
437
+ .globl _main
438
+ .p2align 4, 0x90
439
+ _main: ## @main
440
+ ## BB#0:
441
+ pushl %ebp
442
+ movl %esp, %ebp
443
+ subl $28, %esp
444
+ movl $0, -4(%ebp)
445
+ movl $1, -8(%ebp)
446
+ movl $2, -12(%ebp)
447
+ movl $3, -16(%ebp)
448
+ movl $4, -20(%ebp)
449
+ movl $5, -24(%ebp)
450
+ movl $6, -28(%ebp)
451
+ movl -8(%ebp), %eax
452
+ addl -12(%ebp), %eax
453
+ addl -16(%ebp), %eax
454
+ addl -20(%ebp), %eax
455
+ addl -24(%ebp), %eax
456
+ addl -28(%ebp), %eax
457
+ addl $28, %esp
458
+ popl %ebp
459
+ retl
460
+
461
+ .subsections_via_symbols
462
+
463
+ ```
464
+
465
+ ```C
466
+ //sample7.c
467
+ #include <stdio.h>
468
+
469
+
470
+ int foo();
471
+
472
+ int main(void){
473
+ int a = 1;
474
+ int b = 2;
475
+ int c = 3;
476
+ int d = foo();
477
+ int e = 5;
478
+ int f = 6;
479
+ return a+b+c+d+e+f;
480
+ }
481
+
482
+
483
+ int foo(){
484
+ return 100;
485
+ }
486
+ ```
487
+ ```
488
+ #sample7.s
489
+ .section __TEXT,__text,regular,pure_instructions
490
+ .macosx_version_min 10, 12
491
+ .globl _main
492
+ .p2align 4, 0x90
493
+ _main: ## @main
494
+ ## BB#0:
495
+ pushl %ebp
496
+ movl %esp, %ebp
497
+ subl $40, %esp
498
+ movl $0, -4(%ebp)
499
+ movl $1, -8(%ebp)
500
+ movl $2, -12(%ebp)
501
+ movl $3, -16(%ebp)
502
+ calll _foo
503
+ movl %eax, -20(%ebp)
504
+ movl $5, -24(%ebp)
505
+ movl $6, -28(%ebp)
506
+ movl -8(%ebp), %eax
507
+ addl -12(%ebp), %eax
508
+ addl -16(%ebp), %eax
509
+ addl -20(%ebp), %eax
510
+ addl -24(%ebp), %eax
511
+ addl -28(%ebp), %eax
512
+ addl $40, %esp
513
+ popl %ebp
514
+ retl
515
+
516
+ .globl _foo
517
+ .p2align 4, 0x90
518
+ _foo: ## @foo
519
+ ## BB#0:
520
+ pushl %ebp
521
+ movl %esp, %ebp
522
+ movl $100, %eax
523
+ popl %ebp
524
+ retl
525
+
526
+
527
+ .subsections_via_symbols
528
+
529
+ ```
530
+
531
+ ```
532
+ //sample8.c
533
+ #include <stdio.h>
534
+
535
+ int foo();
536
+
537
+ int main(void){
538
+ int a = 1;
539
+ int b = 2;
540
+ int d = foo();
541
+ int e = 5;
542
+ int f = 6;
543
+ return a+b+d+e+f;
544
+ }
545
+
546
+
547
+ int foo(){
548
+ return 100;
549
+ }
550
+ ```
551
+
552
+ ```
553
+ #sample8.s
554
+ .section __TEXT,__text,regular,pure_instructions
555
+ .macosx_version_min 10, 12
556
+ .globl _main
557
+ .p2align 4, 0x90
558
+ _main: ## @main
559
+ ## BB#0:
560
+ pushl %ebp
561
+ movl %esp, %ebp
562
+ subl $24, %esp
563
+ movl $0, -4(%ebp)
564
+ movl $1, -8(%ebp)
565
+ movl $2, -12(%ebp)
566
+ calll _foo
567
+ movl %eax, -16(%ebp)
568
+ movl $5, -20(%ebp)
569
+ movl $6, -24(%ebp)
570
+ movl -8(%ebp), %eax
571
+ addl -12(%ebp), %eax
572
+ addl -16(%ebp), %eax
573
+ addl -20(%ebp), %eax
574
+ addl -24(%ebp), %eax
575
+ addl $24, %esp
576
+ popl %ebp
577
+ retl
578
+
579
+ .globl _foo
580
+ .p2align 4, 0x90
581
+ _foo: ## @foo
582
+ ## BB#0:
583
+ pushl %ebp
584
+ movl %esp, %ebp
585
+ movl $100, %eax
586
+ popl %ebp
587
+ retl
588
+
589
+
590
+ .subsections_via_symbols
591
+
337
592
  ```

4

タイトルの変更

2017/11/22 01:47

投稿

takuan_no_hito
takuan_no_hito

スコア27

title CHANGED
@@ -1,1 +1,1 @@
1
- アセンブリ言語の-4(%ebp)とは?
1
+ アセンブリ言語の"movl $0, -4(%ebp)"とは?
body CHANGED
File without changes

3

具体例を提示

2017/11/21 11:51

投稿

takuan_no_hito
takuan_no_hito

スコア27

title CHANGED
File without changes
body CHANGED
@@ -114,4 +114,224 @@
114
114
 
115
115
 
116
116
 
117
- Thanks
117
+ Thanks
118
+
119
+ 補足
120
+ ---
121
+ 以下にC言語のsampleコード及びそれに対応するアセンブリコードを5つ記します。
122
+ コマンドは
123
+ gcc -m32 -S sample.c
124
+ です。
125
+ (gcc,i386,OS X)
126
+
127
+ ```C
128
+ //sample0.c
129
+ #include <stdio.h>
130
+
131
+
132
+ int main (void)
133
+ {
134
+
135
+ return 0;
136
+
137
+ }
138
+ ```
139
+
140
+ ```
141
+ #sample0.s
142
+ .section __TEXT,__text,regular,pure_instructions
143
+ .macosx_version_min 10, 12
144
+ .globl _main
145
+ .p2align 4, 0x90
146
+ _main: ## @main
147
+ ## BB#0:
148
+ pushl %ebp
149
+ movl %esp, %ebp
150
+ pushl %eax
151
+ xorl %eax, %eax
152
+ movl $0, -4(%ebp)
153
+ addl $4, %esp
154
+ popl %ebp
155
+ retl
156
+
157
+
158
+ .subsections_via_symbols
159
+ ```
160
+
161
+ ```C
162
+ //sample1.c
163
+ #include <stdio.h>
164
+
165
+
166
+ int main (void)
167
+ {
168
+ int a = 1;
169
+ int b = 2;
170
+ int c = 3;
171
+
172
+ return a + b + c;
173
+ }
174
+ ```
175
+ ```
176
+ #sample1.s
177
+ .section __TEXT,__text,regular,pure_instructions
178
+ .macosx_version_min 10, 12
179
+ .globl _main
180
+ .p2align 4, 0x90
181
+ _main: ## @main
182
+ ## BB#0:
183
+ pushl %ebp
184
+ movl %esp, %ebp
185
+ subl $16, %esp
186
+ movl $0, -4(%ebp)
187
+ movl $1, -8(%ebp)
188
+ movl $2, -12(%ebp)
189
+ movl $3, -16(%ebp)
190
+ movl -8(%ebp), %eax
191
+ addl -12(%ebp), %eax
192
+ addl -16(%ebp), %eax
193
+ addl $16, %esp
194
+ popl %ebp
195
+ retl
196
+
197
+
198
+ .subsections_via_symbols
199
+ ```
200
+
201
+ ```C
202
+ //sample2.c
203
+ #include <stdio.h>
204
+
205
+
206
+ int main ()
207
+ {
208
+ int a = 2;
209
+ int b = 3;
210
+ int c = 4;
211
+
212
+ return a + b + c;
213
+ }
214
+ ```
215
+ ```
216
+ #sample2.s
217
+ .section __TEXT,__text,regular,pure_instructions
218
+ .macosx_version_min 10, 12
219
+ .globl _main
220
+ .p2align 4, 0x90
221
+ _main: ## @main
222
+ ## BB#0:
223
+ pushl %ebp
224
+ movl %esp, %ebp
225
+ subl $16, %esp
226
+ movl $0, -4(%ebp)
227
+ movl $2, -8(%ebp)
228
+ movl $3, -12(%ebp)
229
+ movl $4, -16(%ebp)
230
+ movl -8(%ebp), %eax
231
+ addl -12(%ebp), %eax
232
+ addl -16(%ebp), %eax
233
+ addl $16, %esp
234
+ popl %ebp
235
+ retl
236
+
237
+
238
+ .subsections_via_symbols
239
+ ```
240
+
241
+
242
+ ```C
243
+ //sample3.c
244
+ #include <stdio.h>
245
+
246
+
247
+ int foo()
248
+ {
249
+ int a = 2;
250
+ int b = 3;
251
+ int c = 4;
252
+
253
+ return a + b + c;
254
+ }
255
+ ```
256
+ ```
257
+ #sample3.s
258
+ .section __TEXT,__text,regular,pure_instructions
259
+ .macosx_version_min 10, 12
260
+ .globl _foo
261
+ .p2align 4, 0x90
262
+ _foo: ## @foo
263
+ ## BB#0:
264
+ pushl %ebp
265
+ movl %esp, %ebp
266
+ subl $12, %esp
267
+ movl $2, -4(%ebp)
268
+ movl $3, -8(%ebp)
269
+ movl $4, -12(%ebp)
270
+ movl -4(%ebp), %eax
271
+ addl -8(%ebp), %eax
272
+ addl -12(%ebp), %eax
273
+ addl $12, %esp
274
+ popl %ebp
275
+ retl
276
+
277
+
278
+ .subsections_via_symbols
279
+ ```
280
+
281
+
282
+ ```C
283
+ //sample4.c
284
+ #include <stdio.h>
285
+
286
+ int foo();
287
+
288
+ int main(void){
289
+ return foo();
290
+ }
291
+
292
+ int foo()
293
+ {
294
+ int a = 3;
295
+ int b = 4;
296
+ int c = 5;
297
+
298
+ return a + b + c;
299
+ }
300
+ ```
301
+ ```
302
+ #sample4.s
303
+ .section __TEXT,__text,regular,pure_instructions
304
+ .macosx_version_min 10, 12
305
+ .globl _main
306
+ .p2align 4, 0x90
307
+ _main: ## @main
308
+ ## BB#0:
309
+ pushl %ebp
310
+ movl %esp, %ebp
311
+ subl $8, %esp
312
+ movl $0, -4(%ebp)
313
+ calll _foo
314
+ addl $8, %esp
315
+ popl %ebp
316
+ retl
317
+
318
+ .globl _foo
319
+ .p2align 4, 0x90
320
+ _foo: ## @foo
321
+ ## BB#0:
322
+ pushl %ebp
323
+ movl %esp, %ebp
324
+ subl $12, %esp
325
+ movl $3, -4(%ebp)
326
+ movl $4, -8(%ebp)
327
+ movl $5, -12(%ebp)
328
+ movl -4(%ebp), %eax
329
+ addl -8(%ebp), %eax
330
+ addl -12(%ebp), %eax
331
+ addl $12, %esp
332
+ popl %ebp
333
+ retl
334
+
335
+
336
+ .subsections_via_symbols
337
+ ```

2

誤字の修正、タグの追加、文章の軽微な修正

2017/11/21 03:33

投稿

takuan_no_hito
takuan_no_hito

スコア27

title CHANGED
File without changes
body CHANGED
@@ -1,4 +1,6 @@
1
1
  アセンブリ初学者です。
2
+ アセンブリをより深く理解したく質問したしだいです。
3
+
2
4
  gcc -S で吐かれたアセンブリコードを見て、以下のような記述がありました
3
5
 
4
6
  ```アセンブリ言語
@@ -112,4 +114,4 @@
112
114
 
113
115
 
114
116
 
115
- Thnaks
117
+ Thanks

1

タグを追加

2017/11/20 07:50

投稿

takuan_no_hito
takuan_no_hito

スコア27

title CHANGED
File without changes
body CHANGED
File without changes