Siêu thị PDFTải ngay đi em, trời tối mất

Thư viện tri thức trực tuyến

Kho tài liệu với 50,000+ tài liệu học thuật

© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Modern X86 Assembly Language Programming
PREMIUM
Số trang
617
Kích thước
17.4 MB
Định dạng
PDF
Lượt xem
1807

Modern X86 Assembly Language Programming

Nội dung xem thử

Mô tả chi tiết

Modern X86

Assembly Language

Programming

Covers x86 64-bit, AVX,

AVX2, and AVX-512

Second Edition

Daniel Kusswurm

Modern X86 Assembly

Language Programming

Covers x86 64-bit, AVX, AVX2, and AVX-512

Second Edition

Daniel Kusswurm

Modern X86 Assembly Language Programming: Covers x86 64-bit, AVX, AVX2, and AVX-512

Daniel Kusswurm

Geneva, IL, USA

ISBN-13 (pbk): 978-1-4842-4062-5 ISBN-13 (electronic): 978-1-4842-4063-2

https://doi.org/10.1007/978-1-4842-4063-2

Library of Congress Control Number: 2018964262

Copyright © 2018 by Daniel Kusswurm

This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the

material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation,

broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage

and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or

hereafter developed.

Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with

every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an

editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.

The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are

not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to

proprietary rights.

While the advice and information in this book are believed to be true and accurate at the date of publication,

neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or

omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material

contained herein.

Managing Director, Apress Media LLC: Welmoed Spahr

Acquisitions Editor: Steve Anglin

Development Editor: Matthew Moodie

Coordinating Editor: Mark Powers

Cover designed by eStudioCalamar

Cover image designed by Freepik (www.freepik.com)

Distributed to the book trade worldwide by Springer Science+Business Media New York,

233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail

[email protected], or visit www.springeronline.com. Apress Media, LLC is a California LLC

and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc).

SSBM Finance Inc is a Delaware corporation.

For information on translations, please e-mail [email protected]; for reprint, paperback, or audio

rights, please email [email protected].

Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions

and licenses are also available for most titles. For more information, reference our Print and eBook Bulk

Sales web page at http://www.apress.com/bulk-sales.

Any source code or other supplementary material referenced by the author in this book is available to

readers on GitHub via the book’s product page, located at www.apress.com/9781484240625. For more

detailed information, please visit http://www.apress.com/source-code.

Printed on acid-free paper

This book is dedicated to those individuals who suffer the ravages of

Alzheimer’s disease and their unsung compassionate caregivers.

v

Contents

About the Author ��������������������������������������������������������������������������������������������������� xiii

About the Technical Reviewer ���������������������������������������������������������������������������������xv

Acknowledgments�������������������������������������������������������������������������������������������������xvii

Introduction������������������������������������������������������������������������������������������������������������xix

■Chapter 1: X86-64 Core Architecture �������������������������������������������������������������������� 1

Historical Overview ���������������������������������������������������������������������������������������������������������� 1

Data Types������������������������������������������������������������������������������������������������������������������������ 3

Fundamental Data Types ������������������������������������������������������������������������������������������������������������������������ 3

Numerical Data Types����������������������������������������������������������������������������������������������������������������������������� 4

SIMD Data Types������������������������������������������������������������������������������������������������������������������������������������� 5

Miscellaneous Data Types ���������������������������������������������������������������������������������������������������������������������� 6

Internal Architecture��������������������������������������������������������������������������������������������������������� 6

General-Purpose Registers��������������������������������������������������������������������������������������������������������������������� 7

RFLAGS Register������������������������������������������������������������������������������������������������������������������������������������� 9

Instruction Pointer�������������������������������������������������������������������������������������������������������������������������������� 10

Instruction Operands ���������������������������������������������������������������������������������������������������������������������������� 10

Memory Addressing������������������������������������������������������������������������������������������������������������������������������ 11

Differences Between x86-64 and x86-32 Programming������������������������������������������������ 13

Invalid Instructions ������������������������������������������������������������������������������������������������������������������������������� 15

Deprecated Instructions ����������������������������������������������������������������������������������������������������������������������� 15

Instruction Set Overview������������������������������������������������������������������������������������������������ 15

Summary������������������������������������������������������������������������������������������������������������������������ 18

■ Contents

vi

