CSC448: Code Generation: Learning via GCC I [8/14] Previous pageContentsNext page

A practical way to learn x86 assembly language and to understand the operation of a compiler is to look at the code generated by a C compiler.

Also beneficial when debugging C/C++ programs or identifying performance bottlenecks.

Consider loop.c:

file:loop.c [source]
00001: #include <stdio.h>
00002: #include <stdlib.h>
00003: 
00004: int main (int argc, char *argv[])
00005: {
00006:   int x;
00007:   
00008:   x = 3;
00009:   goto l2;
00010:  l1:
00011:   printf ("%d\n", x);
00012:   x = x - 1;
00013:  l2:
00014:   if (0 <= x) goto l1;
00015:   
00016:   return 0;
00017: }
00018: 

Compile, link, and run (use a ".exe" extension for loop on Windows):

$ gcc -o loop loop.c
$ ls -l loop*
-rwxrwxr-x    1 cpitcher cpitcher    13663 Feb 25 00:56 loop
-rw-rw-r--    1 cpitcher cpitcher      191 Feb 25 00:48 loop.c
$ ./loop 
3
2
1
0
$ rm loop 

Previous pageContentsNext page