Mastering Assembly Programming

Book Description

Incorporate the assembly language routines in your high level language applications

About This Book

  • Understand the Assembly programming concepts and the benefits of examining the AL codes generated from high level languages
  • Learn to incorporate the assembly language routines in your high level language applications
  • Understand how a CPU works when programming in high level languages

Who This Book Is For

This book is for developers who would like to learn about Assembly language. Prior programming knowledge of C and C++ is assumed.

What You Will Learn

  • Obtain deeper understanding of the underlying platform
  • Understand binary arithmetic and logic operations
  • Create elegant and efficient code in Assembly language
  • Understand how to link Assembly code to outer world
  • Obtain in-depth understanding of relevant internal mechanisms of Intel CPU
  • Write stable, efficient and elegant patches for running processes

In Detail

The Assembly language is the lowest level human readable programming language on any platform. Knowing the way things are on the Assembly level will help developers design their code in a much more elegant and efficient way. It may be produced by compiling source code from a high-level programming language (such as C/C++) but can also be written from scratch. Assembly code can be converted to machine code using an assembler.

The first section of the book starts with setting up the development environment on Windows and Linux, mentioning most common toolchains. The reader is led through the basic structure of CPU and memory, and is presented the most important Assembly instructions through examples for both Windows and Linux, 32 and 64 bits. Then the reader would understand how high level languages are translated into Assembly and then compiled into object code. Finally we will cover patching existing code, either legacy code without sources or a running code in same or remote process.

Style and approach

This book takes a step-by-step, detailed approach to Comprehensively learning Assembly Programming.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

Publisher Resources

Download Example Code

