Faculty of Electronic Engineering – Dept. of Computer Science

Download Report

Transcript Faculty of Electronic Engineering – Dept. of Computer Science

Microprocessors

Chapter 4

Data Movement Instructions

prepared by Dr. Mohamed A. Shohla

Chapter Overview

• • • •

MOV Revisited PUSH/POP String Data Transfers Miscellaneous Data Transfer Instructions ةي فون ملا ةع ماج Faculty of Electronic Engineering – Dept. of Computer Science & Eng.

Microprocessors Course 4 - 2

ةي فون ملا ةع ماج

PUSH

• The 8086-80286 PUSH instruction always transfers two bytes of data to the stack; the 80386 and above transfer two or four bytes, depending on the register or size of the memory location. •

Examples of the PUSH instructions.

Symbolic

PUSH reg16 PUSH reg32 PUSH mem16 PUSH seg PUSH imm8 PUSHW imm16 PUSHD imm32 PUSHA PUSHAD PUSHF PUSHFD PUSH BX

Example

PUSH EDX PUSH WORD PTR [BX] PUSH DS PUSH ‘,’ PUSHW 1000H PUSHD 20 PUSHA PUSHAD PUSHF PUSHFD

Note

16-bit register 32-bit register 16-bit pointer Segment register 8-bit immediate 16-bit immediate 32-bit immediate Save all 16-bit registers Save all 32-bit registers Save flags Save EFLAGs

Faculty of Electronic Engineering – Dept. of Computer Science & Eng.

Microprocessors Course 4 - 3

PUSH Example : PUSH AX

ةي فون ملا ةع ماج

The effect of the PUSH AX instruction on ESP and stack memory location 37FFH and 37FEH. This instruction is shown after execution.

Faculty of Electronic Engineering – Dept. of Computer Science & Eng.

Microprocessors Course 4 - 4

PUSH Example : PUSHA

ةي فون ملا ةع ماج

The operation of the PUSHA instruction, showing the location and order of stack data.

Faculty of Electronic Engineering – Dept. of Computer Science & Eng.

Microprocessors Course 4 - 5

ةي فون ملا ةع ماج

POP

The POP instruction performs the inverse operation of a PUSH instruction. The POP instruction removes data from the stack and places it into the target 16-bit register, segment register, or a 16-bit memory location. In the 80386 and above, a POP can also remove 32-bit data from the stack and use a 32-bit address. •

Examples of the POP instructions.

Symbolic

POP reg16 POP reg32 POP mem16 POP mem32 POP seg POPA POPAD POPF POPFD

Example

POP CX POP EBP POP WORD PTR[BX+1] POP DATA3 POP FS POPA POPAD POPF POPFD

Note

16-bit register 32-bit register 16-bit pointer 32-bit memory address Segment register Pop all 16-bit registers Pop all 32-bit registers Pop flags Pop EFLAGs

Faculty of Electronic Engineering – Dept. of Computer Science & Eng.

Microprocessors Course 4 - 6

POP Example : POP BX

ةي فون ملا ةع ماج

The POP BX instruction, showing how data are removed from the stack. This instruction is shown after execution.

Faculty of Electronic Engineering – Dept. of Computer Science & Eng.

Microprocessors Course 4 - 7

ةي فون ملا ةع ماج

LEA

• The LEA instruction loads a 16- or 32-bit register with the offset address of the data specified by the operand.

• By comparing LEA with MOV, it is observed that LEA BX,[DI] loads the offset address specified by [DI] (contents of DI) into the BX register; MOV BX,[DI] loads the data stored at the memory location addressed by [DI] into register BX.

• For example, the MOV BX,OFFSET LIST performs the same function as LEA BX,LIST. Both instructions load the offset address of memory location LIST into the BX register.

Faculty of Electronic Engineering – Dept. of Computer Science & Eng.

Microprocessors Course 4 - 8

Examples of Load-effective address instructions.

ةي فون ملا ةع ماج

Assembly Language

LEA AX, NUMB LEA EAX, NUMB LDS DI, LIST LDS EDI, LIST LES BX, CAT LFS DI, DATA1 LGS SI, DATA5 LSS SP, MEM

Operation

Loads AX with the address of NUMB Loads EAX with the address of NUMB Loads DS and Dl with the 32-bit contents of data segment memory location LIST Loads DS and EDI with the 48-bit contents of data segment memory location LIST Loads ES and BX with the 32-bit contents of data segment memory location CAT Loads FS and Dl with the 32-bit contents of data segment memory location DATA1 Loads GS and SI with the 32-bit contents of data segment memory location DATA5 Loads SS and SP with the 32-bit contents of memory location MEM