■Chapter 2: X86-64 Core Programming – Part 1��������������������������������������������������� 21

Simple Integer Arithmetic����������������������������������������������������������������������������������������������� 21

Addition and Subtraction ���������������������������������������������������������������������������������������������������������������������� 22

Logical Operations�������������������������������������������������������������������������������������������������������������������������������� 24

Shift Operations������������������������������������������������������������������������������������������������������������������������������������ 27

Advanced Integer Arithmetic ������������������������������������������������������������������������������������������ 30

Multiplication and Division ������������������������������������������������������������������������������������������������������������������� 31

Calculations Using Mixed Types ����������������������������������������������������������������������������������������������������������� 35

Memory Addressing and Condition Codes���������������������������������������������������������������������� 40

Memory Addressing Modes ������������������������������������������������������������������������������������������������������������������ 40

Condition Codes������������������������������������������������������������������������������������������������������������������������������������ 44

Summary������������������������������������������������������������������������������������������������������������������������ 49

■Chapter 3: X86-64 Core Programming – Part 2��������������������������������������������������� 51

Arrays ����������������������������������������������������������������������������������������������������������������������������� 51

One-Dimensional Arrays ����������������������������������������������������������������������������������������������������������������������� 51

Two-Dimensional Arrays����������������������������������������������������������������������������������������������������������������������� 58

Structures����������������������������������������������������������������������������������������������������������������������� 68

Strings���������������������������������������������������������������������������������������������������������������������������� 71

Counting Characters ����������������������������������������������������������������������������������������������������������������������������� 71

String Concatenation ���������������������������������������������������������������������������������������������������������������������������� 74

Comparing Arrays ��������������������������������������������������������������������������������������������������������������������������������� 79

Array Reversal �������������������������������������������������������������������������������������������������������������������������������������� 82

Summary������������������������������������������������������������������������������������������������������������������������ 86

■Chapter 4: Advanced Vector Extensions�������������������������������������������������������������� 87

AVX Overview����������������������������������������������������������������������������������������������������������������� 87

SIMD Programming Concepts ���������������������������������������������������������������������������������������� 88

Wraparound vs. Saturated Arithmetic ���������������������������������������������������������������������������� 90

AVX Execution Environment ������������������������������������������������������������������������������������������� 91

Register Set������������������������������������������������������������������������������������������������������������������������������������������ 91

■ Contents

vii

Data Types �������������������������������������������������������������������������������������������������������������������������������������������� 92

Instruction Syntax��������������������������������������������������������������������������������������������������������������������������������� 93

AVX Scalar Floating-Point����������������������������������������������������������������������������������������������� 94

Floating-Point Programming Concepts������������������������������������������������������������������������������������������������� 94

Scalar Floating-Point Register Set�������������������������������������������������������������������������������������������������������� 97

Control-Status Register������������������������������������������������������������������������������������������������������������������������ 97

Instruction Set Overview ���������������������������������������������������������������������������������������������������������������������� 98

AVX Packed Floating-Point������������������������������������������������������������������������������������������� 100

Instruction Set Overview �������������������������������������������������������������������������������������������������������������������� 101

AVX Packed Integer������������������������������������������������������������������������������������������������������ 103

Instruction Set Overview �������������������������������������������������������������������������������������������������������������������� 104

Differences Between x86-AVX and x86-SSE ���������������������������������������������������������������� 105

Summary���������������������������������������������������������������������������������������������������������������������� 107

■Chapter 5: AVX Programming – Scalar Floating-Point �������������������������������������� 109

Scalar Floating-Point Arithmetic ���������������������������������������������������������������������������������� 109

Single-Precision Floating-Point���������������������������������������������������������������������������������������������������������� 110

Double-Precision Floating-Point��������������������������������������������������������������������������������������������������������� 112

Scalar Floating-Point Compares and Conversions ������������������������������������������������������� 118

Floating-Point Compares �������������������������������������������������������������������������������������������������������������������� 118

Floating-Point Conversions����������������������������������������������������������������������������������������������������������������� 128

Scalar Floating-Point Arrays and Matrices������������������������������������������������������������������� 135

Floating-Point Arrays �������������������������������������������������������������������������������������������������������������������������� 135

Floating-Point Matrices ���������������������������������������������������������������������������������������������������������������������� 138

Calling Convention�������������������������������������������������������������������������������������������������������� 143

