Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Лаб2012 / 25366517.pdf
Скачиваний:
65
Добавлен:
02.02.2015
Размер:
3.33 Mб
Скачать

PROGRAMMING WITH GENERAL-PURPOSE INSTRUCTIONS

CMPXCHG8B instruction can be combined with the LOCK prefix to perform the operation atomically.

7.3.1.3Exchange Instructions in 64-Bit Mode

The CMPXCHG16B instruction is available in 64-bit mode only. It is an extension of the functionality provided by CMPXCHG8B that operates on 128-bits of data.

7.3.1.4Stack Manipulation Instructions

The PUSH, POP, PUSHA (push all registers), and POPA (pop all registers) instructions move data to and from the stack. The PUSH instruction decrements the stack pointer (contained in the ESP register), then copies the source operand to the top of stack (see Figure 7-1). It operates on memory operands, immediate operands, and register operands (including segment registers). The PUSH instruction is commonly used to place parameters on the stack before calling a procedure. It can also be used to reserve space on the stack for temporary variables.

 

 

 

 

 

 

 

Stack

 

 

 

 

Stack

Before Pushing Doubleword

 

After Pushing Doubleword

 

31

0

 

 

 

31

 

 

 

 

Growth

n

 

 

0

 

 

 

 

 

 

 

 

 

ESP

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

n − 4

 

 

 

 

 

Doubleword Value

 

 

 

ESP

 

 

 

 

 

 

 

 

 

 

 

n − 8

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Figure 7-1. Operation of the PUSH Instruction

The PUSHA instruction saves the contents of the eight general-purpose registers on the stack (see Figure 7-2). This instruction simplifies procedure calls by reducing the number of instructions required to save the contents of the general-purpose registers. The registers are pushed on the stack in the following order: EAX, ECX, EDX, EBX, the initial value of ESP before EAX was pushed, EBP, ESI, and EDI.

Vol. 1 7-7

PROGRAMMING WITH GENERAL-PURPOSE INSTRUCTIONS

 

 

 

 

 

Stack

 

 

 

 

Stack

Before Pushing Registers

 

 

 

 

 

After Pushing Registers

 

 

 

 

31

0

31

0

 

 

 

Growth

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

n

 

 

 

 

 

 

 

 

 

 

 

 

n - 4

 

 

 

 

ESP

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

n - 8

 

 

 

 

 

 

EAX

 

 

 

 

 

n - 12

 

 

 

 

 

 

ECX

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

n - 16

 

 

 

 

 

 

EDX

 

 

 

 

 

n - 20

 

 

 

 

 

 

EBX

 

 

 

 

 

n - 24

 

 

 

 

 

 

Old ESP

 

 

 

 

 

n - 28

 

 

 

 

 

 

EBP

 

 

 

 

 

n - 32

 

 

 

 

 

 

ESI

 

 

 

 

 

n - 36

 

 

 

 

 

 

EDI

 

 

 

ESP

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Figure 7-2. Operation of the PUSHA Instruction

The POP instruction copies the word or doubleword at the current top of stack (indicated by the ESP register) to the location specified with the destination operand. It then increments the ESP register to point to the new top of stack (see Figure 7-3). The destination operand may specify a general-purpose register, a segment register, or a memory location.

 

 

 

 

 

 

 

 

 

Stack

 

 

 

 

Stack

 

 

Before Popping Doubleword

 

 

After Popping Doubleword

 

 

 

 

31

0

 

 

31

0

 

 

 

Growth

 

 

 

 

 

n

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

n - 4

 

 

 

 

 

 

 

 

 

 

 

ESP

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

n - 8

 

 

Doubleword Value

 

 

ESP

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Figure 7-3. Operation of the POP Instruction

The POPA instruction reverses the effect of the PUSHA instruction. It pops the top eight words or doublewords from the top of the stack into the general-purpose registers, except for the ESP register (see Figure 7-4). If the operand-size attribute is 32, the doublewords on the stack are transferred to the registers in the following order: EDI, ESI, EBP, ignore doubleword, EBX, EDX, ECX, and EAX. The ESP register is restored by the action of popping the stack. If the operand-size attribute is 16, the words on the stack are transferred to the registers in the following order: DI, SI, BP, ignore word, BX, DX, CX, and AX.

7-8 Vol. 1

PROGRAMMING WITH GENERAL-PURPOSE INSTRUCTIONS

 

 

 

 

 

 

 

 

 

 

Stack

 

 

 

 

 

 

 

 

 

 

Before Popping Registers

 

 

 

 

After Popping Registers

Stack

 

 

 

0

31

 

 

 

0

31

 

 

 

Growth

n

 

 

 

 

 

 

 

 

 

 

 

 

ESP

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

n - 4

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

n - 8

 

 

EAX

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

n - 12

 

 

ECX

 

 

 

 

 

 

 

 

 

 

n - 16

 

 

EDX

 

 

 

 

 

 

 

 

 

 

n - 20

 

EBX

 

 

 

 

 

 

 

 

 

 

n - 24

 

 

Ignored

 

 

 

 

 

 

 

 

 

 

n - 28

 

 

 

EBP

 

 

 

 

 

 

 

 

 

 

 

n - 32

 

ESI

 

 

 

 

 

 

 

 

 

 

 

n - 36

 

EDI

 

 

ESP

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Figure 7-4. Operation of the POPA Instruction

7.3.1.5Stack Manipulation Instructions in 64-Bit Mode

In 64-bit mode, the stack pointer size is 64 bits and cannot be overridden by an instruction prefix. In implicit stack references, address-size overrides are ignored. Pushes and pops of 32-bit values on the stack are not possible in 64-bit mode. 16-bit pushes and pops are supported by using the 66H operand-size prefix. PUSHA, PUSHAD, POPA, and POPAD are not supported.

7.3.1.6Type Conversion Instructions

The type conversion instructions convert bytes into words, words into doublewords, and doublewords into quadwords. These instructions are especially useful for converting integers to larger integer formats, because they perform sign extension (see Figure 7-5).

Two kinds of type conversion instructions are provided: simple conversion and move and convert.

 

15

0

Before Sign

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

S

N

N

N

N

N

N

N

N

N

N

N

N

N

N

N

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Extension

31

15

0

After Sign

S

S

S

S

S

S

S

S

S

S

S

S

S

S

S

S

S

N

N

N

N

N

N

N

N

N

N

N

N

N

N

N

Extension

Figure 7-5. Sign Extension

Vol. 1 7-9

Соседние файлы в папке Лаб2012