Faculty of Electronic Engineering – Dept. of Computer Science & Eng.

Microprocessors Course 4 - 9

Example: A short program that loads SI with the address of DATA1 and DI with the address of DATA2. Il then exchanges the contents of these memory locations. Note that the LEA and MOV with OFFSET instructions are both the same length (three bytes).

.MODEL SMALL .DATA

DATA1 DW DATA2 DW 2000H 3000H .CODE

.STARTUP

LEA SI, DATA1 ; select SMALL model ; start of DATA segment ; define DATA1 ; define DATA2 ; start of CODE segment ; start of program ; address DATA1 with SI MOV DI, OFFSET DATA2 ;address DATA2 with DI MOV BX, [SI] ; exchange DATA1 with DATA2 MOV CX, [DI] MOV [SI], CX MOV [DI], BX .EXIT

END ; exit to DOS ; end of file

Faculty of Electronic Engineering – Dept. of Computer Science & Eng.

Microprocessors Course 4 - 10 ةي فون ملا ةع ماج

LDS, LES, LFS, LGS, and LSS

• The LDS, LES, LFS, LGS, and LSS instructions load any 16-bit or 32-bit register with an offset address, and the DS, ES, FS, GS, or SS segment register with a segment address.

• These instructions use any of the memory-addressing modes to access a 32-bit or 48-bit section of memory that contains both the segment and offset address. • The 32-bit section of memory contains a 16-bit offset and segment address, while the 48-bit section contains a 32-bit offset and a segment address.

ةي فون ملا ةع ماج Faculty of Electronic Engineering – Dept. of Computer Science & Eng.

Microprocessors Course 4 - 11

Example: LDS BX, [DI]

ةي فون ملا ةع ماج

• The LDS BX, [DI] instruction loads register BX from addresses 11000H and 11001H and register DS from locations 11002H and 11003H. This instruction is shown at the point just before DS changes to 3000H and BX changes to 127AH.

Faculty of Electronic Engineering – Dept. of Computer Science & Eng.

Microprocessors Course 4 - 12

String Data Transfers

• There are five string data transfer instructions: LODS, STOS, MOVS, INS, and OUTS. Each string instruction allows data transfers that are either a single byte, word, or doubleword (or if repeated, a block of bytes, words, or doublewords). • Before the string instructions are presented, the operation of the D flag-bit (direction), DI, and SI must be understood as they apply to the string instructions.

ةي فون ملا ةع ماج Faculty of Electronic Engineering – Dept. of Computer Science & Eng.

Microprocessors Course 4 - 13

ةي فون ملا ةع ماج

The Direction Flag

• The direction flag (D) (located in the flag register) selects the auto-increment (D = 0) or the auto-decrement (D = 1) operation for the DI and SI registers during string operations. • The direction flag is used only with the string instructions. The CLD instruction clears the D flag (D = 0) and the STD instruction sets it (D = 1). Therefore, the CLD instruction selects the auto increment mode (D = 0) and STD selects the auto-decrement mode (D = 1).

• Whenever a string instruction transfers a byte, the contents of DI and/or SI increment or decrement by 1. If a word is transferred, the contents of DI and/or SI increment or decrement by 2. Doubleword transfers cause DI and/or SI to increment or decrement by 4.

Faculty of Electronic Engineering – Dept. of Computer Science & Eng.

Microprocessors Course 4 - 14

LODS

• • • The LODS instruction loads AL, AX, or EAX with data stored at the data segment offset address indexed by the SI register. After loading AL with a byte, AX with a word, or EAX with a doubleword, the contents of SI increment, if D = 0 or decrement, if D = 1. Examples of the LODS instructions.

Assembly Language

LODSB LODSW LODSD LODS LIST LODS DATA1 LODS FROG

Operation

