as

programminglinux
The as command is one of the most frequently used commands in Linux/Unix-like operating systems. as The as command is the GNU assembler. It is used to convert assembly language code into object files that can be linked to create executable programs. It is a key component in the compilation toolchain.

Quick Reference

Command Name:

as

Category:

programming

Platform:

linux

Basic Usage:

as [options] [arguments]

Common Use Cases

    Syntax

    as [options] -o objfile file.s

    Options

    Option Description
    -a[cdghlmns] Turn on listings (combinations of: c=omit false conditionals, d=debug, g=general info, h=include high-level source, l=assembly, m=macros, n=forms processing, s=symbols)
    --alternate Use alternative macro syntax
    -D Produce assembler debugging information
    --debug-prefix-map=old=new Map debug information paths
    --defsym SYM=VAL Define symbol SYM to value VAL
    -f Skip whitespace and comment preprocessing
    -g/--gen-debug Generate debugging information
    --gstabs Generate STABS debugging information
    --gdwarf-2 Generate DWARF2 debugging information
    --help Show help message and exit
    -I PATH Add PATH to include file search path
    -J Don't warn about signed overflow
    -K Warn when differences altered for long displacements
    -L/--keep-locals Keep local symbols (e.g., starting with 'L')
    --listing-lhs-width=NUM Set width in columns of listing hex output
    --listing-cont-lines=NUM Set maximum number of continuation lines
    --listing-lhs-width-alternate=NUM Set alternate width in columns for listing hex output
    -march=CPU Set architecture (e.g., i686, armv7-a)
    --32/--64 Generate 32-bit/64-bit code
    -n Do not optimize code alignment
    -o OBJFILE Name the output file
    -R Fold data section into text section
    --statistics Print statistics about the assembly
    --strip-local-absolute Strip local absolute symbols
    --version Print assembler version
    -W/--no-warn Suppress/enable warnings
    --warn Enable warnings
    --fatal-warnings Treat warnings as errors
    --target-help Show target-specific options
    --size-check=[error|warning] Issue error or warning for invalid size modifiers

    Examples

    How to Use These Examples

    The examples below show common ways to use the as command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    #

    Basic Examples:

    # Assemble a source file into an object file
    as -o hello.o hello.s
    # Assemble with debugging information as -g -o hello.o hello.s
    # Specify the target architecture as --32 -o program.o program.s
    # Enable all warnings as -Wall -o output.o input.s
    # Assemble and list the source with hex machine code as -ahls=listing.txt -o program.o program.s

    Advanced Examples:

    # Assemble for a specific architecture
    as -march=armv7-a -o arm_code.o arm_code.s
    # Include search paths for include files as -I/path/to/includes -o program.o program.s # Generate position-independent code as --pic -o library.o library.s # Assemble with relaxation for optimizing instruction sizes as --relax -o optimized.o source.s # Use alternative macro syntax as -alternate -o program.o program.s # Assemble with target-specific options as -mfpu=neon -o arm_vector.o arm_vector.s # Include debug information in DWARF format as --gdwarf-2 -o debug.o source.s # Control which warnings are shown as -W -Wno-deprecated -o output.o input.s # Assemble with statistics output as --statistics -o program.o program.s # Use .include directives to modularize assembly code echo '.include "header.s"' > main.s echo 'mov $42, %eax' > header.s as -o main.o main.s

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    Overview:

    The GNU assembler (as) is a fundamental tool in the compilation process that translates human-readable assembly language into machine code object files. These object files can then be linked together by a linker (like ld) to create executables or libraries.

    Role in the Compilation Process:

    In a typical compilation workflow:

    1. The compiler (e.g., gcc) translates high-level code to assembly
    2. The assembler (as) converts assembly to object files
    3. The linker (ld) combines object files into an executable

    Assembly Syntax:

    The GNU assembler supports multiple assembly syntax styles:

    • AT&T syntax: The default for GNU tools on Unix-like systems, where operands are specified in source-destination order
    • Intel syntax: Can be enabled with the .intel_syntax directive, where operands are specified in destination-source order

    Key Directives:

    Assembly code contains special directives that provide instructions to the assembler:

    • .text: Indicates the start of code section
    • .data: Indicates the start of data section
    • .bss: Indicates the start of uninitialized data section
    • .global symbol: Makes a symbol visible to the linker
    • .include "file": Includes another assembly file
    • .macro/.endm: Defines a macro
    • .equ symbol, value: Defines a constant
    • .align value: Aligns the next data/instruction
    • .ascii "string": Defines an ASCII string
    • .byte, .word, .long: Defines data of specific sizes

    Architecture Support:

    The GNU assembler supports multiple CPU architectures:

    • x86 and x86-64
    • ARM and AArch64
    • MIPS
    • PowerPC
    • SPARC
    • RISC-V
    • Many others through specific configuration options

    Debugging Information:

    The assembler can include debugging information in the object files:

    • -g or --gen-debug: General debugging information
    • --gdwarf-2: DWARF version 2 format (commonly used)
    • --gstabs: STABS format debugging information

    Optimization:

    The assembler performs certain optimizations:

    • Instruction size optimization (using shorter forms when possible)
    • Relaxation: Optimizing jump instructions based on distance
    • Alignment optimization for performance

    Common Error Messages:

    • "Unknown mnemonic": Invalid instruction name or syntax
    • "No such instruction": Instruction not available on target architecture
    • "Too many operands": Incorrect number of operands for instruction
    • "Invalid operand": Incorrect operand type or syntax
    • "Symbol not found": Referenced an undefined symbol
    • "Operation out of range": Value too large for instruction format

    Relationship to GCC:

    Most users don't call as directly but instead use a compiler like GCC, which automatically invokes the assembler as part of the compilation process. You can see the assembly code generated by GCC using the -S flag:

    gcc -S program.c

    Cross-Compilation:

    For cross-compilation scenarios, specific assembler variants are used:

    • arm-linux-gnueabi-as: Assembler for ARM
    • x86_64-w64-mingw32-as: Assembler for Windows 64-bit
    • These cross-assemblers are typically installed as part of a cross-compilation toolchain

    Macro Features:

    The GNU assembler provides macro capabilities for code reuse:

    • Define macros using .macro and .endm directives
    • Macros can take parameters
    • Conditional assembly with .if, .else, .endif
    • Repetition with .rept and .endr

    Important Notes:

    • Assembly language is highly architecture-specific; code written for one CPU may not work on another
    • The as command does not resolve external symbols; that's the job of the linker (ld)
    • Assembly language provides direct access to hardware but requires detailed knowledge of CPU architecture
    • Unlike high-level languages, there's minimal error checking; the assembler only catches syntax errors
    • For most applications, writing directly in assembly is not recommended due to maintenance and portability issues
    • When examining compiler-generated assembly, use -fverbose-asm with GCC to include helpful comments
    • The GNU assembler is part of the GNU Binutils package

    Related Commands:

    • gcc: GNU Compiler Collection (often invokes as automatically)
    • ld: GNU linker for combining object files
    • objdump: Display information from object files
    • nm: List symbols from object files
    • readelf: Display information about ELF files
    • ar: Create, modify, and extract from archives
    • gdb: GNU debugger for debugging compiled programs
    • strip: Discard symbols from object files
    • c++filt: Demangle C++ and Java symbols
    • addr2line: Convert addresses to file and line

    Related Commands

    These commands are frequently used alongside as or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the as command works in different scenarios.

    $ as
    View All Commands