我们都知道gcc的-S开关可以用来生成汇编代码,
但有时候,单有汇编文件是不够的,我们希望的是将C语言程序的源代码和汇编语言文本交错在一起查看,
这是LISTING功能,在gcc中并没有专门的FAQ说明,
区区在网上查了很多资料才知道怎么实现,所以特此记下。
gcc -c -g -Wa,-adlhn ee.c > ee.anno.s
由此生成的ee.anno.s即是ee.c对应的C与汇编混排的列表
又如C程序
/* EE */
#include<stdio.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<windows.h>
int main(int argc, char **argv)
{
char * p = "TO STDOUT";
int ic = (int)GetStdHandle(STD_OUTPUT_HANDLE);
printf("%d/n", ic);
WriteFile(ic, p, strlen(p), &ic, NULL);
return 0;
}
1 .file "ee.c"4 .text5 Ltext0:4222 .section .rdata,"dr"4223 LC0:4224 0000 544F2053 .ascii "TO STDOUT/0"4224 54444F554224 54004225 LC1:4226 000a 25640A00 .ascii "%d/12/0"4227 000e 0000 .text4231 .globl _main4233 _main:1:ee.c **** /* EE */2:ee.c **** #include<stdio.h>3:ee.c **** #include<stdio.h>4:ee.c **** #include<string.h>5:ee.c **** #include<stdlib.h>6:ee.c **** #include<windows.h>7:ee.c **** int main(int argc, char **argv)8:ee.c **** {4235 LM1:4236 0000 55 pushl %ebp4237 0001 89E5 movl %esp, %ebp4238 0003 83EC28 subl $40, %esp4239 0006 83E4F0 andl $-16, %esp4240 0009 B8000000 movl $0, %eax4240 004241 000e 83C00F addl $15, %eax4242 0011 83C00F addl $15, %eax4243 0014 C1E804 shrl $4, %eax4244 0017 C1E004 sall $4, %eax4245 001a 8945F4 movl %eax, -12(%ebp)4246 001d 8B45F4 movl -12(%ebp), %eax4247 0020 E8000000 call __alloca4247 004249 LM2:4250 0025 E8000000 call ___main4250 009:ee.c **** char * p = "TO STDOUT";4252 LM3:4253 002a C745FC00 movl $LC0, -4(%ebp)4253 00000010:ee.c **** int ic = (int)GetStdHandle(STD_OUTPUT_HANDLE);4255 LM4:4256 0031 C70424F5 movl $-11, (%esp)4256 FFFFFF4257 0038 E8000000 call _GetStdHandle@44257 004258 003d 83EC04 subl $4, %esp4259 0040 8945F8 movl %eax, -8(%ebp)11:ee.c **** printf("%d/n", ic);4261 LM5:4262 0043 8B45F8 movl -8(%ebp), %eax4263 0046 89442404 movl %eax, 4(%esp)4264 004a C704240A movl $LC1, (%esp)4264 0000004265 0051 E8000000 call _printf4265 0012:ee.c **** WriteFile(ic, p, strlen(p), &ic, NULL);4267 LM6:4268 0056 8B45FC movl -4(%ebp), %eax4269 0059 890424 movl %eax, (%esp)4270 005c E8000000 call _strlen4270 004271 0061 89C2 movl %eax, %edx4272 0063 C7442410 movl $0, 16(%esp)4272 000000004273 006b 8D45F8 leal -8(%ebp), %eax4274 006e 8944240C movl %eax, 12(%esp)4275 0072 89542408 movl %edx, 8(%esp)4276 0076 8B45FC movl -4(%ebp), %eax4277 0079 89442404 movl %eax, 4(%esp)4278 007d 8B45F8 movl -8(%ebp), %eax4279 0080 890424 movl %eax, (%esp)4280 0083 E8000000 call _WriteFile@204280 004281 0088 83EC14 subl $20, %esp13:ee.c ****14:ee.c **** return 0;4283 LM7:4284 008b B8000000 movl $0, %eax4284 0015:ee.c **** }4286 LM8:4287 0090 C9 leave4288 0091 C3 ret4293 Lscope0:4295 .text4297 Letext: