如何在Linux上安装和使用GAS(GNU编译器)?

发布于 2024-10-03 12:39:22 字数 73 浏览 7 评论 0原文

我正在使用 Ubuntu,我正在寻找 Linux 的汇编编译器,我找到了 GAS。

我正在尝试安装并运行它,但不能。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

北方的巷 2024-10-10 12:39:22

asGNU 汇编器。它可以在 binutils 中找到,但如果您执行以下操作:

sudo apt-get install build-essential

您将获得 gas 以及 gcc (默认使用 gas 在后端进行组装)。

有关使用 gas 的“教程”,您可能需要阅读编程From the Ground Up,它使用它。


要从 .s 文件构建静态可执行文件,

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

如果您想链接库,通常最简单的方法是让 gcc 使用 as的正确命令行选项>ld 从 asm 源文件构建可执行文件时。
如果您的 foo.s 定义了 main 函数,则 gcc foo.s -o foo 将起作用。

另相关:在 a 上组装 32 位二进制文​​件64 位系统(GNU 工具链)(如果您在 x86-64 系统上编写 32 位程序)。

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.

榆西 2024-10-10 12:39:22

它位于 binutils 包中。

It's in the binutils package.

淡忘如思 2024-10-10 12:39:22

启动 Synaptic 并在快速搜索栏中输入“gnu assembler”。很明显,binutils 是所需的包。

您可能会发现它已经安装了。我的 binutils 2.20.1-3ubuntu7 已经安装,并且我有一个相当普通的设置。

从终端窗口输入 as --version 会让您知道:

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'.

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'.
沐歌 2024-10-10 12:39:22

你读过 http://www.faqs.org/docs/Linux- HOWTO/Assembly-HOWTO.html? Debian GAS 上包含在软件包中

binutils

所以

sudo apt-get install binutils
dpkg -L binutils

$man as

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

内心荒芜 2024-10-10 12:39:22

从源代码构建并使用它

#!/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 上游< /a>.

TODO:如何配置特定选项?我们使用了 binutils-gdb 顶层的 ./configure,但它包含来自多个项目的选项,例如 gdb 我相信,而不是作为特定的?

在 Ubuntu 18.04 上测试。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文