You are previewing Linux Assembly Language Programming.
O'Reilly logo
Linux Assembly Language Programming

Book Description

  • Master x86 assembly language from a Linux perspective!

  • Essential information for creating Linux device drivers

  • How Linux works "under the hood!"

The first Linux-centered guide to x86 assembly language!

In Linux Assembly Language Programming, Bob Neveln explains all the key features of x86 assembly language in the context of the Linux operating system and the C language. The book's step-by-step, one-concept-at-a-time coverage will help any hardware programmer move to Linux, and master essential skills for Linux device driver development. You won't just learn new x86 assembly language skills: you'll also gain powerful "under the hood" insight into how Linux works.

Linux x86 assembly language programming, from start to finish!

  • Place-holding numeration

  • Logic circuits

  • Computation

  • The four-field format

  • Machine language

  • Memory

  • The stack

  • Linux user programs

  • NASM

  • DOS programs

  • Interrupts

  • Bit manipulations

  • Device drivers

  • And more...

Table of Contents

  1. Copyright
    1. Dedication
  2. PREFACE
  3. 1. INTRODUCTION
    1. 1.1. The Fetch-Execute Cycle
    2. 1.2. The Linux Operating System
    3. 1.3. The Gnu C Compiler
    4. 1.4. The Edlinas Assembler
      1. 1.4.1. Editing
      2. 1.4.2. Assembling
      3. 1.4.3. Interpreting
      4. 1.4.4. Dosemu
    5. 1.5. NASM
    6. 1.6. Other Assemblers
      1. Further Reading
        1. Bibliography
  4. 2. PLACEHOLDING NUMERATION
    1. 2.1. The Decimal and Pentimal Systems
      1. 2.1.1. Purely Decimal Money
      2. 2.1.2. Pentimal Money
      3. 2.1.3. Pentimal Numerals
    2. 2.2. Pentimal Arithmetic
      1. 2.2.1. Native Addition
      2. 2.2.2. Native Subtraction
    3. 2.3. Conversion to Pentimal
    4. 2.4. The Binary System
      1. 2.4.1. Binary Addition
      2. 2.4.2. Binary Subtraction
      3. 2.4.3. Conversion to Binary
    5. 2.5. Memory as a Rectangle of Bits
    6. 2.6. The Hexadecimal System
      1. 2.6.1. Addition in Hex
      2. 2.6.2. Subtraction in Hex
      3. 2.6.3. Conversion to Hex
      4. 2.6.4. Why not Octal?
    7. 2.7. Base Distinguishing Notations
    8. 2.8. * Fractions in Other Bases
    9. 2.9. * Converting Fractions
      1. Further Reading
        1. Bibliography
  5. 3. LOGIC CIRCUITS AND COMPUTATION
    1. 3.1. The NOT Gate
      1. 3.1.1. Logic Gate Terminals
        1. Logic Value Conventions
      2. 3.1.2. Behavior of the NOT Gate
      3. 3.1.3. Truth Table for the NOT Gate
      4. 3.1.4. Gate Level Diagrams
    2. 3.2. Boolean Operators
      1. 3.2.1. The OR Operator
      2. 3.2.2. The AND Operator
      3. 3.2.3. Boolean Expressions
    3. 3.3. Logic Gates
      1. 3.3.1. The OR Gate
      2. 3.3.2. The AND Gate
      3. 3.3.3. Boolean Circuits
      4. 3.3.4. Propagation Delay
    4. 3.4. Addition Circuits
    5. 3.5. Sequential Circuits
      1. 3.5.1. A Flip-flop
      2. 3.5.2. A Latch
      3. 3.5.3. Registers
      4. 3.5.4. Bit Significance
    6. 3.6. Negative Number Representation
      1. 3.6.1. Signed Magnitude Addition
      2. 3.6.2. Easy Addition
      3. 3.6.3. Odometer Arithmetic
      4. 3.6.4. Register Arithmetic
      5. 3.6.5. Signed vs. Unsigned Numbers
    7. 3.7. Subtraction Using Negation
      1. 3.7.1. Two's Complementation
      2. 3.7.2. Two's Complementation in Hex
      3. 3.7.3. Conversions
    8. 3.8. * Placeholding Two's Complement
    9. 3.9. Memory Circuits
      1. 3.9.1. Tri-State Outputs
        1. Definition of Memory Capacity
        2. SRAM vs. DRAM
    10. 3.10. x86 General Registers and their Ancestry
      1. 3.10.1. 8080 Registers
      2. 3.10.2. 8086 Registers
      3. 3.10.3. 80386 Registers
    11. 3.11. The MOV Command
      1. 3.11.1. MOV reg,imm
      2. 3.11.2. MOV reg,reg
      3. 3.11.3. Ambiguity Problem
    12. 3.12. Addition and Subtraction Commands
      1. 3.12.1. ADD reg.imm
      2. 3.12.2. ADD reg.reg
      3. 3.12.3. SUB reg.imm
      4. 3.12.4. SUB reg.reg
    13. 3.13. * Multiplication and Division Commands
      1. 3.13.1. The MUL Command
      2. 3.13.2. The DIV Command
      3. 3.13.3. Negative Numbers and Congruences
        1. Congruence Addition Theorem
        2. Congruence Multiplication Theorem
      4. Further Reading
        1. Bibliography
  6. 4. ASSEMBLY LANGUAGE
    1. 4.1. The Four Field Format
    2. 4.2. Computers from the CPU Standpoint
      1. 4.2.1. Memory vs. I/O
      2. 4.2.2. The IN Command
      3. 4.2.3. The OUT Command
      4. 4.2.4. Memory Mapped I/O
    3. 4.3. Simple Assembly Language Programs
      1. 4.3.1. Edlinas Ports
      2. 4.3.2. The RET Command
      3. 4.3.3. Program to Add Two Numbers
    4. 4.4. Assembler Programs with Jumps
      1. 4.4.1. The JMP Command
      2. 4.4.2. Conditional Jumps and Flags
        1. The Zero and Sign Flags
      3. 4.4.3. The JZ, JS, JNZ, and JNS Commands
    5. 4.5. Assembler Programs with Loops
      1. The INC and DEC Commands
    6. 4.6. Signed Comparisons
      1. 4.6.1. Comparison-Based Jumps
      2. 4.6.2. The Overflow Flag
      3. 4.6.3. The CMP Command
      4. 4.6.4. More Jump Commands
    7. 4.7. Unsigned Comparisons
      1. 4.7.1. The Carry Flag
      2. 4.7.2. Out of Range Flag Summary
      3. 4.7.3. Still More Jump Commands
    8. 4.8. Linux .s files
      1. 4.8.1. The Pentium's Dual Pipeline
  7. 5. MACHINE LANGUAGE
    1. 5.1. Assembling Simple Programs
      1. 5.1.1. Register Codes
      2. 5.1.2. Little Endian Immediate Codes
      3. 5.1.3. Relative Jumps
      4. 5.1.4. Short Jumps
    2. 5.2. Opcode Space
    3. 5.3. The ModRM Byte
      1. 5.3.1. rv,rmv Coding
      2. 5.3.2. rmv.rv Coding
      3. 5.3.3. Nonregister R Bits
    4. 5.4. 386 Space (OF + …)
    5. 5.5. 32-Bit vs. 16-Bit Code
    6. 5.6. The 8-Bit Registers
    7. 5.7. Linux .0 Files
  8. 6. MEMORY
    1. 6.1. 4-Byte Data Width
      1. 6.1.1. Storage Protocols
      2. 6.1.2. Little and Big Endian Architecture
      3. 6.1.3. Alignment
    2. 6.2. Addresses in Brackets
      1. 6.2.1. The [imm] Format
        1. 64-Bit Arithmetic
        2. Powers of 3
      2. 6.2.2. The [reg] Format
      3. 6.2.3. The [reg+scale*reg+imm] Format
      4. 6.2.4. 16-Bit Addresses
      5. 6.2.5. CISC vs. RISC
    3. 6.3. Operand Size Ambiguity
    4. 6.4. Labels
      1. 6.4.1. Address Encoding
      2. 6.4.2. Program Loading
      3. 6.4.3. Variables
      4. 6.4.4. Arrays
    5. 6.5. Immediate Storage
      1. 6.5.1. Byte Definition Assembler Directive
      2. 6.5.2. Hand Coding of Instructions
      3. 6.5.3. Initializing Data
  9. 7. THE STACK
    1. 7.1. Push and Pop Operations
      1. 7.1.1. The PUSH Command
      2. 7.1.2. The POP Command
    2. 7.2. Subprograms
      1. 7.2.1. The CALL and RET Commands
    3. 7.3. Parameter Passing
      1. 7.3.1. The Global Assembler Directive
      2. 7.3.2. Calling Assembler from C
    4. 7.4. Recursion
  10. 8. LINUX USER PROGRAMS
    1. 8.1. Multitasking
    2. 8.2. Paging
      1. 8.2.1. Virtual Memory
        1. 4K Pages
      2. 8.2.2. Paging vs. Swapping
        1. Page Faults
      3. 8.2.3. Kernel Addresses
      4. 8.2.4. Kernel Pages
      5. 8.2.5. Kernel Modules
    3. 8.3. Address Translation
      1. 8.3.1. Two-Tiered x86 Page Tables
      2. 8.3.2. Address Parsing
      3. 8.3.3. Translation Example
      4. 8.3.4. Page Table Entries
      5. 8.3.5. Memory Protection
      6. 8.3.6. The CR3 Register
      7. 8.3.7. Kernel Memory Access
      8. 8.3.8. Paging Page Tables
    4. 8.4. Program Segments
      1. 8.4.1. Non-Writeable Memory
      2. 8.4.2. Writeable Memory
      3. 8.4.3. Text and Data Segments
      4. 8.4.4. Text and Data Sections
      5. 8.4.5. Section Definition Directives
      6. 8.4.6. Segments in C Code
      7. 8.4.7. Calling C from Assembler
    5. 8.5. Other Data Segments
      1. 8.5.1. Initialized Data Space
      2. 8.5.2. Uninitialized Data Space
      3. 8.5.3. Dynamically Allocated Memory
    6. 8.6. I/O Protection
    7. 8.7. Executable Files in ELF Format
      1. 8.7.1. The ELF Header
      2. 8.7.2. The Program Header Table
      3. 8.7.3. ELF Segments
    8. 8.8. Object Files in ELF Format
      1. 8.8.1. Section Header Table
      2. 8.8.2. Sections
      3. Further Reading
        1. Bibliography
  11. 9. INTERRUPTS
    1. 9.1. Polling
      1. 9.1.1. Data Ports
      2. 9.1.2. Status Ports
    2. 9.2. External Interrupts
      1. 9.2.1. x86 Interrupt Handling
        1. The Interrupt Acknowledge Bus Cycle
        2. ISR Address Fetching
        3. The Interrupt Descriptor Table
        4. ISR Execution
      2. 9.2.2. Interrupting an Interrupt
        1. The Interrupt Enable Flag
        2. Nested Interrupts
        3. The EOI Command
      3. 9.2.3. The NMI and RESET Interrupts
    3. 9.3. ISA Architecture
      1. 9.3.1. The 8259 Interrupt Controller
      2. 9.3.2. IRQs
    4. 9.4. Internal and Software Interrupts
      1. 9.4.1. Exceptions
      2. 9.4.2. Software Interrupts
      3. 9.4.3. Faults and Traps
    5. 9.5. System Calls
    6. 9.6. Privilege Levels
      1. 9.6.1. The CPL Register
      2. 9.6.2. Privileged Instructions
      3. 9.6.3. Stack Problems
    7. 9.7. Control Transfer
      1. 9.7.1. Clear the Interrupt Enable Flag?
      2. 9.7.2. Store 0 into the CPL
      3. 9.7.3. Change Stacks
      4. 9.7.4. Jump to the ISR
    8. 9.8. Scheduling
      1. Further Reading
        1. Bibliography
  12. 10. BIT MANIPULATIONS
    1. 10.1. Bitwise Logic Operations
    2. 10.2. The AND, OR, NOT, and XOR Commands
    3. 10.3. Bit Setting and Testing
      1. 10.3.1. Turning the Speaker On and Off
      2. 10.3.2. Edge vs. Level Triggered IRQ Bits
      3. 10.3.3. Disallowing Non—maskable Interrupts
    4. 10.4. Shift Instructions
  13. 11. DEVICE DRIVERS
    1. 11.1. Device-Independent Files
      1. 11.1.1. Floppy Drive Controller
      2. 11.1.2. Bad Old Files
      3. 11.1.3. * Direct Memory Access
    2. 11.2. Devices as Files
    3. 11.3. Morse Code Speaker Driver
      1. 11.3.1. The file_operations Structure
      2. 11.3.2. Timing Using Delay Loops
      3. 11.3.3. The Timer Queue
      4. 11.3.4. Device Memory
      5. 11.3.5. Wait Queues
      6. 11.3.6. Signals
        1. Drivers that Can't Be Killed
      7. 11.3.7. States of a Process
      8. 11.3.8. Timing the Morse Code
      9. 11.3.9. Assembly Language Speaker Code
      10. 11.3.10. C Language Driver Code
      11. 11.3.11. Loose Ends
    4. 11.4. Serial Port Digitizer Driver
      1. 11.4.1. Setting Up a New Interrupt Handler
      2. 11.4.2. Scalex Interrupt Handler
      3. 11.4.3. Scalex System Call Handlers
      4. Further Reading
        1. Bibliography
  14. 12. DOS PROGRAMS
    1. 12.1. Real Mode Segmentation
      1. Segments and Segmentation
      2. 12.1.1. Segment Registers
      3. 12.1.2. Segment:Offset Notation
      4. 12.1.3. Default Segments
    2. 12.2. Edlinas Environment Variables
    3. 12.3. Fixed Memory Areas
      1. 12.3.1. Video Buffer
      2. 12.3.2. Keyboard Buffer
    4. 12.4. Real Mode Interrupts
      1. 12.4.1. DOS Interrupts
      2. 12.4.2. BIOS Interrupts
      3. 12.4.3. The Real Mode Interrupt Table
        1. Interrupt Vectors
    5. 12.5. Checking DOS Memory
      1. Further Reading
        1. Bibliography
  15. 13. LINUX BOOT TIME PROGRAMS
    1. 13.1. Changing to Protected Mode
    2. 13.2. Protected Mode Segmentation
      1. 13.2.1. Protected Mode Memory Segments
      2. 13.2.2. Special Memory Segments, the GDT and the IDT
      3. 13.2.3. Selectors
      4. 13.2.4. Segment Operations
      5. 13.2.5. Descriptor Encoding
      6. 13.2.6. Task Isolation via Local Descriptor Tables
    3. 13.3. Setting Up the Global Descriptor Table
    4. 13.4. Closing