How to install and use GAS (GNU Compiler) on Linux?
I'm using Ubun开发者_如何学JAVAtu, 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.
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.
It's in the binutils
package.
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'.
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
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.
精彩评论