AL = DS:[SI]; SI = SI ± 1 AX = DS:[SI];SI = SI ± EAX = DS:[SI];SI = SI ± 2 4 AL = DS:[SIJ; SI = SI ± 1 (if LIST is a byte) AX = DS:[SI], SI = SI ± 2 (if DATA1 is a word) EAX = DS:[SI]; SI = SI ± 4 (FROG is a doubleword)

ةي فون ملا ةع ماج Faculty of Electronic Engineering – Dept. of Computer Science & Eng.

Microprocessors Course 4 - 15

LODS Example : LODSW

ةي فون ملا ةع ماج

The operation of the LODSW instruction if DS = 1000H, D = 0,11000H = 32, and 11001H = A0. This instruction is shown after AX is loaded from memory, but before SI increments by 2.

Faculty of Electronic Engineering – Dept. of Computer Science & Eng.

Microprocessors Course 4 - 16

STOS

ةي فون ملا ةع ماج

• • • The STOS instruction stores AL, AX, or EAX at the extra segment memory location addressed by the DI register. The STOSB (stores a byte) instruction stores the byte in AL at the extra segment memory location addressed by DI.

Examples of the STOS instructions.

Assembly Language

STOSB STOSW STOSD STOS LIST STOS DATA3 STOS DATA4

Operation

ES:[DI] = AL; Dl = Dl ± 1 ES:[DI] = AX; Dl = Dl ± 2 ES:[DI] = EAX; Dl = Dl ± 4 ES:[DI] = AL; Dl = Dl ± 1 (list is a byte) ES:[DI] = AX; Dl = Dl ± 2 (DATA3 is a word) ES:[DI] = EAX; Dl = Dl ± 4 (DATA4 is a doubleword)

Faculty of Electronic Engineering – Dept. of Computer Science & Eng.

Microprocessors Course 4 - 17

• •

MOVS

• The MOVS instruction transfers a byte, word, or doubleword from the data segment location addressed by SI to the extra segment location addressed by DI. As with the other string instructions, the pointers then increment or decrement, as dictated by the direction flag.

Examples of the MOVS instructions.

ةي فون ملا ةع ماج

Assembly Language

MOVSB MOVSW MOVSD MOVS BYTE1,BYTE2

Operation

ES:[DI] = DS:[SI]; DI = DI ± 1; SI = SI ± 1 (byte transferred) ES:[DI] = DS:[SI];D! = DI ± transferred) ES:[DI] = DS:[SI]; Dl = DI ± 2; SI = SI ± 2 (word 4; SI = SI ± 4 (doubleword transferred) ES:[D!] = DS:[SI]; DI = DI ± and BYTE2are bytes) 1;S! = SI ± 1 (if BYTE1

Faculty of Electronic Engineering – Dept. of Computer Science & Eng.

Microprocessors Course 4 - 18

Miscellaneous Data Transfer Instructions XCHG

The XCHG (exchange) instruction exchanges the contents of a register with the contents of any other register or memory location. •

Examples of XCHG instructions.

Assembly Language

XCHG AL, CL XCHG CX, BP XCHG EDX, ESI XCHG AL, DATA2

Operation

Exchanges the contents of AL with CL Exchanges the contents of CX with BP Exchanges the contents of EDX with ESI Exchanges the contents of AL with data segment memory location DATA2

ةي فون ملا ةع ماج Faculty of Electronic Engineering – Dept. of Computer Science & Eng.

Microprocessors Course 4 - 19

ةي فون ملا ةع ماج

IN and OUT

• The contents of AL, AX, or EAX are transferred only between the I/O device and the microprocessor. • An IN instruction transfers data from an external I/O device to AL, AX, or EAX.

• An OUT transfers data from AL, AX, or EAX to an external I/O device.

• • Two forms of I/O device (port) addressing exist for IN and OUT: fixed-port and variable-port.

Fixed-port addressing

allows data transfer between AL, AX, or EAX using an 8-bit I/O port address. • Variable-port addressing allows data transfers between AL, AX, or EAX and a 16-bit port address. It is called

variable-port addressing

because the I/O port number is stored in register DX

Faculty of Electronic Engineering – Dept. of Computer Science & Eng.

Microprocessors Course 4 - 20

•Example of IN and OUT instructions.

Assembly Language Operation

IN AL, p8 IN AX, p8 IN EAX, p8 IN AL, DX IN AX, DX IN EAX, DX OUT p8, AL OUT p8, AX OUTp8, EAX OUT DX, AL OUT DX, AX OUT DX, EAX 8-bits are input to AL from I/O port p8 16-bits are input to AX from I/O port p8 32-bits are input to EAX from I/O port p8 8-bits are input to AL from I/O port DX 16-bits are input to AX from I/O port DX 32-bits are input to EAX from I/O port DX 8-bits are output from AL to I/O port p8 16-bits are output from AX to I/O port p8 32-bits are output from EAX to I/O port p8 8-bits are output from AL to I/O port DX 16-bits are output from AX to I/O port DX 32-bits are output from EAX to I/O port DX

Note:

p8 = an 8-bit I/O port number and DX = the 16-bit port address held in DX.

ةي فون ملا ةع ماج Faculty of Electronic Engineering – Dept. of Computer Science & Eng.

Microprocessors Course 4 - 21

MOVSX and MOVZX

• The MOVSX (move and sign-extend) and MOVZX (move and zero-extend) instructions are found in the 80386-Pentium 4 instruction sets. These instructions move data, and at the same time either sign- or zero-extend it. •

Examples of the MOVSX and MOVZX instructions.

Assembly Language

MOVSX CX, BL MOVSX ECX, AX MOVSX BX, DATA1 MOVSX EAX, [EDI] MOVZX DX, AL MOVZX EBP, DI MOVZX DX, DATA2

Operation

Sign-extends BL into CX Sign-extends AX into ECX Sign-extends the byte at DATA1 into BX Sign-extends the word at the data segment memory location addressed by EDI into EAX Zero-extends AL into DX Zero-extends Dl into EBP Zero-extends the byte at data segment memory location DATA2 into DX

ةي فون ملا ةع ماج Faculty of Electronic Engineering – Dept. of Computer Science & Eng.

Microprocessors Course 4 - 22

BSWAP

• The BSWAP (byte swap) instruction is available only in the 80486 and all versions of the Pentium microprocessors. • This instruction takes the contents of any 32-bit register and swaps the first byte with the fourth, and the second with the third. • For example, BSWAP EAX instruction with EAX = 00112233H swaps bytes in EAX, resulting in EAX = 33221100H.

ةي فون ملا ةع ماج Faculty of Electronic Engineering – Dept. of Computer Science & Eng.

Microprocessors Course 4 - 23

ةي فون ملا ةع ماج

CMOV

• The CMOV

(conditional

move) class of instruction is new to the Pentium Pro and Pentium 4 instruction sets. • These instructions move the data only if the condition is true. • For example, the CMOVZ instruction moves data only if the result from some prior instruction was a zero. • The destination is limited to only a 16- or 32-bit register, but the source can be a 16- or 32-bit register or memory location.

Faculty of Electronic Engineering – Dept. of Computer Science & Eng.

Microprocessors Course 4 - 24

ةي فون ملا ةع ماج

Examples of the conditional move instructions.

Assembly

Language CMOVB

CMOVAE CMOVBE CMOVA

CMOVZ

CMOVNE CMOVL CMOVLE CMOVG CMOVGE CMOVS CMOVNS CMOVC CMOVNC CMOVO CMOVNO CMOVP

CMOVNP Condition Tested

C = 1 c = 0 Z = 1 or C = 1 Z = 0 and C = 0 Z = 1 Z = 0 S<>0 Z = 1 or S <> 0 Z = 0 and S = 0 S = 0 S = 1 S = 0 C = 1 C = 0 O = 1 0 = 0 P = 1 P = 0

Operation

Move if below Move if above or equal Move if below or equal Move if above Move if equal or set if zero Move if not equal or set if not zero Move if less than Move if less than or equal Move if greater than Move if greater than or equal Move if sign (negative) Move if no sign (positive) Move if carry Move if no carry Move if overflow Move if no overflow Move if parity or set if parity even Move if no parity or set if parity odd

Faculty of Electronic Engineering – Dept. of Computer Science & Eng.

Microprocessors Course 4 - 25

Questions and Answers

ةي فون ملا ةع ماج Faculty of Electronic Engineering – Dept. of Computer Science & Eng.

Microprocessors Course 4 - 26

Which registers move onto the stack with the PUSHA instruction?

• The PUSHA

(push

all) instruction copies the 16-bit registers to the stack in the following order: AX, CX, DX, BX, SP, BP, SI, and DI.

ةي فون ملا ةع ماج Faculty of Electronic Engineering – Dept. of Computer Science & Eng.

Microprocessors Course 4 - 27

Which registers move onto the stack for a PUSHAD instruction?

• The PUSHAD

(push all)

instruction copies the 32-bit registers to the stack in the following order: EAX, ECX, EDX, EBX, ESP, EBP, ESI,

ةي فون ملا ةع ماج Faculty of Electronic Engineering – Dept. of Computer Science & Eng.

Microprocessors Course 4 - 28

ةي فون ملا ةع ماج Describe the operation of each of the following instructions: (a) PUSH (c) PUSH AX [BX] (b) POP (d) PUSHFD ESI (e) POPDS (f) PUSHD 4 Instruction PUSH AX POP ESI PUSH [BX] PUSHFD POP DS PUSHD 4 Operation

Pushes the contents of AX onto the stack.

Remove a 32-bit number from the stack and places it into the ESI register.

Pushes the 16-bit contents of the data segment memory location addressed by BX onto the stack.

Pushes the EFLAG register onto the stack.

Remove a 16-bit number from the stack and places it into the DS register.

Pushes the 32-bit number 4 onto the stack.

Faculty of Electronic Engineering – Dept. of Computer Science & Eng.

Microprocessors Course 4 - 29

Explain what happens when the PUSH BX instruction executes. Make sure to show where BH and BL are stored. (Assume that SP = 0100H and SS = 0200H.)

1 2 3 4 0 1 0 0 Stack segment 02102 02101 02100 020FF 020FE EBX ESP 1 2 3 4 0 0 F E Stack segment 1 2 3 4 02102 02101 02100 020FF 020FE 020FD 020FC

Repeat the above question for the PUSH EAX

EAX 1 2 3 4 5 6 7 8 Stack segment 02102 02101 02100 020FF 020FE EAX 1 2 3 4 5 6 7 8 ESP 0 0 F C Stack segment 1 2 3 4 5 6 7 8 02102 02101 02100 020FF 020FE 020FD 020FC ESP 0 1 0 0 CS DS SS 0100 02000 CS DS SS 0 2 0 0 00FC + 02000 0 2 0 0 02000 + 02100 02000 020FC

Stack before the instruction PUSH EAX executed.

ةي فون ملا ةع ماج

Stack after the instruction PUSH EAX executed.

Faculty of Electronic Engineering – Dept. of Computer Science & Eng.

Microprocessors Course 4 - 31

Develop a sequence of instructions that move the contents of data segment memory locations NUMB and NUMB+1 into BX, DX, and SI.

MOV BX, NUMB MOV DX, BX MOV SI, BX

ةي فون ملا ةع ماج Faculty of Electronic Engineering – Dept. of Computer Science & Eng.

Microprocessors Course 4 - 32

Develop a sequence of instructions that copy 12 bytes of data from an area of memory addressed by SOURCE into an area of memory addressed by DEST.

MOV SI, OFFSET SOURCE MOV DI, OFFEST DEST MOV CX, 12 REP MOVSB

ةي فون ملا ةع ماج Faculty of Electronic Engineering – Dept. of Computer Science & Eng.

Microprocessors Course 4 - 33

Select an assembly language instruction that exchanges the contents of the EBX register with the ESI register.

XCHG EBX, ESI

ةي فون ملا ةع ماج Faculty of Electronic Engineering – Dept. of Computer Science & Eng.

Microprocessors Course 4 - 34

Write a sequence of instructions that input 50 bytes of data from an I/O device whose address is 03ACH and stores the data in extra segment memory array LISTS.

;Using the REP INSB to input data to a memory array MOV Dl, OFFSET LISTS MOV DX, 3ACH CLD MOV CX, 50 REP INSB ;address array ;address I/O ; auto-increment ; load count ;input data ةي فون ملا ةع ماج Faculty of Electronic Engineering – Dept. of Computer Science & Eng.

Microprocessors Course 4 - 35

Write a short sequence of instructions that transfer data from a data segment memory array (ARRAY) to an I/O device at I/O address 3ACH.

; Using the REP OUTS to output data from a memory array MOV SI, OFFSET ARRAY MOV DX, 3ACH CLD MOV CX, 100 REP OUTSB ;address array ;address I/O ; auto-increment ; load count ةي فون ملا ةع ماج Faculty of Electronic Engineering – Dept. of Computer Science & Eng.

Microprocessors Course 4 - 36

Develop a sequence of instructions that exchange the contents of AX with BX, ECX with EDX, and SI with DI.

XCHG XCHG XCHG AX, BX ECX, EDX SI, DI

ةي فون ملا ةع ماج Faculty of Electronic Engineering – Dept. of Computer Science & Eng.

Microprocessors Course 4 - 37

Write a short program that exchanges the contents of memory locations DATA1 with the and DI with the contents of these memory locations DATA2.

ةي فون ملا ةع ماج .MODEL SMALL .DATA

DATA1 DW 2000H DATA2 DW 3000H .CODE

.STARTUP

MOV SI, OFFSET DATA1 MOV DI, OFFSET DATA2 MOV BX, [SI] MOV CX, [DI] MOV [SI], CX MOV [DI], BX .EXIT

END ; select SMALL model ; start of DATA segment ; define DATA1 ; define DATA2 ; start of CODE segment ; start of program ; address DATA1 with SI ; address DATA2 with DI ; exit to DOS ; end of file Faculty of Electronic Engineering – Dept. of Computer Science & Eng.

Microprocessors Course 4 - 38