Search code examples
gccassemblyraspberry-pignu-assembler

What are the differences between compiling assembly using gcc and as


I am learning how to use assembly language (on Raspberry Pi incidentally) and I am wondering what the difference is between using gcc and as to do the compiling.

So far, the differences I have noticed are:

  • I should do the extra linking step with as.
  • On the Raspberry Pi, as seems to recognize the architecture better than gcc by itself. I have to tell gcc the architecture before I can use instructions like integer division.
  • With gcc I have easy access to the C standard library functions. I assume this is possible using as but I haven't figured it out yet.

I would like to stick to a particular compiler. What other differences should I be aware of. Is there an advantage / disadvantage to using either?


Solution

  • gcc is just a front-end that runs as (and ld unless you use -c to stop at object files without linking). Use gcc -v to see what it runs and what command line options it passes.

    If you want to link with libraries, generally use gcc. It knows the right command-line options to pass to ld to set library paths, and which order to put things in on the ld command line.

    You might find gcc -nostdlib or -nostartfiles useful, e.g. if you want to write your own _start but still link libraries. Also -no-pie and/or -static depending on how you want to link.


    If you're curious to learn more about the toolchain and linking, then sure play around with ld options and see what breaks when you change the options. And/or use readelf -a to examine the resulting executable.