Docsity

Assembly Language Succinctly, Notas de estudo de Matemática

Pré-visualização parcial do texto

Baixe Assembly Language Succinctly e outras Notas de estudo em PDF para Matemática, somente na Docsity!

By

Christopher Rose

Foreword by Daniel Jebaraj

Table of Contents

The Story behind the Succinctly Series of Books

Daniel Jebaraj, Vice President Syncfusion, Inc. taying on the cutting edge

As many of you may know, Syncfusion is a provider of software components for

the Microsoft platform. This puts us in the exciting but challenging position of

always being on the cutting edge.

Whenever platforms or tools are shipping out of Microsoft, which seems to be about every

other week these days, we have to educate ourselves, quickly.

Information is plentiful but harder to digest

In reality, this translates into a lot of book orders, blog searches, and Twitter scans.

While more information is becoming available on the Internet and more and more books are

being published, even on topics that are relatively new, one aspect that continues to inhibit

us is the inability to find concise technology overview books.

We are usually faced with two options: read several 500+ page books or scour the web for

relevant blog posts and other articles. Just as everyone else who has a job to do and

customers to serve, we find this quite frustrating.

The Succinctly series

This frustration translated into a deep desire to produce a series of concise technical books

that would be targeted at developers working on the Microsoft platform.

We firmly believe, given the background knowledge such developers have, that most topics

can be translated into books that are between 50 and 100 pages.

This is exactly what we resolved to accomplish with the Succinctly series. Isn’t everything

wonderful born out of a deep desire to change things for the better?

The best authors, the best content

Each author was carefully chosen from a pool of talented experts who shared our vision. The

book you now hold in your hands, and the others available in this series, are a result of the

authors’ tireless work. You will find original content that is guaranteed to get you up and

running in about the time it takes to drink a few cups of coffee.

Free forever

Syncfusion will be working to produce books on several topics. The books will always be

free. Any updates we publish will also be free.

S

Free? What is the catch?

There is no catch here. Syncfusion has a vested interest in this effort.

As a component vendor, our unique claim has always been that we offer deeper and broader

frameworks than anyone else on the market. Developer education greatly helps us market

and sell against competing vendors who promise to “enable AJAX support with one click,” or

“turn the moon to cheese!”

Let us know what you think

If you have any topics of interest, thoughts, or feedback, please feel free to send them to us

at [email protected].

We sincerely hope you enjoy reading this book and that it helps you better understand the

topic of study. Thank you for reading.

Please follow us on Twitter and “Like” us on Facebook to help us spread the

word about the Succinctly series!

Introduction

Assembly Language

This book is an introduction to x64 assembly language. This is the language used by almost

all modern desktop and laptop computers. x64 is a generic term for the newest generation of

the x86 CPU used by AMD, Intel, VIA, and other CPU manufacturers. x64 assembly has a

steep learning curve and very few concepts from high-level languages are applicable. It is

the most powerful language available to x64 CPU programmers, but it is not often the most

practical language.

An assembly language is the language of a CPU, but the numbers of the machine code are

replaced by easy-to-remember mnemonics. Instead of programming using pure

hexadecimal, such as 83 C4 04 , programmers can use something easier to remember and

read, such as ADD ESP, 4 , which adds 4 to ESP. The human readable version is read by a

program called an assembler, and then it is translated into machine code by a process called

assembling (analogous to compiling in high-level languages). A modern assembly language

is the result of both the physical CPU and the assembler. Modern assembly languages also

have high-level features such as macros and user-defined data types.

Why Learn Assembly?

