質問編集履歴

5

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

2017/11/22 01:47

投稿

takuan_no_hito
takuan_no_hito

スコア27

test CHANGED
File without changes
test CHANGED
@@ -671,3 +671,513 @@
671
671
  .subsections_via_symbols
672
672
 
673
673
  ```
674
+
675
+
676
+
677
+ コードの追加
678
+
679
+ ---
680
+
681
+ ちなみに、-4(%ebp)の部分を詰めて、例えばsample3.sを以下のように書き換えても、プログラムは動きます。ここでは%espを引く値も変えていますが、変えなくても当然動きます。
682
+
683
+
684
+
685
+ ```アセンブリ言語
686
+
687
+ #sample2.s
688
+
689
+ .section __TEXT,__text,regular,pure_instructions
690
+
691
+ .macosx_version_min 10, 12
692
+
693
+ .globl _main
694
+
695
+ .p2align 4, 0x90
696
+
697
+ _main: ## @main
698
+
699
+ ## BB#0:
700
+
701
+ pushl %ebp
702
+
703
+ movl %esp, %ebp
704
+
705
+ subl $12, %esp
706
+
707
+ movl $2, -4(%ebp)
708
+
709
+ movl $3, -8(%ebp)
710
+
711
+ movl $4, -12(%ebp)
712
+
713
+ movl -4(%ebp), %eax
714
+
715
+ addl -8(%ebp), %eax
716
+
717
+ addl -12(%ebp), %eax
718
+
719
+ addl $12, %esp
720
+
721
+ popl %ebp
722
+
723
+ retl
724
+
725
+
726
+
727
+
728
+
729
+ .subsections_via_symbols
730
+
731
+ ```
732
+
733
+
734
+
735
+ 新たにsample5~sample8を公開します。
736
+
737
+ ```C
738
+
739
+ //sample5.c
740
+
741
+ #include <stdio.h>
742
+
743
+
744
+
745
+
746
+
747
+
748
+
749
+ int main(void){
750
+
751
+ int a = 1;
752
+
753
+ int b = 2;
754
+
755
+ int c = 3;
756
+
757
+ int d = 4;
758
+
759
+ int e = 5;
760
+
761
+ return a+b+c+d+e;
762
+
763
+ }
764
+
765
+ ```
766
+
767
+
768
+
769
+ ```
770
+
771
+ #sample5.s
772
+
773
+ .section __TEXT,__text,regular,pure_instructions
774
+
775
+ .macosx_version_min 10, 12
776
+
777
+ .globl _main
778
+
779
+ .p2align 4, 0x90
780
+
781
+ _main: ## @main
782
+
783
+ ## BB#0:
784
+
785
+ pushl %ebp
786
+
787
+ movl %esp, %ebp
788
+
789
+ subl $24, %esp
790
+
791
+ movl $0, -4(%ebp)
792
+
793
+ movl $1, -8(%ebp)
794
+
795
+ movl $2, -12(%ebp)
796
+
797
+ movl $3, -16(%ebp)
798
+
799
+ movl $4, -20(%ebp)
800
+
801
+ movl $5, -24(%ebp)
802
+
803
+ movl -8(%ebp), %eax
804
+
805
+ addl -12(%ebp), %eax
806
+
807
+ addl -16(%ebp), %eax
808
+
809
+ addl -20(%ebp), %eax
810
+
811
+ addl -24(%ebp), %eax
812
+
813
+ addl $24, %esp
814
+
815
+ popl %ebp
816
+
817
+ retl
818
+
819
+
820
+
821
+
822
+
823
+ .subsections_via_symbols
824
+
825
+
826
+
827
+ ```
828
+
829
+
830
+
831
+ ```C
832
+
833
+ //sample6.c
834
+
835
+ #include <stdio.h>
836
+
837
+
838
+
839
+
840
+
841
+
842
+
843
+ int main(void){
844
+
845
+ int a = 1;
846
+
847
+ int b = 2;
848
+
849
+ int c = 3;
850
+
851
+ int d = 4;
852
+
853
+ int e = 5;
854
+
855
+ int f = 6;
856
+
857
+ return a+b+c+d+e+f;
858
+
859
+ }
860
+
861
+ ```
862
+
863
+
864
+
865
+ ```
866
+
867
+ #sample7.s
868
+
869
+ .section __TEXT,__text,regular,pure_instructions
870
+
871
+ .macosx_version_min 10, 12
872
+
873
+ .globl _main
874
+
875
+ .p2align 4, 0x90
876
+
877
+ _main: ## @main
878
+
879
+ ## BB#0:
880
+
881
+ pushl %ebp
882
+
883
+ movl %esp, %ebp
884
+
885
+ subl $28, %esp
886
+
887
+ movl $0, -4(%ebp)
888
+
889
+ movl $1, -8(%ebp)
890
+
891
+ movl $2, -12(%ebp)
892
+
893
+ movl $3, -16(%ebp)
894
+
895
+ movl $4, -20(%ebp)
896
+
897
+ movl $5, -24(%ebp)
898
+
899
+ movl $6, -28(%ebp)
900
+
901
+ movl -8(%ebp), %eax
902
+
903
+ addl -12(%ebp), %eax
904
+
905
+ addl -16(%ebp), %eax
906
+
907
+ addl -20(%ebp), %eax
908
+
909
+ addl -24(%ebp), %eax
910
+
911
+ addl -28(%ebp), %eax
912
+
913
+ addl $28, %esp
914
+
915
+ popl %ebp
916
+
917
+ retl
918
+
919
+
920
+
921
+ .subsections_via_symbols
922
+
923
+
924
+
925
+ ```
926
+
927
+
928
+
929
+ ```C
930
+
931
+ //sample7.c
932
+
933
+ #include <stdio.h>
934
+
935
+
936
+
937
+
938
+
939
+ int foo();
940
+
941
+
942
+
943
+ int main(void){
944
+
945
+ int a = 1;
946
+
947
+ int b = 2;
948
+
949
+ int c = 3;
950
+
951
+ int d = foo();
952
+
953
+ int e = 5;
954
+
955
+ int f = 6;
956
+
957
+ return a+b+c+d+e+f;
958
+
959
+ }
960
+
961
+
962
+
963
+
964
+
965
+ int foo(){
966
+
967
+ return 100;
968
+
969
+ }
970
+
971
+ ```
972
+
973
+ ```
974
+
975
+ #sample7.s
976
+
977
+ .section __TEXT,__text,regular,pure_instructions
978
+
979
+ .macosx_version_min 10, 12
980
+
981
+ .globl _main
982
+
983
+ .p2align 4, 0x90
984
+
985
+ _main: ## @main
986
+
987
+ ## BB#0:
988
+
989
+ pushl %ebp
990
+
991
+ movl %esp, %ebp
992
+
993
+ subl $40, %esp
994
+
995
+ movl $0, -4(%ebp)
996
+
997
+ movl $1, -8(%ebp)
998
+
999
+ movl $2, -12(%ebp)
1000
+
1001
+ movl $3, -16(%ebp)
1002
+
1003
+ calll _foo
1004
+
1005
+ movl %eax, -20(%ebp)
1006
+
1007
+ movl $5, -24(%ebp)
1008
+
1009
+ movl $6, -28(%ebp)
1010
+
1011
+ movl -8(%ebp), %eax
1012
+
1013
+ addl -12(%ebp), %eax
1014
+
1015
+ addl -16(%ebp), %eax
1016
+
1017
+ addl -20(%ebp), %eax
1018
+
1019
+ addl -24(%ebp), %eax
1020
+
1021
+ addl -28(%ebp), %eax
1022
+
1023
+ addl $40, %esp
1024
+
1025
+ popl %ebp
1026
+
1027
+ retl
1028
+
1029
+
1030
+
1031
+ .globl _foo
1032
+
1033
+ .p2align 4, 0x90
1034
+
1035
+ _foo: ## @foo
1036
+
1037
+ ## BB#0:
1038
+
1039
+ pushl %ebp
1040
+
1041
+ movl %esp, %ebp
1042
+
1043
+ movl $100, %eax
1044
+
1045
+ popl %ebp
1046
+
1047
+ retl
1048
+
1049
+
1050
+
1051
+
1052
+
1053
+ .subsections_via_symbols
1054
+
1055
+
1056
+
1057
+ ```
1058
+
1059
+
1060
+
1061
+ ```
1062
+
1063
+ //sample8.c
1064
+
1065
+ #include <stdio.h>
1066
+
1067
+
1068
+
1069
+ int foo();
1070
+
1071
+
1072
+
1073
+ int main(void){
1074
+
1075
+ int a = 1;
1076
+
1077
+ int b = 2;
1078
+
1079
+ int d = foo();
1080
+
1081
+ int e = 5;
1082
+
1083
+ int f = 6;
1084
+
1085
+ return a+b+d+e+f;
1086
+
1087
+ }
1088
+
1089
+
1090
+
1091
+
1092
+
1093
+ int foo(){
1094
+
1095
+ return 100;
1096
+
1097
+ }
1098
+
1099
+ ```
1100
+
1101
+
1102
+
1103
+ ```
1104
+
1105
+ #sample8.s
1106
+
1107
+ .section __TEXT,__text,regular,pure_instructions
1108
+
1109
+ .macosx_version_min 10, 12
1110
+
1111
+ .globl _main
1112
+
1113
+ .p2align 4, 0x90
1114
+
1115
+ _main: ## @main
1116
+
1117
+ ## BB#0:
1118
+
1119
+ pushl %ebp
1120
+
1121
+ movl %esp, %ebp
1122
+
1123
+ subl $24, %esp
1124
+
1125
+ movl $0, -4(%ebp)
1126
+
1127
+ movl $1, -8(%ebp)
1128
+
1129
+ movl $2, -12(%ebp)
1130
+
1131
+ calll _foo
1132
+
1133
+ movl %eax, -16(%ebp)
1134
+
1135
+ movl $5, -20(%ebp)
1136
+
1137
+ movl $6, -24(%ebp)
1138
+
1139
+ movl -8(%ebp), %eax
1140
+
1141
+ addl -12(%ebp), %eax
1142
+
1143
+ addl -16(%ebp), %eax
1144
+
1145
+ addl -20(%ebp), %eax
1146
+
1147
+ addl -24(%ebp), %eax
1148
+
1149
+ addl $24, %esp
1150
+
1151
+ popl %ebp
1152
+
1153
+ retl
1154
+
1155
+
1156
+
1157
+ .globl _foo
1158
+
1159
+ .p2align 4, 0x90
1160
+
1161
+ _foo: ## @foo
1162
+
1163
+ ## BB#0:
1164
+
1165
+ pushl %ebp
1166
+
1167
+ movl %esp, %ebp
1168
+
1169
+ movl $100, %eax
1170
+
1171
+ popl %ebp
1172
+
1173
+ retl
1174
+
1175
+
1176
+
1177
+
1178
+
1179
+ .subsections_via_symbols
1180
+
1181
+
1182
+
1183
+ ```

4

タイトルの変更

2017/11/22 01:47

投稿

takuan_no_hito
takuan_no_hito

スコア27

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

3

具体例を提示

2017/11/21 11:51

投稿

takuan_no_hito
takuan_no_hito

スコア27

test CHANGED
File without changes
test CHANGED
@@ -231,3 +231,443 @@
231
231
 
232
232
 
233
233
  Thanks
234
+
235
+
236
+
237
+ 補足
238
+
239
+ ---
240
+
241
+ 以下にC言語のsampleコード及びそれに対応するアセンブリコードを5つ記します。
242
+
243
+ コマンドは
244
+
245
+ gcc -m32 -S sample.c
246
+
247
+ です。
248
+
249
+ (gcc,i386,OS X)
250
+
251
+
252
+
253
+ ```C
254
+
255
+ //sample0.c
256
+
257
+ #include <stdio.h>
258
+
259
+
260
+
261
+
262
+
263
+ int main (void)
264
+
265
+ {
266
+
267
+
268
+
269
+ return 0;
270
+
271
+
272
+
273
+ }
274
+
275
+ ```
276
+
277
+
278
+
279
+ ```
280
+
281
+ #sample0.s
282
+
283
+ .section __TEXT,__text,regular,pure_instructions
284
+
285
+ .macosx_version_min 10, 12
286
+
287
+ .globl _main
288
+
289
+ .p2align 4, 0x90
290
+
291
+ _main: ## @main
292
+
293
+ ## BB#0:
294
+
295
+ pushl %ebp
296
+
297
+ movl %esp, %ebp
298
+
299
+ pushl %eax
300
+
301
+ xorl %eax, %eax
302
+
303
+ movl $0, -4(%ebp)
304
+
305
+ addl $4, %esp
306
+
307
+ popl %ebp
308
+
309
+ retl
310
+
311
+
312
+
313
+
314
+
315
+ .subsections_via_symbols
316
+
317
+ ```
318
+
319
+
320
+
321
+ ```C
322
+
323
+ //sample1.c
324
+
325
+ #include <stdio.h>
326
+
327
+
328
+
329
+
330
+
331
+ int main (void)
332
+
333
+ {
334
+
335
+ int a = 1;
336
+
337
+ int b = 2;
338
+
339
+ int c = 3;
340
+
341
+
342
+
343
+ return a + b + c;
344
+
345
+ }
346
+
347
+ ```
348
+
349
+ ```
350
+
351
+ #sample1.s
352
+
353
+ .section __TEXT,__text,regular,pure_instructions
354
+
355
+ .macosx_version_min 10, 12
356
+
357
+ .globl _main
358
+
359
+ .p2align 4, 0x90
360
+
361
+ _main: ## @main
362
+
363
+ ## BB#0:
364
+
365
+ pushl %ebp
366
+
367
+ movl %esp, %ebp
368
+
369
+ subl $16, %esp
370
+
371
+ movl $0, -4(%ebp)
372
+
373
+ movl $1, -8(%ebp)
374
+
375
+ movl $2, -12(%ebp)
376
+
377
+ movl $3, -16(%ebp)
378
+
379
+ movl -8(%ebp), %eax
380
+
381
+ addl -12(%ebp), %eax
382
+
383
+ addl -16(%ebp), %eax
384
+
385
+ addl $16, %esp
386
+
387
+ popl %ebp
388
+
389
+ retl
390
+
391
+
392
+
393
+
394
+
395
+ .subsections_via_symbols
396
+
397
+ ```
398
+
399
+
400
+
401
+ ```C
402
+
403
+ //sample2.c
404
+
405
+ #include <stdio.h>
406
+
407
+
408
+
409
+
410
+
411
+ int main ()
412
+
413
+ {
414
+
415
+ int a = 2;
416
+
417
+ int b = 3;
418
+
419
+ int c = 4;
420
+
421
+
422
+
423
+ return a + b + c;
424
+
425
+ }
426
+
427
+ ```
428
+
429
+ ```
430
+
431
+ #sample2.s
432
+
433
+ .section __TEXT,__text,regular,pure_instructions
434
+
435
+ .macosx_version_min 10, 12
436
+
437
+ .globl _main
438
+
439
+ .p2align 4, 0x90
440
+
441
+ _main: ## @main
442
+
443
+ ## BB#0:
444
+
445
+ pushl %ebp
446
+
447
+ movl %esp, %ebp
448
+
449
+ subl $16, %esp
450
+
451
+ movl $0, -4(%ebp)
452
+
453
+ movl $2, -8(%ebp)
454
+
455
+ movl $3, -12(%ebp)
456
+
457
+ movl $4, -16(%ebp)
458
+
459
+ movl -8(%ebp), %eax
460
+
461
+ addl -12(%ebp), %eax
462
+
463
+ addl -16(%ebp), %eax
464
+
465
+ addl $16, %esp
466
+
467
+ popl %ebp
468
+
469
+ retl
470
+
471
+
472
+
473
+
474
+
475
+ .subsections_via_symbols
476
+
477
+ ```
478
+
479
+
480
+
481
+
482
+
483
+ ```C
484
+
485
+ //sample3.c
486
+
487
+ #include <stdio.h>
488
+
489
+
490
+
491
+
492
+
493
+ int foo()
494
+
495
+ {
496
+
497
+ int a = 2;
498
+
499
+ int b = 3;
500
+
501
+ int c = 4;
502
+
503
+
504
+
505
+ return a + b + c;
506
+
507
+ }
508
+
509
+ ```
510
+
511
+ ```
512
+
513
+ #sample3.s
514
+
515
+ .section __TEXT,__text,regular,pure_instructions
516
+
517
+ .macosx_version_min 10, 12
518
+
519
+ .globl _foo
520
+
521
+ .p2align 4, 0x90
522
+
523
+ _foo: ## @foo
524
+
525
+ ## BB#0:
526
+
527
+ pushl %ebp
528
+
529
+ movl %esp, %ebp
530
+
531
+ subl $12, %esp
532
+
533
+ movl $2, -4(%ebp)
534
+
535
+ movl $3, -8(%ebp)
536
+
537
+ movl $4, -12(%ebp)
538
+
539
+ movl -4(%ebp), %eax
540
+
541
+ addl -8(%ebp), %eax
542
+
543
+ addl -12(%ebp), %eax
544
+
545
+ addl $12, %esp
546
+
547
+ popl %ebp
548
+
549
+ retl
550
+
551
+
552
+
553
+
554
+
555
+ .subsections_via_symbols
556
+
557
+ ```
558
+
559
+
560
+
561
+
562
+
563
+ ```C
564
+
565
+ //sample4.c
566
+
567
+ #include <stdio.h>
568
+
569
+
570
+
571
+ int foo();
572
+
573
+
574
+
575
+ int main(void){
576
+
577
+ return foo();
578
+
579
+ }
580
+
581
+
582
+
583
+ int foo()
584
+
585
+ {
586
+
587
+ int a = 3;
588
+
589
+ int b = 4;
590
+
591
+ int c = 5;
592
+
593
+
594
+
595
+ return a + b + c;
596
+
597
+ }
598
+
599
+ ```
600
+
601
+ ```
602
+
603
+ #sample4.s
604
+
605
+ .section __TEXT,__text,regular,pure_instructions
606
+
607
+ .macosx_version_min 10, 12
608
+
609
+ .globl _main
610
+
611
+ .p2align 4, 0x90
612
+
613
+ _main: ## @main
614
+
615
+ ## BB#0:
616
+
617
+ pushl %ebp
618
+
619
+ movl %esp, %ebp
620
+
621
+ subl $8, %esp
622
+
623
+ movl $0, -4(%ebp)
624
+
625
+ calll _foo
626
+
627
+ addl $8, %esp
628
+
629
+ popl %ebp
630
+
631
+ retl
632
+
633
+
634
+
635
+ .globl _foo
636
+
637
+ .p2align 4, 0x90
638
+
639
+ _foo: ## @foo
640
+
641
+ ## BB#0:
642
+
643
+ pushl %ebp
644
+
645
+ movl %esp, %ebp
646
+
647
+ subl $12, %esp
648
+
649
+ movl $3, -4(%ebp)
650
+
651
+ movl $4, -8(%ebp)
652
+
653
+ movl $5, -12(%ebp)
654
+
655
+ movl -4(%ebp), %eax
656
+
657
+ addl -8(%ebp), %eax
658
+
659
+ addl -12(%ebp), %eax
660
+
661
+ addl $12, %esp
662
+
663
+ popl %ebp
664
+
665
+ retl
666
+
667
+
668
+
669
+
670
+
671
+ .subsections_via_symbols
672
+
673
+ ```

2

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

2017/11/21 03:33

投稿

takuan_no_hito
takuan_no_hito

スコア27

test CHANGED
File without changes
test CHANGED
@@ -1,5 +1,9 @@
1
1
  アセンブリ初学者です。
2
2
 
3
+ アセンブリをより深く理解したく質問したしだいです。
4
+
5
+
6
+
3
7
  gcc -S で吐かれたアセンブリコードを見て、以下のような記述がありました
4
8
 
5
9
 
@@ -226,4 +230,4 @@
226
230
 
227
231
 
228
232
 
229
- Thnaks
233
+ Thanks

1

タグを追加

2017/11/20 07:50

投稿

takuan_no_hito
takuan_no_hito

スコア27

test CHANGED
File without changes
test CHANGED
File without changes