Basic Stack Frames���������������������������������������������������������������������������������������������������������������������������� 144

Using Non-Volatile General-Purpose Registers ���������������������������������������������������������������������������������� 148

Using Non-Volatile XMM Registers ����������������������������������������������������������������������������������������������������� 153

Macros for Prologs and Epilogs���������������������������������������������������������������������������������������������������������� 159

Summary���������������������������������������������������������������������������������������������������������������������� 166

■ Contents

viii

■Chapter 6: AVX Programming – Packed Floating-Point������������������������������������� 167

Packed Floating-Point Arithmetic��������������������������������������������������������������������������������� 167

Packed Floating-Point Compares ��������������������������������������������������������������������������������� 173

Packed Floating-Point Conversions������������������������������������������������������������������������������ 179

Packed Floating-Point Arrays ��������������������������������������������������������������������������������������� 183

Packed Floating-Point Square Roots �������������������������������������������������������������������������������������������������� 184

Packed Floating-Point Array Min-Max ������������������������������������������������������������������������������������������������ 188

Packed Floating-Point Least Squares������������������������������������������������������������������������������������������������� 193

Packed Floating-Point Matrices ����������������������������������������������������������������������������������� 199

Matrix Transposition ��������������������������������������������������������������������������������������������������������������������������� 199

Matrix Multiplication��������������������������������������������������������������������������������������������������������������������������� 207

Summary���������������������������������������������������������������������������������������������������������������������� 214

■Chapter 7: AVX Programming – Packed Integers ���������������������������������������������� 215

Packed Integer Addition and Subtraction��������������������������������������������������������������������� 215

Packed Integer Shifts ��������������������������������������������������������������������������������������������������� 221

Packed Integer Multiplication��������������������������������������������������������������������������������������� 226

Packed Integer Image Processing�������������������������������������������������������������������������������� 232

Pixel Minimum-Maximum Values ������������������������������������������������������������������������������������������������������� 232

Pixel Mean Intensity ��������������������������������������������������������������������������������������������������������������������������� 240

Pixel Conversions ������������������������������������������������������������������������������������������������������������������������������� 246

Image Histograms ������������������������������������������������������������������������������������������������������������������������������ 255

Image Thresholding ���������������������������������������������������������������������������������������������������������������������������� 262

Summary���������������������������������������������������������������������������������������������������������������������� 274

■Chapter 8: Advanced Vector Extensions 2��������������������������������������������������������� 277

AVX2 Execution Environment ��������������������������������������������������������������������������������������� 277

AVX2 Packed Floating-Point����������������������������������������������������������������������������������������� 278

AVX2 Packed Integer���������������������������������������������������������������������������������������������������� 279

X86 Instruction Set Extensions������������������������������������������������������������������������������������� 280

Half-Precision Floating-Point�������������������������������������������������������������������������������������������������������������� 280

■ Contents

ix

Fused-Multiply-Add (FMA)������������������������������������������������������������������������������������������������������������������ 281

General-Purpose Register Instruction Set Extensions������������������������������������������������������������������������ 282

Summary���������������������������������������������������������������������������������������������������������������������� 283

■Chapter 9: AVX2 Programming – Packed Floating-Point����������������������������������� 285

Packed Floating-Point Arithmetic��������������������������������������������������������������������������������� 285

Packed Floating-Point Arrays ��������������������������������������������������������������������������������������� 292

Simple Calculations ���������������������������������������������������������������������������������������������������������������������������� 292

Column Means������������������������������������������������������������������������������������������������������������������������������������ 298

Correlation Coefficient������������������������������������������������������������������������������������������������������������������������ 305

Matrix Multiplication and Transposition ����������������������������������������������������������������������� 312

Matrix Inversion������������������������������������������������������������������������������������������������������������ 320

Blend and Permute Instructions����������������������������������������������������������������������������������� 333

Data Gather Instructions����������������������������������������������������������������������������������������������� 339

Summary���������������������������������������������������������������������������������������������������������������������� 346

■Chapter 10: AVX2 Programming – Packed Integers ������������������������������������������ 347

Packed Integer Fundamentals�������������������������������������������������������������������������������������� 347

Basic Arithmetic ��������������������������������������������������������������������������������������������������������������������������������� 347

Pack and Unpack�������������������������������������������������������������������������������������������������������������������������������� 352