Many high-level languages (Java, C#, Python, etc.) share common characteristics. If a

programmer is familiar with any one of them, then he or she will have no trouble picking up

one of the others after a few weeks of study. Assembly language is very different; it shares

almost nothing with high-level languages. Assembly languages for different CPU

architectures often have little in common. For instance, the MIPS R4400 assembly language

is very different from the x86 language. There are no compound statements. There are no if

statements, and the goto instruction ( JMP ) is used all the time. There are no objects, and

there is no type safety. Programmers have to build their own looping structures, and there is

no difference between a float and an int. There is nothing to assist programmers in

preventing logical errors, and there is no difference between execute instructions and data.

There are many differences between assembly languages.

I could go on forever listing the useful features that x64 assembly language is missing when

compared to high-level languages, but in a sense, this means that assembly language has

fewer obstacles. Type safety, predefined calling conventions, and separating code from data

are all restrictions. These restrictions do not exist in assembly; the only restrictions are those

imposed by the hardware itself. If the machine is capable of doing something, it can be told

to do so using its own assembly language.

A French person might know English as their second language and they could be instructed

to do a task in English, but if the task is too complicated, some concepts may be lost in

translation. The best way to explain how to perform a complex task to a French person is to

explain it in French. Likewise, C++ and other high-level languages are not the CPU's native

language. The computer is very good at taking instructions in C++, but when you need to

explain exactly how to do something very complicated, the CPU's native language is the only

option.

Another important reason to learn an assembly language is simply to understand the CPU. A

CPU is not distinct from its assembly language. The language is etched into the silicon of the

CPU itself.

Intended Audience

This book is aimed at developers using Microsoft's Visual Studio. This is a versatile and very

powerful assembly language IDE. This book is targeted at programmers with a good

foundation in C++ and a desire to program native assembly using the Visual Studio IDE

(professional versions and the express editions). The examples have been tested using

Visual Studio and the assembler that comes bundled with it, ML64.exe (the 64-bit version of

MASM, Microsoft's Macro Assembler).

Having knowledge of assembly language programming also helps programmers understand

high-level languages like Java and C#. These languages are compiled to virtual machine

code (Java Byte Code for Java and CIL or Common Intermediate Language for .NET

languages). The virtual machine code can be disassembled and examined from .NET

executables or DLL files using the ILDasm.exe tool, which comes with Visual Studio. When a

.NET application is executed by another tool, ILAsm.exe, it translates the CIL machine code

into native x86 machine code, which is then executed by the CPU. CIL is similar to an

assembly language, and a thorough knowledge of x86 assembly makes most of CIL

readable, even though they are different languages. This book is focused on C++, but this

information is similarly applicable to programming high-level languages.

This book is about the assembly language of most desktop and laptop PCs. Almost all

modern desktop PCs have a 64-bit CPU based on the x86 architecture. The legacy 32-bit

and 16-bit CPUs and their assembly languages will not be covered in any great detail.

MASM uses Intel syntax, and the code in this book is not compatible with AT&T assemblers.

Most of the instructions are the same in other popular Intel syntax assemblers, such as

YASM and NASM, but the directive syntax for each assembler is different.

There are several benefits to using inline assembly instead of a native 32-bit assembly file.

Passing parameters to procedures is handled entirely by the C++ compiler, and the

programmer can refer to local and global variables by name. In native assembly, the stack

must be manipulated manually. Parameters passed to procedures, as well as local variables,

must be referred to as offsets from the RSP (stack pointer) or the RBP (base pointer). This

requires some background knowledge.

There is absolutely no overhead for using inline assembly. The C++ compiler will inject the

exact machine code the inline assembly generates into the machine code it is generating

from the C++ source. Some things are simply easier to describe in assembly, and it is

sometimes not convenient to add an entire native assembly file to a project.

Another benefit of inline assembly is that it uses the same commenting syntax as C++ since

we have not actually left the C++ code file. Not having to add separate assembly source

code files to a project may make navigating the project easier and enable better

maintainability.

The downside to using inline assembly is that programmers lose some of the control they

would have otherwise. They lose the ability to manually manipulate the stack and define their

own calling convention, as well as the ability to describe segments in detail. The most

important compromise is in Visual Studio’s lack of support for x64 inline assembly. Visual

Studio does not support inline assembly for 64-bit applications, so any programs with inline

assembly will already be obsolete because they are confined to the legacy 32-bit x86. This

may not be a problem, since applications that require the larger addressing space and

registers provided by x64 are rare.

Native Assembly Files in C++

Inline assembly offers a good deal of flexibility, but there are some things that programmers

cannot access with inline assembly. For this reason, it is common to add a separate, native

assembly code file to your project.

Visual Studio Professional installs all the components to easily change a project's target

CPU from 32-bit to 64-bit, but the express versions of Visual C++ require the additional

installation of the Windows 7 SDK.

Note: If you are using Visual C++ Express, download and install the latest Windows 7 SDK (version 7.1 or higher for .NET 4).

You will now go through a guide on how to add a native assembly to a simple C++ project.

1. Create a new Empty C++ project. I have created an empty project for this example,

but adding assembly files to Windows applications is the same.

2. Add a C++ file to your project called main.cpp. As mentioned previously, this book is

not about making entire applications in assembly. For this reason, we shall make a

basic C++ front end that calls upon assembly whenever it requires more

performance.

3. Right-click on your project name in the Solution Explorer and choose Build

Customizations.... The build customizations are important because they contain the

rules for how Visual Studio deals with assembly files. We do not want the C++

compiler to compile .asm files, we wish for Visual Studio to give these files to MASM

for assembling. MASM assembles the .asm files, and they are linked with the C++

files after compilation to form the final executable.

Figure 1

4. Select the box named masm (.targets, .props). It is important to do this step prior to

actually adding an assembly code file, because Visual Studio assigns what is to be

done with a file when the file is created, not when the project is built.

Figure 2

5. Add another C++ code file, this time with an .asm extension. I have used

asmfunctions.asm for my second file name in the sample code). The file name can

be anything other than the name you selected for your main program file. Do not

name your assembly file main.asm because the compiler may have trouble

identifying where your main method is.

cout<<"Smallest is "<<FindSmallest(arr, 10)<<endl;

cin.get();

return 0;

}

; asmfunctions.asm

.xmm

.model flat, c

.data

.code

FindSmallest proc export

mov edx, dword ptr [esp+4] ; edx = *int mov ecx, dword ptr [esp+8] ; ecx = Count

mov eax, 7fffffffh ; eax will be our answer

cmp ecx, 0 ; Are there 0 items? jle Finished ; If so we're done

MainLoop:

cmp dword ptr [edx], eax ; Is *edx < eax? cmovl eax, dword ptr [edx] ; If so, eax = edx

add edx, 4 ; Move *edx to next int

dec ecx ; Decrement counter jnz MainLoop ; Loop if there's more

Finished:

ret ; Return with lowest in eax

FindSmallest endp

end

Additional Steps for x

Visual Studio 2010, 2012, and 2013 Professional come with all the tools needed to quickly

add native assembly code files to your C++ projects. These steps provide one method of

adding native assembly code to a C++ project. The screenshots are taken from Visual

Studio 2010, but 2012 is almost identical in these aspects. Steps one through six for creating

this project are identical to those described for 32-bit applications. After you have completed

these steps, the project must be changed to compile for the x64 architecture.

7. Open the Build menu and select Configuration Manager.

Figure 4

8. In the configuration manager window, select <New...> from the Platform column.