CSC448: Code Generation: Learning via GCC I [61/133] 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:

#include <stdio.h>
#include <stdlib.h>

int main (int argc, char *argv[])
{
  int x;

  x = 3;
  goto l2;
 l1:
  printf ("%d\n", x);
  x = x - 1;
 l2:
  if (0 <= x) goto l1;

  return 0;
}
        

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