Table of Contents

  1. Preface
    1. What this book covers
    2. What you need for this book
    3. Who this book is for
    4. Conventions
    5. Reader feedback
    6. Customer support
      1. Downloading the example code
      2. Errata
      3. Piracy
      4. Questions
  2. Intel Architecture
    1. Processor registers
      1. General purpose registers
        1. Accumulators
        2. Counter
        3. Stack pointer
        4. Source and destination indices
        5. Base pointer
        6. Instruction pointer
      2. Floating point registers
      3. XMM registers
      4. Segment registers and memory organization
        1. Real mode
        2. Protected mode - segmentation
        3. Protected mode - paging
        4. Long mode - paging
      5. Control registers
      6. Debug registers
        1. Debug address registers DR0 - DR3
        2. Debug control register (DR7)
        3. Debug status register (DR6)
      7. The EFlags register
        1. Bit #0 - carry flag
        2. Bit #2 - parity flag
        3. Bit #4 - adjust flag
        4. Bit #6 - zero flag
        5. Bit #7 - sign flag
        6. Bit #8 - trap flag
        7. Bit #9 - interrupt enable flag
        8. Bit #10 - direction flag
        9. Bit #11 - overflow flag
        10. Remaining bits
    2. Summary
  3. Setting Up a Development Environment
    1. Microsoft Macro Assembler
      1. Installing Microsoft Visual Studio 2017 Community
      2. Setting up the Assembly project
    2. GNU Assembler (GAS)
      1. Installing GAS
        1. Step 1 - installing GAS
        2. Step 2 - let's test
    3. Flat Assembler
      1. Installing the Flat Assembler
      2. The first FASM program
        1. Windows
        2. Linux
    4. Summary
  4. Intel Instruction Set Architecture (ISA)
    1. Assembly source template
      1. The Windows Assembly template (32-bit)
      2. The Linux Assembly template (32-bit)
    2. Data types and their definitions
    3. A debugger
    4. The instruction set summary
      1. General purpose instructions
        1. Data transfer instructions
        2. Binary Arithmetic Instructions
        3. Decimal arithmetic instructions
        4. Logical instructions
        5. Shift and rotate instructions
        6. Bit and byte instructions
        7. Execution flow transfer instructions
        8. String instructions
        9. ENTER/LEAVE
        10. Flag control instructions
        11. Miscellaneous instructions
      2. FPU instructions
      3. Extensions
        1. AES-NI
        2. SSE
        3. Example program
    5. Summary
  5. Memory Addressing Modes
    1. Addressing code
      1. Sequential addressing
      2. Direct addressing
      3. Indirect addressing
      4. RIP based addressing
    2. Addressing data
      1. Sequential addressing
      2. Direct addressing
      3. Scale, index, base, and displacement
      4. RIP addressing
    3. Far pointers
    4. Summary
  6. Parallel Data Processing
    1. SSE
      1. Registers
      2. Revisions
    2. Biorhythm calculator
      1. The idea
      2. The algorithm
      3. Data section
      4. The code
        1. Standard header
        2. The main() function
          1. Data preparation steps
          2. Calculation loop
        3. Adjustment of sine input values
        4. Computing sine
        5. Exponentiation
        6. Factorials
    3. AVX-512
    4. Summary
  7. Macro Instructions
    1. What are macro instructions?
      1. How it works
      2. Macro instructions with parameters
      3. Variadic macro instructions
    2. An introduction to calling conventions
      1. cdecl (32-bit)
      2. stdcall (32-bit)
      3. Microsoft x64 (64-bit)
      4. AMD64 (64-bit)
    3. A note on Flat Assembler's macro capabilities
    4. Macro instructions in MASM and GAS
      1. Microsoft Macro Assembler
      2. The GNU Assembler
    5. Other assembler directives (FASM Specific)
      1. The conditional assembly
      2. Repeat directives
      3. Inclusion directives
        1. The include directive
        2. File directive
    6. Summary
  8. Data Structures
    1. Arrays
      1. Simple byte arrays
      2. Arrays of words, double words, and quad words
    2. Structures
      1. Addressing structure members
      2. Arrays of structures
      3. Arrays of pointers to structures
    3. Linked lists
      1. Special cases of linked lists
        1. Stack
        2. Queue and deque
        3. Priority queues
        4. Cyclic linked list
        5. Summary for special cases of linked lists
    4. Trees
      1. A practical example
        1. Example - trivial cryptographic virtual machine
          1. Virtual machine architecture
          2. Adding support for a virtual processor to the Flat Assembler
          3. Virtual code
          4. The virtual processor
          5. Searching the tree
          6. The loop
      2. Tree balancing
    5. Sparse matrices
    6. Graphs
    7. Summary
  9. Mixing Modules Written in Assembly and Those Written in High-Level Languages
    1. Crypto Core
      1. Portability
        1. Specifying the output format
        2. Conditional declaration of code and data sections
        3. Exporting symbols
      2. Core procedures
        1. Encryption/decryption
        2. Setting the encryption/decryption parameters
          1. f_set_data_pointer
          2. f_set_data_length
        3. GetPointers()
    2. Interfacing with C/C++
      1. Static linking - Visual Studio 2017
      2. Static linking - GCC
      3. Dynamic linking
    3. Assembly and managed code
      1. Native structure versus managed structure
      2. Importing from DLL/SO and function pointers
    4. Summary
  10. Operating System Interface
    1. The rings
    2. System call
      1. System call hardware interface
      2. Direct system calls
      3. Indirect system calls
    3. Using libraries
      1. Windows
        1. Linking against object and/or library files
          1. Object file
          2. Producing the executable
        2. Importing procedures from DLL
      2. Linux
        1. Linking against object and/or library files
          1. Object file
          2. Producing the executable
        2. Dynamic linking of ELF
          1. The code
    4. Summary
  11. Patching Legacy Code
    1. The executable
    2. The issue
    3. PE files
      1. Headers
      2. Imports
      3. Gathering information
        1. Locating calls to gets()
        2. Preparing for the patch
      4. Importing fgets()
      5. Patching calls
      6. Shim code
      7. Applying the patch
      8. A complex scenario
        1. Preparing the patch
        2. Adjusting file headers
        3. Appending a new section
        4. Fixing the call instruction
    4. ELF executables
      1. LD_PRELOAD
      2. A shared object
    5. Summary
  12. Oh, Almost Forgot
    1. Protecting the code
      1. The original code
      2. The call
      3. The call obfuscation macro
    2. A bit of kernel space
      1. LKM structure
      2. LKM source
        1. .init.text
        2. .exit.text
        3. .rodata.str1.1
        4. .modinfo
        5. .gnu.linkonce.this_module
        6. __versions
      3. Testing the LKM
    3. Summary

Product Information

  • Title: Mastering Assembly Programming
  • Author(s): Alexey Lyashko
  • Release date: September 2017
  • Publisher(s): Packt Publishing
  • ISBN: 9781787287488