Tutorials to .com

Tutorials to .com » Software » Asm » Windows Assembly Language Programming Tutorial

Windows Assembly Language Programming Tutorial

Print View , by: iSee ,Total views: 47 ,Word Count: 8340 ,Date: Wed, 20 May 2009 Time: 4:06 AM

2005.7.10

Windows Assembly Language Programming Tutorial

Version 1.02

Copyright @ 2005, Jeff Huang.All rights reserved

Translator: fqh 2005.7.10

JEFF HUANG for, fqh translation


Contents

Introduction ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... 2

Why choose assembly language ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... 2

Why did you choose Windows system ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... 2

Ⅰ journey started learning ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... .... ... 3

Compiler ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... 3

Editor ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... 3

Ⅱ first procedure ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... 4

Console application ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... 4

Form procedures ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... 6

ADDR and OFFSET ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... 6

Ⅲ compilation based on ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... 7

cpu register ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... 7

Instruction set based on ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... 8

Push and Pop ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... 8

Invoke ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... 9

Procedures for example ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... 9

IV. Form the basis of the procedures ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... 10

Prior knowledge ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... 10

Macro ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... 10

Process ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... 10

Variable ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... 10

Form a simple procedure ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... 11

IV. Thorough and systematic compilation of ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... 13

String operation ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... 13

Document Management ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... 13

Storage ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... 14

Procedures for example ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... 14

Control ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... 15

Additional Resources ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... 16

Internet ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... 16

Books ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... 16

MASM32 ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... 16

MSDN ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... 16

News Group ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... 16

IRC ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... 16

Referral   Shao


"This is for all you folks out there, who want to learn the magic art of Assembly programming"

-MAD

Introduction

I only recently started to learn assembly language windows system programming, this tutorial is my compilation of language in learning to write down the process. I read a lot of online tutorials, books, as well as through news groups and IRC communication tools to ask the others, I was the adoption of these methods of learning assembly language. The Internet have a lot of compilation of programming tutorials, but the tutorial is only focused on the X86 compilation. Because these materials have assumed that the reader has mastered the high-level programming languages and basic knowledge of computer systems.

Why choose assembly language?

Assembly language with a number of characteristics, making this a case, assembly language is a good choice.

1 rapid assembly language program to run faster than high-level language program to be fast. In general, the requirements to run efficient subroutine is written in assembly language.

2 powerful use of assembly language, you can get unlimited power. On the other hand, high-level language there are various restrictions, in the realization of the request of certain difficult.

3 small assembly language program than the other languages are usually much smaller program. This feature in the case of the limited space available is very useful.

Why did you choose Windows system?

In any operating system and processor mode, can be prepared in the corresponding assembly language procedures. However, at present, the majority of people in the use of x86 processor-based Windows system, running from the preparation of such environment in the beginning of our tutorial program. Once a basic knowledge of assembly language mastered, we will very easy to write in other compilation of procedures under the operating environment.

Chapter

Ⅰ journey started learning

The preparation of the compilation process, we must have some tools, they are the compiler and editor. We have chosen a number of these jobs capable of running on a Windows system tools as follows.

Compiler

Written by the compiler can compile code into machine code. Usually, it comes with a connector. Connector can be used to connect the compiler to generate the file and executable file. Windows system is based on an executable file. Exe as a suffix. The following is a number of popular compilers:

1 MASM this compiler is the chosen course in learning the course of this tutorial, you can use it. It was originally developed by Microsoft, is now included in the package with the MASM32v8. MASM32v8 package also includes other tools. You can get it from this URL: http://www.masm32.com/.

Note: There are a number of tutorial instructions and macros, and only in the MASM compiler is effective, it is strongly recommended that you choose from the beginning when learning MASM.

2. TASM This is another popular compiler. Developed by Borland, and still is, a commercial software, so you can not access it for free.

3. NASM a free open-source compiler, it can also be used on other platforms. It can be access from this website to http://sourceforge.net/projects/nasm/ Remember

Editor

Editor in the preparation of pre-compiled software code. Editor may be made on individual freedom of choice. Now in a very wide range of editor, you can try them and you choose a favorite.

1 Notepad Notepad, Windows system, built-in. While it lacks many features, but it is easy to use.

2 Visual Studio it is not a free editor, but it is excellent syntax highlighting allows you to display the code easier to read.

