Yasm User Manual

Peter Johnson

2007

Abstract

This document is the user manual for the Yasm assembler. It is intended as both an introduction and a general-purpose reference for all Yasm users.


Table of Contents

Introduction
1. License
2. Material Covered in this Book
1. Running Yasm
1.1. yasm Synopsis
1.2. Description
1.3. Options
1.3.1. General Options
1.3.2. Warning Options
1.3.3. Preprocessor Options
1.4. Supported Target Architectures
1.5. Supported Parsers (Syntaxes)
1.6. Supported Object Formats
1.7. Supported Debugging Formats
1.8. Examples
I. NASM Syntax
2. The NASM Language
2.1. Layout of a NASM Source Line
2.2. Pseudo-Instructions
2.2.1. DB and Friends: Declaring Initialized Data
2.2.2. RESB and Friends: Declaring Uninitialized Data
2.2.3. INCBIN: Including External Binary Files
2.2.4. EQU: Defining Constants
2.2.5. TIMES: Repeating Instructions or Data
2.3. Effective Addresses
2.3.1. 64-bit Displacements
2.3.2. RIP Relative Addressing
2.4. Immediate Operands
2.5. Constants
2.5.1. Numeric Constants
2.5.2. Character Constants
2.5.3. String Constants
2.5.4. Floating-Point Constants
2.6. Expressions
2.6.1. |: Bitwise OR Operator
2.6.2. ^: Bitwise XOR Operator
2.6.3. &: Bitwise AND Operator
2.6.4. << and >>: Bit Shift Operators
2.6.5. + and -: Addition and Subtraction Operators
2.6.6. *, /, //, % and %%: Multiplication and Division
2.6.7. Unary Operators: +, -, ~ and SEG
2.7. SEG and WRT
2.8. STRICT: Inhibiting Optimization
2.9. Critical Expressions
2.10. Local Labels
3. The NASM Preprocessor
3.1. Single-Line Macros
3.1.1. The Normal Way: %define
3.1.2. Enhancing %define: %xdefine
3.1.3. Concatenating Single Line Macro Tokens: %+
3.1.4. Undefining macros: %undef
3.1.5. Preprocessor Variables: %assign
3.2. String Handling in Macros
3.2.1. String Length: %strlen
3.2.2. Sub-strings: %substr
3.3. Multi-Line Macros
3.3.1. Overloading Multi-Line Macros
3.3.2. Macro-Local Labels
3.3.3. Greedy Macro Parameters
3.3.4. Default Macro Parameters
3.3.5. %0: Macro Parameter Counter
3.3.6. %rotate: Rotating Macro Parameters
3.3.7. Concatenating Macro Parameters
3.3.8. Condition Codes as Macro Parameters
3.3.9. Disabling Listing Expansion
3.4. Conditional Assembly
3.4.1. %ifdef: Testing Single-Line Macro Existence
3.4.2. %ifmacro: Testing Multi-Line Macro Existence
3.4.3. %ifctx: Testing the Context Stack
3.4.4. %if: Testing Arbitrary Numeric Expressions
3.4.5. %ifidn and %ifidni: Testing Exact Text Identity
3.4.6. %ifid, %ifnum, %ifstr: Testing Token Types
3.4.7. %error: Reporting User-Defined Errors
3.5. Preprocessor Loops
3.6. Including Other Files
3.7. The Context Stack
3.7.1. %push and %pop: Creating and Removing Contexts
3.7.2. Context-Local Labels
3.7.3. Context-Local Single-Line Macros
3.7.4. %repl: Renaming a Context
3.7.5. Example Use of the Context Stack: Block IFs
3.8. Standard Macros
3.8.1. __YASM_MAJOR__, etc: Yasm Version
3.8.2. __FILE__ and __LINE__: File Name and Line Number
3.8.3. __YASM_OBJFMT__ and __OUTPUT_FORMAT__: Output Object Format Keyword
3.8.4. STRUC and ENDSTRUC: Declaring Structure Data Types
3.8.5. ISTRUC, AT and IEND: Declaring Instances of Structures
3.8.6. ALIGN and ALIGNB: Data Alignment
4. NASM Assembler Directives
4.1. Specifying Target Processor Mode
4.1.1. BITS
4.1.2. USE16, USE32, and USE64
4.2. DEFAULT: Change the assembler defaults
4.3. Changing and Defining Sections
4.3.1. SECTION and SEGMENT
4.3.2. Standardized Section Names
4.3.3. The __SECT__ Macro
4.4. ABSOLUTE: Defining Absolute Labels
4.5. EXTERN: Importing Symbols
4.6. GLOBAL: Exporting Symbols
4.7. COMMON: Defining Common Data Areas
4.8. CPU: Defining CPU Dependencies
II. GAS Syntax
III. Object Formats
5. bin: Flat-Form Binary Output
5.1. ORG: Binary Origin
5.2. bin Extensions to the SECTION Directive
5.3. bin Special Symbols
5.4. Map Files
6. coff: Common Object File Format
7. elf32: Executable and Linkable Format 32-bit Object Files
7.1. Debugging Format Support
7.2. ELF Sections
7.3. ELF Directives
7.3.1. IDENT: Add file identification
7.3.2. SIZE: Set symbol size
7.3.3. TYPE: Set symbol type
7.3.4. WEAK: Create weak symbol
7.4. ELF Extensions to the GLOBAL Directive
7.5. ELF Extensions to the COMMON Directive
7.6. elf32 Special Symbols and WRT
8. elf64: Executable and Linkable Format 64-bit Object Files
8.1. elf64 Special Symbols and WRT
9. macho32: Mach 32-bit Object File Format
10. macho64: Mach 64-bit Object File Format
11. rdf: Relocatable Dynamic Object File Format
12. win32: Microsoft Win32 Object Files
13. win64: PE32+ (Microsoft Win64) Object Files
13.1. win64 Extensions to the SECTION Directive
13.2. win64 Structured Exception Handling
13.2.1. x64 Stack, Register and Function Parameter Conventions
13.2.2. Types of Functions
13.2.3. Frame Function Structure
13.2.4. Stack Frame Details
13.2.5. Yasm Primitives for Unwind Operations
13.2.6. Yasm Macros for Formal Stack Operations
14. xdf: Extended Dynamic Object Format
IV. Debugging Formats
15. cv8: CodeView Debugging Format for VC8
16. dwarf2: DWARF2 Debugging Format
17. stabs: Stabs Debugging Format
V. Architectures
18. x86 Architecture
18.1. Instructions
18.2. Execution Modes and Extensions
18.2.1. CPU Options
18.3. Registers
18.4. Segmentation
Index

List of Figures

13.1. x64 Calling Convention
13.2. x64 Detailed Stack Frame
18.1. x86 General Purpose Registers

List of Tables

5.1. bin Section Attributes
7.1. ELF Section Attributes
7.2. ELF Standard Sections
13.1. Function Structured Exception Handling Rules
18.1. x86 CPU Feature Flags
18.2. x86 CPU Names

List of Examples

13.1. Win64 Unwind Primitives
13.2. Win64 Unwind Macros