Jo Van Hoey

Beginning x64 Assembly Programming

From Novice to AVX Professional

Jo Van Hoey
Hamme, Belgium
ISBN 978-1-4842-5075-4e-ISBN 978-1-4842-5076-1
© Jo Van Hoey 2019
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.
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.
Introduction

Learning to program in assembly can be frustrating, not in the least because it is an unforgiving language; the computer will “yell” at you on every possible occasion. And if it doesn’t, you may just have unknowingly introduced a hidden bug that will bite you later in your program or at execution time. On top of that, the learning curve is steep, the language is cryptic, the official Intel documentation is overwhelming, and the available development tools have their own peculiarities.

In this book, you will learn to program in assembly starting with simple programs and moving all the way up to Advanced Vector Extensions (AVX) programming. By the end of this book, you will be able to write and read assembly code, mix assembly with higher-generation languages, understand what AVX is, and more. The purpose of this book is to show you how to use assembly language instructions. This book is not about programming style or code performance optimization. After you have acquired basic knowledge of assembly, you can continue learning how to optimize your code. This book should not be the first book you read on programming; if you have never programmed before, put this book aside for a while and learn some basics of programming with a higher-level language such as C.

All the code used in this book can be accessed via the Download Source Code link at www.apress.com/9781484250754 . The code used in this book is kept as simple as possible, which means no graphical user interfaces or bells and whistles or error checking. Adding all these nice features would deviate our attention from the purpose: to learn assembly language.

The theory is kept to a strict minimum: a little bit on binary numbers, a short presentation of logical operators, and some limited linear algebra. And we stay far away from doing floating-point conversions. If you need to convert binary or hexadecimal numbers, find a web site that does that for you. Don’t waste your time doing the calculations manually. Stick to the purpose: learning assembly.

The assembly code is presented in complete programs so that you can test them on your computer, play with them, change them, break them....

We will also show you what tools can be used, how to use them, and the potential problems in those tools. Having the right tools is essential to overcoming the steep learning curve. At times we will point you to books, white papers, and web sites that can be useful or that give more details.

It is not our intention to give you a comprehensive course on all of the assembly instructions. That is impossible in one book (look at the size of the Intel manuals!). We will give you a taste of the main items so that you will have an idea about what is going on. If you work through this book, you will acquire the knowledge to investigate certain domains in more detail on your own. When you have finished this book, you will be able to study the Intel manuals and (try to) make sense of their content.

The majority of the book is dedicated to assembly on Linux, because it is the easiest platform to learn assembly language. At the end, we provide a number of chapters to get you on your way with assembly on Windows. You will see that once you have Linux assembly under your belt, it is much easier to take on Windows assembly.

There are a number of assemblers available for use with Intel processors, such as FASM, MASM, GAS, NASM, and YASM to name a few. We will use NASM as in this book, because it is multiplatform; it is available on Linux, Windows, and macOS. Also, it has a relatively large user base. But don’t worry, once you know one assembler, it will be easy to adopt another assembly’s “dialect.”

We have carefully written and tested the code used in this book. However, if there are any typos in the text or bugs in the programs, we do not take any responsibility. We blame them on our two cats, who love to walk over our keyboard while we are typing.

The ideas and opinions we present in this book are our own and don’t necessarily represent IBM’s positions, strategies, or opinions.

Before You Start
You should know some basic things before you start reading this book.
  • You should know how to install and manage virtualization software (VMware, VirtualBox, or similar) . If you don’t have a clue what that means, download the free Oracle VirtualBox software ( https://www.virtualbox.org ), install it, and learn to use it by installing, for example, Ubuntu Desktop Linux as a guest operating system (OS). Virtualization software allows you to install different guest operating systems on your main machine, and if you mess things up in the guest system, you can delete that guest system and reinstall it. Or if you have taken a snapshot, you can return to a previous version of your guest installation. In other words, there’s no harm to your main (host) operating system when experimenting. There are plenty of resources on the Internet explaining VirtualBox and other virtualization software solutions.

  • You should have basic knowledge of the Linux command-line interface (CLI) . We will be using Ubuntu Desktop here, and we will use the CLI all the time, starting in Chapter 1 . You can use another Linux distribution if you want, but make sure you can install the tools used in the book (NASM, GCC , GDB , SASM, and so on). The following is the basic knowledge you need: how to install the OS, how to install additional software, how to start a terminal with a command prompt, and how to create, move, copy, and delete directories and files at the CLI. You also need to know how to use the tar utility, grep , find , ls , time , and so on. You need to know how to start and use a text editor. No advanced Linux knowledge is required; you need only a basic knowledge of these tasks to follow the explanations in this book. If you do not know Linux, play around with it to get used to it. There are plenty of good, short, beginning tutorials available on the Internet (e.g. https://www.guru99.com/unix-linux-tutorial.html ). You will see that after you learned assembly on a Linux machine, learning assembly on another OS is not that difficult.

  • You should have some basic knowledge of the C programming language . We will use a couple of C functions to simplify the example assembly code. Also, we will show how to interface with a higher-level language such as C. If you do not know C and want to fully enjoy this book, take a couple of free introductory C courses at, for example, tutorialspoint.com . There’s no need to do the whole course; just take a look at a few programs in the language. You can always return later to find out more details.

Why Learn Assembly?

Learning assembly has several benefits.
  • You’ll learn how a CPU and memory works.

  • You’ll learn how a computer and operating system work together.

  • You’ll learn how high-level language compilers generate machine language, and that knowledge can help you to write more efficient code.

  • You will be better equipped to analyze bugs in your programs.

  • It is a lot of fun when you eventually get your program working.

  • And the reason I wrote this book: if you want to investigate malware, you have only the machine code, not the source code. With an understanding of assembly language, you will be able to analyze malware and take necessary actions and precautions.

The Intel Manuals

The Intel manuals contain everything you ever wanted to know about programming Intel processors. However, the information is hard to swallow for a beginner. When you are progressing in this book, you will see that the explanations in these Intel manuals will make gradually more sense to you. We will refer often to these massive volumes of information.

You can find the Intel manuals here:

https://software.intel.com/en-us/articles/intel-sdm

Just don’t print them—think about all the trees you would be destroying! Take a short look at the manuals to see how comprehensive, detailed, and formal they are. Learning assembly from these manuals would be very daunting. Of special interest to us will be Volume 2, where you will find detailed explanations about the assembly programming instructions.

You will find a useful source here: https://www.felixcloutier.com/x86/index.html . This site provides a list of all the instructions with a summary of how to use them. If the information provided here is not sufficient, you can always go back to the Intel manuals or your friend Google.

Table of Contents

Index 407

About the Author and About the Technical Reviewer

About the Author

Jo Van Hoey
../images/483996_1_En_BookFrontmatter_Figb_HTML.jpg

has 40 years of experience in IT, consisting of diverse functions, multiple IT companies, and various computing platforms. He recently retired from IBM, where he was a mainframe software account manager. He has always been interested in IT security, and knowledge of assembly language is an essential skill in defending IT infrastructure against attacks and malware.

 

About the Technical Reviewer

Paul Cohen
../images/483996_1_En_BookFrontmatter_Figc_HTML.jpg

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), and is a traffic commissioner for the City of Beaverton, Oregon, and on the board of directors of multiple nonprofit organizations.

 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.78.237