3. The other - there are many other editors, the more they do not list names. Some of them very popular:

a. Ultraedit (my personal favorite e) http://www.ultraedit.com/

b. Textpad http://www.textpad.com/

c. VIM http://www.vim.org/

d. Emacs http://www.gnu.org/software/emacs/emacs.html

jEdit E. http://www.jedit.org/


Chapter II


Ⅱ first procedure

Now that we have our own tool, open your text editor, then the following introduction, you begin to learn programming. This is one of the most common procedures, "Hello World" program.

Console Application

Console program is running in the system console (also known to all the command-line). For the creation of this procedure, first of all, paste the following code to your text editor, and save it as a document "hello.asm".

.386

. model flat, stdcall

option casemap: none

include \ masm32 \ include \ windows.inc

include \ masm32 \ include \ kernel32.inc

include \ masm32 \ include \ masm32.inc

includelib \ masm32 \ lib \ kernel32.lib

includelib \ masm32 \ lib \ masm32.lib

. data

HelloWorld db "Hello World!", 0

. code

start:

invoke StdOut, addr HelloWorld

invoke ExitProcess, 0

end start

Now, through the Start menu, point "Run ..." option, type "cmd" (without quotation marks) and enter, you can enter to the command line. Cmd then transferred to the preservation of "hello.asm" directory, enter "\ masm32 \ bin \ ml / c / Zd / coff hello.asm". Hope that the compiler will not prompt an error, your program can be compiled correctly! Then, we have to connect it, so then enter "\ masm32 \ bin \ Link / SUBSYSTEM: CONSOLE hello.obj". Congratulations! You have successfully compiled the first assembly language program. Folder appears in a document in Hello.exe. Under the command line in the play "hello" to run your program. It will output "Hello World!". Can be seen, in order to display "Hello World!", We have very little to prepare the code as long as you can.

Since these codes have what effect? Let us line by line to them are listed.

.386

The role of this directive is to tell the compiler use instruction set .386. At present, almost no processor is more than .386 to make the old set of specified. We also have the option to use .486. Or 586, but .386 is the best instruction set compatibility.


. model flat, stdcall

. MODEL is a designated program memory mode of your compilation command. Flat is a convenient model system procedures, because in this mode is no longer differentiate between far pointer (far) and near-pointer (far). Stdcall system function is a way to pass parameters, it means you can be right-to-left order of the parameters you pass.

option casemap: none

Mandatory code of your case-sensitive, which means Hello and hello are seen as different. Similarly, many high-level programming languages are case-sensitive, so this is a good programming habits.

include \ masm32 \ include \ windows.inc

include \ masm32 \ include \ kernel32.inc

include \ masm32 \ include \ masm32.inc

This is a system file that contains the necessary procedures. windows.inc usually must be included because it contains the Win32 API constants and the definition of the statement. kernel32.inc we use contains the ExitProcess function. masm32.inc contains StdOut function. Win32 function is not StdOut function, it is added into MASM32v8.

includelib \ masm32 \ lib \ kernel32.lib

includelib \ masm32 \ lib \ masm32.lib

Function dependent on the Treasury, based on this end, the library was to include.

. data

Initialization procedure for all the data must be placed on the following instructions. In addition, there are other commands such as. Data? And. Const. They are not initialized at the front of the data and constants, but, in our "Hello World" program did not use them.

HelloWorld db "Hello World!", 0

db on behalf of the "byte" and a statement as a string HelloWorld. "Hello World!" Followed by a "NULL" letters ANSI This is because at the end of the string must be NULL.

. code

This represents the beginning of the procedure code.

start:

Your code is located in the back of the label, but at "end start" in front.

invoke StdOut, addr HelloWorld

Invoke to call a function and its parameters, addr HelloWorld located behind it. The transmission line is done by "Hello World!" The address and call StdOut. Note StdOut function effectively only in the MASM32, it is a call the other function to output file macros. In other compilers, you have to use more and use the code to the win32 function WriteConsole..

invoke ExitProcess, 0

It is obvious that it is passing the parameters 0 to ExitProcess function, and thus withdraw from the process.
Form procedures

We can also prepare a form version of "Hello World" program. Paste the following text into your file editor and save it as a document in "hellow.asm".

.386

. model flat, stdcall

option casemap: none

include \ masm32 \ include \ windows.inc

include \ masm32 \ include \ kernel32.inc

