15

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.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
rogcg
  • 10,451
  • 20
  • 91
  • 133

5 Answers5

25

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.

evandrix
  • 6,041
  • 4
  • 27
  • 38
wkl
  • 77,184
  • 16
  • 165
  • 176
  • I've installed, and now to compile a .asm file? Do I have to paste the file in the same folder of the compiler?? – – rogcg Nov 23 '10 at 02:10
  • @phyhclo: If you installed with `apt-get` or similar tools, it should be already in your system PATH. So simply try `as` or `gas` on the terminal and see what it says. If `as` was not installed and you typed out `as` on the terminal, Ubuntu systems normally show the package in which the command is bundled. So you can simply go ahead and `sudo apt-get install ` on the terminal. – vpit3833 Nov 23 '10 at 02:24
5

It's in the binutils package.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • yeah, I downloaded a folder with a lot of folders inside. one of them is binutils. But What file in binutils folder I access, and how? thanks. – rogcg Nov 23 '10 at 01:56
4

Fire up Synaptic and enter "gnu assembler" into the quick search bar. It's immediately obvious that binutils is the required package.

And you may well find it's already installed. My binutils 2.20.1-3ubuntu7 is already installed and I have a fairly vanilla set-up.

Entering as --version from a terminal window will let you know:

GNU assembler (GNU Binutils for Ubuntu) 2.20.1-system.20100303
Copyright 2009 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License version 3 or later.
This program has absolutely no warranty.
This assembler was configured for a target of `i486-linux-gnu'.
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

Have you read http://www.faqs.org/docs/Linux-HOWTO/Assembly-HOWTO.html? on Debian GAS is contained in the package

binutils

so

sudo apt-get install binutils
dpkg -L binutils

$man as

BozoJoe
  • 6,117
  • 4
  • 44
  • 66
  • I've installed, and now to compile a .asm file? Do I have to paste the file in the same folder of the compiler?? – rogcg Nov 23 '10 at 02:07
  • @psyhclo sounds like you need a tutorial? http://www.hep.wisc.edu/~pinghc/x86AssmTutorial.htm – BozoJoe Nov 23 '10 at 02:42
0

Build from source and use it

#!/usr/bin/env bash
set -eux

# Build.
sudo apt-get build-dep binutils
git clone git://sourceware.org/git/binutils-gdb.git
cd binutils-gdb
git checkout binutils-2_31
./configure --target x86_64-elf --prefix "$(pwd)/install"
make -j `nproc`
make install

# Test it out.
cat <<'EOF' > hello.S
.data
    s:
        .ascii "hello world\n"
        len = . - s
.text
    .global _start
    _start:
        mov $4, %eax
        mov $1, %ebx
        mov $s, %ecx
        mov $len, %edx
        int $0x80
        mov $1, %eax
        mov $0, %ebx
        int $0x80
EOF
./install/bin/x86_64-elf-as -o hello.o hello.S
./install/bin/x86_64-elf-ld -o hello hello.o
./hello

GitHub upstream.

TODO: how to configure as specific options? We have used the ./configure from the binutils-gdb top-level, but that contains options from multiple projects such as gdb I believe, and not as specific ones?

Tested on Ubuntu 18.04.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985