Size Promotions���������������������������������������������������������������������������������������������������������������������������������� 358

Packed Integer Image Processing�������������������������������������������������������������������������������� 363

Pixel Clipping�������������������������������������������������������������������������������������������������������������������������������������� 363

RGB Pixel Min-Max Values������������������������������������������������������������������������������������������������������������������ 369

RGB to Grayscale Conversion ������������������������������������������������������������������������������������������������������������� 376

Summary���������������������������������������������������������������������������������������������������������������������� 384

■Chapter 11: AVX2 Programming – Extended Instructions ��������������������������������� 385

FMA Programming ������������������������������������������������������������������������������������������������������� 385

Convolutions ��������������������������������������������������������������������������������������������������������������������������������������� 385

Scalar FMA ����������������������������������������������������������������������������������������������������������������������������������������� 388

Packed FMA���������������������������������������������������������������������������������������������������������������������������������������� 398

■ Contents

x

General-Purpose Register Instructions ������������������������������������������������������������������������ 406

Flagless Multiplication and Shifts������������������������������������������������������������������������������������������������������� 406

Enhanced Bit Manipulation����������������������������������������������������������������������������������������������������������������� 412

Half-Precision Floating-Point Conversions������������������������������������������������������������������� 415

Summary���������������������������������������������������������������������������������������������������������������������� 419

■Chapter 12: Advanced Vector Extensions 512��������������������������������������������������� 421

AVX-512 Overview�������������������������������������������������������������������������������������������������������� 421

AVX-512 Execution Environment���������������������������������������������������������������������������������� 422

Register Sets �������������������������������������������������������������������������������������������������������������������������������������� 422

Data Types ������������������������������������������������������������������������������������������������������������������������������������������ 423

Instruction Syntax������������������������������������������������������������������������������������������������������������������������������� 424

Instruction Set Overview���������������������������������������������������������������������������������������������� 427

AVX512F ��������������������������������������������������������������������������������������������������������������������������������������������� 427

AVX512CD������������������������������������������������������������������������������������������������������������������������������������������� 430

AVX512BW������������������������������������������������������������������������������������������������������������������������������������������ 430

AVX512DQ������������������������������������������������������������������������������������������������������������������������������������������� 431

Opmask Registers ������������������������������������������������������������������������������������������������������������������������������ 431

Summary���������������������������������������������������������������������������������������������������������������������� 432

■Chapter 13: AVX-512 Programming – Floating-Point���������������������������������������� 433

Scalar Floating-Point���������������������������������������������������������������������������������������������������� 433

Merge Masking����������������������������������������������������������������������������������������������������������������������������������� 433

Zero Masking�������������������������������������������������������������������������������������������������������������������������������������� 437

Instruction-Level Rounding����������������������������������������������������������������������������������������������������������������� 440

Packed Floating-Point �������������������������������������������������������������������������������������������������� 444

Packed Floating-Point Arithmetic ������������������������������������������������������������������������������������������������������� 445

Packed Floating-Point Compares ������������������������������������������������������������������������������������������������������� 452

Packed Floating-Point Column Means������������������������������������������������������������������������������������������������ 457

Vector Cross Products ������������������������������������������������������������������������������������������������������������������������ 466

■ Contents

xi

Matrix-Vector Multiplication ��������������������������������������������������������������������������������������������������������������� 476

Convolutions ��������������������������������������������������������������������������������������������������������������������������������������� 485

Summary���������������������������������������������������������������������������������������������������������������������� 489

■Chapter 14: AVX-512 Programming – Packed Integers������������������������������������� 491

Basic Arithmetic ����������������������������������������������������������������������������������������������������������� 491

Image Processing��������������������������������������������������������������������������������������������������������� 497

Pixel Conversions ������������������������������������������������������������������������������������������������������������������������������� 497

Image Thresholding ���������������������������������������������������������������������������������������������������������������������������� 504

Image Statistics���������������������������������������������������������������������������������������������������������������������������������� 510

RGB to Grayscale Conversion ������������������������������������������������������������������������������������������������������������� 520

Summary���������������������������������������������������������������������������������������������������������������������� 527

■Chapter 15: Optimization Strategies and Techniques���������������������������������������� 529

Processor Microarchitecture ���������������������������������������������������������������������������������������� 529

Processor Architecture Overview ������������������������������������������������������������������������������������������������������� 530