include \ masm32 \ include \ user32.inc

includelib \ masm32 \ lib \ kernel32.lib

includelib \ masm32 \ lib \ user32.lib

. data

HelloWorld db "Hello World!", 0

. code

start:

invoke MessageBox, NULL, addr HelloWorld, addr HelloWorld, MB_OK

invoke ExitProcess, 0

end start

Now, re-open the command line and go to "hellow.asm" directory is located. Enter the "\ masm32 \ bin \ ml / C++ / Zd / coff hellow.asm" Enter, then enter "\ masm32 \ bin \ Link / SUBSYSTEM: WINDOWS hellow.obj" and enter. Note, subsystem is WINDOWS is no longer a CONSOLE. This procedure shows up a "Hello World!" Message box.

Compared with the console version, version of the code form only 3 lines are different. Among them, two lines of the masm32 library file contains the files and replace the user32 library contains documents and files, this is because we are using the MessageBox function, rather than using the StdOut. No. 3 line is a different function instead of using MessageBox function StdOut. Differences only on so much!

ADDR and OFFSET

In our "Hello World!" Example program, we use the''''addr''''to get the string "Hello World!" Address. There is another command similar to the''''offset'''', Although the purpose of the two procedures were implemented to obtain the address change is variable. Their main difference is that''''offset''''can only get the address of global variables, however, to obtain the overall change addr and address local variables. However, we do not discuss local variables, so we do not worry about this distinction. However, we still have to remember this distinction.

Chapter III

Compilation based on

cpu registers

Now we have been able to prepare and run a simple procedure. Let us go to the core content of this tutorial - compilation of basic grammar right. You have to write their own compilation of procedures, such basic knowledge is available. There are 32 general-purpose register 8. Four of them in front of them that is, eax, ebx, ecx, edx, also use their 16-bit or 8-bit access to the names of the form. For example, ax access eax low 16, al access to low-8, as well as access ah is 9-16. The remaining registers will also be able to access a similar manner. As we imagine it, although most of these general-purpose registers have specific purposes, but they are common place.

Address

Name

Description

EAX *

Cumulative register

Calculated results of the operation and preservation of data

EBX

Base address register

Data point to data register

ECX *

Count Register

String as well as the cycle count

EDX *

Data register

Input / output indicators

ESI

Source Index Register

Operation of the source string pointer

EDI

The purpose of changing the site register

The purpose of the string pointer operation

ESP

Stack pointer register

Stack pointer can not use it man-made

EBP

Stack base address register

The data point to the stack

Note: Although they are known as general-purpose register, but only those marked with an asterisk * in the form can be used in programming.


Here are six 16-bit register of the paragraph. They are defined in the memory segment.

Address

Name

Description

CS

Code segment register

Save To run the command

DS, ES, FS, GS

Data segment register

Data segment

SS

Stack Segment Register

The current process stack

Finally, there are two 32-bit register is not classified

Address

Name

Description

EFLAGE

Logo register

State, control, system logo

EIP

Instruction pointer register

A pointer to the implementation of the offset


Instruction set based on

x86 instruction set is very ambitious, but we do not usually use them all. Here are some instructions that we should grasp.

Directive

Description

ADD * reg / memory, reg / memory / constant

The sum of the two operands and the results saved into the first operand. If there is binary, it will set up a CF flag

SUB * reg / memory, reg / memory / constant

The first operand minus the second operand, and the results saved to the first operand in

AND * reg / memory, reg / memory / constant

Logic and the two operands and deposit the results of the first operand in

OR * reg / memory, reg / memory / constant

Logic or two operands and deposit the results of the first operand in

XOR * reg / memory, reg / memory / constant

XOR the two, and keep the results to the first operation a few years. Note you can not

For two different memory operand or operation

MUL reg / memory

Operand multiply and accumulator register, and then deposit the results into the accumulator storage

Browser

DIV reg / memory

Operand register by accumulator and the results kept in addition to the accumulator

INC reg / memory

By the value of operand 1 and the results deposited operand

DEC reg / memory

By the value of operand 1 and the results deposited operand

NEG reg / memory

Operand values and the results take up deposited operand

NOT reg / memory

Operand values and the results from the anti-deposited operand

PUSH reg / memory / constant

The operand stack pressure into the top

POP reg / memory

Pop the top stack value and save to the operand