How to install and use GAS (GNU Compiler) on Linux?

rogcg picture rogcg · Nov 23, 2010 · Viewed 24.7k times · Source

I'm using Ubuntu, and I was looking for an assembler compiler for Linux, and I found GAS.

I'm trying to install it and run it, but I can't.

Answer

wkl picture wkl · Nov 23, 2010

as is the GNU Assembler. It's found in binutils but if you do:

sudo apt-get install build-essential

You will get gas along with gcc (which default uses gas for assembling on the back end).

For a 'tutorial' about using gas, you probably want to read Programming From the Ground Up, which uses it.


To build a static executable from a .s file,

#!/bin/bash
f="${1:-}"
as "${f}" -o "${f%%.s}.o" && ld "${f%%.s}.0" -o "${f%%.s}"
gcc -nostdlib -static "${f}" -o "${f%%.s}"

If you want to link with libraries, it's normally easiest to let gcc use the right command line options for as and ld when building an executable from an asm source file.
gcc foo.s -o foo will work if your foo.s defines a main function.

Also related: Assembling 32-bit binaries on a 64-bit system (GNU toolchain) if you're writing 32-bit programs on an x86-64 system.