2

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?

user668074
  • 1,111
  • 10
  • 16

2 Answers2

9

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.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • [Here](https://stackoverflow.com/questions/41372118/whats-the-relationship-between-gcc-linking-and-ld-linking) are some more detailed informations about the difference ìn linking between `gcc` and `ld`. Maybe it is helpfull. – Kampi Jul 03 '19 at 06:15
2

You can access the C standard library functios in assembly too. Just follow the GCC calling convention and use the standard printf call. You have to compile your assembly program with as first to create an object file. After that you can use the linker to link all neccessarry libraries (stdio for example).

Kampi
  • 1,798
  • 18
  • 26
  • 1
    stdio functions are part of the standard C library. On GNU/Linux systems like RPi, the library is `libc.so`. The link option for `ld` is `-lc` – Peter Cordes Jul 03 '19 at 05:59