Microarchitecture Pipeline Functionality �������������������������������������������������������������������������������������������� 531

Execution Engine �������������������������������������������������������������������������������������������������������������������������������� 532

Optimizing Assembly Language Code �������������������������������������������������������������������������� 534

Basic Techniques�������������������������������������������������������������������������������������������������������������������������������� 534

Floating-Point Arithmetic�������������������������������������������������������������������������������������������������������������������� 536

Program Branches������������������������������������������������������������������������������������������������������������������������������ 536

Data Alignment ����������������������������������������������������������������������������������������������������������������������������������� 538

SIMD Techniques �������������������������������������������������������������������������������������������������������������������������������� 539

Summary���������������������������������������������������������������������������������������������������������������������� 540

■Chapter 16: Advanced Programming ���������������������������������������������������������������� 541

CPUID Instruction ��������������������������������������������������������������������������������������������������������� 541

Non-Temporal Memory Stores ������������������������������������������������������������������������������������� 557

Data Prefetch���������������������������������������������������������������������������������������������������������������� 562

Multiple Threads����������������������������������������������������������������������������������������������������������� 570

Summary���������������������������������������������������������������������������������������������������������������������� 584

■ Contents

xii

■Appendix A �������������������������������������������������������������������������������������������������������� 585

Software Utilities for x86 Processors ��������������������������������������������������������������������������� 585

Visual Studio ���������������������������������������������������������������������������������������������������������������� 585

Running a Source Code Example�������������������������������������������������������������������������������������������������������� 586

Creating a Visual Studio C++ Project ������������������������������������������������������������������������������������������������� 586

References ������������������������������������������������������������������������������������������������������������������� 594

X86 Programming Reference Manuals����������������������������������������������������������������������������������������������� 594

X86 Programming and Microarchitecture References������������������������������������������������������������������������ 595

Ancillary Resources���������������������������������������������������������������������������������������������������������������������������� 596

Algorithm References ������������������������������������������������������������������������������������������������������������������������� 597

C++ References ��������������������������������������������������������������������������������������������������������������������������������� 598

Index��������������������������������������������������������������������������������������������������������������������� 599

xiii

About the Author

Daniel Kusswurm has over 30 years of professional experience as a

software developer and computer scientist. During his career, he has

developed innovative software for medical devices, scientific instruments,

and image processing applications. On many of these projects, he

successfully employed x86 assembly language to significantly improve

the performance of computationally-intense algorithms and solve unique

programming challenges. His educational background includes a BS in

electrical engineering technology from Northern Illinois University along

with an MS and PhD in computer science from DePaul University.

xv

About the Technical Reviewer

Paul Cohen joined Intel Corporation during the very early days of the x86

architecture, starting with the 8086, and retired from Intel after 26 years

in sales/marketing/management. He is currently partnered with Douglas

Technology Group, focusing on the creation of technology books on behalf

of Intel and other corporations. Paul also teaches a class that transforms

middle and high school students into real, confident entrepreneurs, in

conjunction with the Young Entrepreneurs Academy (YEA). He is also a

Traffic Commissioner for the City of Beaverton, Oregon and on the Board

of Directors of multiple non-profit organizations.

xvii

Acknowledgments

The production of a motion picture and the publication of a book are somewhat analogous. Movie trailers

extol the performances of the lead actors. The front cover of a book trumpets the authors’ names. Actors and

authors ultimately receive public acclamation for their efforts. It is, however, impossible to produce a movie

or publish a book without the dedication, expertise, and creativity of a professional behind-the-scenes team.

This book is no exception.

I would like to thank the talented editorial team at Apress for their efforts especially Steve Anglin, Mark

Powers, and Matthew Moodie. Paul Cohen deserves kudos for his meticulous technical review and practical

suggestions. Proofreader Ed Kusswurm merits applause and recognition for his hard work and constructive

feedback. I accept full responsibility for any remaining imperfections.

I would also like to thank Nirmal Selvaraj, Dulcy Nirmala, Kezia Endsley, Dhaneesh Kumar and the

entire production staff at Apress for their contributions, and my professional colleagues for their support and

encouragement. Finally, I would like to recognize parental nodes Armin (RIP) and Mary along with sibling

nodes Mary, Tom, Ed, and John for their inspiration during the writing of this book.

Tải ngay đi em, còn do dự, trời tối mất!