x86 Assembly Guide

Source: Internet
Author: User
Tags arithmetic arrays constant

Http://www.cs.virginia.edu/~evans/cs216/guides/x86.html

University of Virginia Computer Science
Cs216:program and Data representation, Spring 2006

2016 July
x86 Assembly Guide

Contents: Registers | Memory and Addressing | Instructions | calling convention

This guide describes the basics of 32-bit x86 assembly language programming, covering a small but useful subset of the AVA Ilable instructions and assembler directives. There is several different assembly languages for generating x86 machine code. The one we'll use on CS216 is the Microsoft Macro Assembler (MASM) assembler. MASM uses the standard Intel syntax for writing x86 assembly code.

The full x86 instruction set is large and complex (Intel's x86 instruction set manuals comprise over 2900 pages), and we D o not cover it all in this guide. For example, there is a 16-bit subset of the x86 instruction set. Using The 16-bit programming model can be quite complex. It has a segmented memory model, more restrictions on register usage, and so on. In this guide, we'll limit our attention to more modern aspects of x86 programming, and delve into the instruction set O Nly in enough detail to get a basic feel for x86 programming.

Resources Guide to Using Assembly in Visual Studio-a tutorial on building and debugging Assembly code in Visual Studio Intel x86 I Nstruction Set Reference Intel ' s Pentium Manuals (the full gory details)

Registers

Modern (i.e 386 and beyond) x86 processors has eight 32-bit general purpose registers, as depicted in Figure 1. The register names is mostly historical. For example, eax used to be called the accumulator since it is used by a number of arithmetic operations, AND&N Bsp Ecx was known as the counter since it is used to hold a loop index. Whereas most of the registers has lost their special purposes in the modern instruction set, by convention, and both are Reser VED for special purposes-the stack pointer (ESP) and the base pointer (EBP).

For the EAX, EBX, ECX, and EDX registers, subsections could be used. For example, the least significant 2 bytes of EAX can be treated as a 16-bit register called AX. The least significant byte of ax can be used as a single 8-bit register called AL, while the most significant byte of Ax C An is used as a single 8-bit register called AH. These names refer to the same physical register. When a two-byte quantity was placed into DX, the update affects the value of DH, DL, and EDX. These sub-registers is mainly hold-overs from older, 16-bit versions of the instruction set. However, they is sometimes convenient when dealing with data that is smaller than 32-bits (e.g. 1-byte ASCII characters) .

When referring to registers in assembly language, the names is not case-sensitive. For example, the names EAX and EAX refer to the same register.


Figure 1. x86 registers Memory and addressing Modes declaring Static Data regions You can declare static data regions (analogous to global variables) in x86 assembly using special assembler directives for This purpose. Data declarations should is preceded by the. DATA directive. Following this directive, the Directives DB, DW, and DD can is used to declare one, both, and four bytes data location S, respectively. Declared locations can be labeled with names for later Reference-this are similar to declaring variables by name, but Abi Des by some lower level rules. For example, locations declared in sequence would be located in memory next to one another.

Example declarations:

. DATA      
var db 64    ; Declare a byte, referred to as Location var, containing the value.
var2 db ? ; Declare a uninitialized byte, referred to as LOCATION VAR2.
  db 10 ; Declare a byte with no label, containing the value 10. Its location is var2 + 1.
X dw ? ; Declare a 2-byte uninitialized value, referred to as location x.
Y dd 30000     ; Declare a 4-byte value, referred to as location y, initialized to 30000.

Unlike in high level languages where arrays can has many dimensions and is accessed by indices, arrays in x86 assembly L Anguage is simply a number of cells located contiguously in memory. An array can is declared by just listing the values and as in the first example below. Common methods used for declaring arrays of data is the DUP directive and the use of string literals. The DUP directive tells the assembler to duplicate an expression a given number of times. For example, 4 DUP (2) are equivalent to 2, 2, 2, 2.

Some Examples:

Z DD 1, 2, 3 ; Declare three 4-byte values, initialized to 1, 2, and 3. The value of location Z + 8 would be 3.
bytes DB ten DUP (?) ; Declare uninitialized bytes starting at location bytes.
Arr DD-DUP (0) ; Declare 4-byte words starting at location arr, all initialized to 0
Str DB ' Hello ', 0 ; Declare 6 bytes Starting at the address str, initialized to the ASCII character values for Hello and the null (0) byte.
Addressing MemoryModern x86-compatible processors is capable of addressing up to 2 bytes of memory:memory addresses is 32-bits wide. In the examples above, where we used labels to refer to memory regions, these labels is actually replaced by the ASSEMBL Er with 32-bit quantities this specify addresses in memory. In addition to supporting referring to memory regions by labels (i.e. constant values), the x86 provides a flexible scheme For computing and referring to memory addresses:up to both of the 32-bit registers and a 32-bit signed constant can be ad Ded together to compute a memory address. One of the registers can is optionally pre-multiplied by 2, 4, or 8.

The addressing modes can be used with many x86 instructions (we'll describe them in the next section). Here we illustrate some examples using the MOV instruction that moves data between registers and memory. This instruction have operands:the first is the destination and the second specifies the source.

Some examples of MOV instructions using address computations are:

mov eax, [ebx] ; Move the 4 bytes in memory @ The address contained in EBX into EAX
mov [var], ebx ; Move the contents of EBX into the 4 bytes @ memory address var. (Note, Var is a 32-bit constant).
mov eax, [esi-4] ; Move 4 bytes at memory address ESI + ( -4) to EAX
mov [ESI+EAX], cl ; Move the contents of CL into the byte at address Esi+eax
mov edx, [ESI+4*EBX] ; Move the 4 bytes of data at address ESI+4*EBX to EDX
Some examples of invalid address calculations include:

mov eax, [ebx-ecx] ; Can only add register values
mov [Eax+esi+edi], ebx ; At more 2 registers in address computation
Size directivesIn general, the intended size of the the the data item at a given memory address can be inferred from the assembly code INS Truction in which it is referenced. For example, with all of the above instructions, the size of the memory regions could is inferred from the size of the Regis ter operand. When we were loading a 32-bit register, the assembler could infer then the region of memory we were referring to is 4 byt Es wide. When we were storing the value of a one byte register to memory, the assembler could infer that we wanted the address to R Efer to a single byte in memory.

However, in some cases the size of a referred-to memory region is ambiguous. Consider the instruction mov [EBX], 2. Should this instruction move the value 2 into the a single byte at address EBX? Perhaps it should move the 32-bit integer representation of 2 into the 4-bytes starting at address EBX. Since either is a valid possible interpretation, the assembler must being explicitly directed as to which are correct. The size Directives BYTE ptr, WORD PTR, and DWORD ptr serve this purpose, indicating sizes of 1, 2, and 4 bytes resp Ectively.

For example:

mov BYTE PTR [ebx], 2 ; Move 2 into the single byte at the address stored in EBX.
mov WORD PTR [ebx], 2 ; Move the 16-bit integer representation of 2 to the 2 bytes starting at the address in EBX.
mov DWORD PTR [ebx], 2 ; Move the 32-bit integer representation of 2 to the 4 bytes starting at the address in EBX.
InstructionsMachine instructions generally fall into three categories:data movement, Arithmetic/logic, and Control-flow. In this section, we'll look at the important examples of x86 instructions from each category. This section should is considered a exhaustive list of x86 instructions, but rather a useful subset. For a complete list, see Intel ' s instruction set reference.

We use the following notation:

<reg32>     any 32-bit register (eax, ebx, ecx,  Edx, esi, edi, esp, OR&NBSP;EBP)
<reg16> any 16-bit register ( AX,&NBSP;BX,&NBSP;CX, OR&NBSP;DX)
<reg8> any 8-bit register (ah, bh,&nbs P CH,&NBSP;DH,&NBSP;AL,&NBSP;BL,&NBSP;CL, OR&NBSP;DL)
<reg> any register
   
<mem> A memory address (e.g.,& nbsp [Eax], [var + 4], or dword ptr [eax+ebx])
<con32> any 32-bit consta NT
<con16> any 16-bit constant
<con8> any 8-bit constant
<con> any 8-, 16-, or 32-bit constant
Data Movement InstructionsMov-move (opcodes:88, 8 A, 8B, 8C, 8E, ...) The MOV instruction copies the data item referred to by its second operand (i.e. register contents, memory contents, or a Constant value) into the "referred to" by its first operand (i.e. a register or memory). While Register-to-register moves is possible, direct memory-to-memory moves is not. In cases where memory transfers is desired, the source memory contents must first be loaded to a register, then can is Stored to the destination memory address.

Syntax
MOV <reg>,<reg>
MOV <reg>,<mem>
MOV <mem>,<reg>
MOV <reg>,<const>
MOV <mem>,<const>

Examples
mov eax, ebx-copy the value in EBX to EAX
mov byte ptr [var], 5-store the value 5 into the-the byte at location var

Push -push Stack (OPCODES:FF, 8 A, 8B, 8C, 8E, ...) The push instruction places its operand onto the top of the hardware supported stack in memory. Specifically, push first decrements ESP by 4, then places it operand into the contents of the 32-bit in address [ESP]. ESP (the stack pointer) is decremented by push since the x86 stack grows down-i.e. The stack grows from high addresses t o Lower addresses.

Syntax
Push <reg32>
Push <mem>
Push <con32>

Examples
Push Eax-push eax on the stack
Push [Var]-push the 4 bytes at address var onto the stack pop -pop stack The pop instruction removes the 4-b Yte data element from the top of the hardware-supported stack into the specified operand (i.e. register or memory location ). It First moves the 4 bytes located at memory location [SP] to the specified register or memory location, and then Incre ments SP by 4.

Syntax
Pop <reg32>
Pop <mem>

Examples
Pop Edi-pop the top element of the stack into EDI.
Pop [Ebx]-pop the top element of the stack into memory at the bytes starting at location EBX. Lea -load effective address the LEA instruction places the address specified by its second operand into the R Egister specified by its first operand. Note, the contents of the memory location was not loaded, only the effective address was computed and placed into the Regi Ster. This was useful for obtaining a pointer to a memory region.

Syntax
Lea <reg32>,<mem>

Examples
Lea EDI, [ebx+4*esi] -the quantity Ebx+4*esi is placed in EDI.
Lea EAX, [var] -the value In var is placed in EAX.
Lea eax, [val] -the Value val is placed in EAX. arithmetic and Logic instructions Add  -integer addition the  Add instruction adds together its-operand

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.