Download presentation
Presentation is loading. Please wait.
1
Chapter 4 Data Movement Instructions
Microprocessors Chapter 4 Data Movement Instructions prepared by Dr. Mohamed A. Shohla
2
Chapter Overview MOV Revisited PUSH/POP String Data Transfers
Miscellaneous Data Transfer Instructions
3
PUSH Examples of the PUSH instructions.
The PUSH instruction always transfers two bytes of data to the stack; the and above transfer two or four bytes, depending on the register or size of the memory location. Examples of the PUSH instructions. Symbolic Example Note PUSH reg16 PUSH BX 16-bit register PUSH reg32 PUSH EDX 32-bit register PUSH mem16 PUSH WORD PTR [BX] 16-bit pointer PUSH seg PUSH DS Segment register PUSH imm8 PUSH ‘,’ 8-bit immediate PUSHW imm16 PUSHW 1000H 16-bit immediate PUSHD imm32 PUSHD 20 32-bit immediate PUSHA Save all 16-bit registers PUSHAD Save all 32-bit registers PUSHF Save flags PUSHFD Save EFLAGs
4
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.
5
PUSH Example : PUSHA The operation of the PUSHA instruction, showing the location and order of stack data.
6
POP Examples of the POP instructions. Symbolic Example Note POP reg16
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 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 Example Note POP reg16 POP CX 16-bit register POP reg32 POP EBP 32-bit register POP mem16 POP WORD PTR[BX+1] 16-bit pointer POP mem32 POP DATA3 32-bit memory address POP seg POP FS Segment register POPA Pop all 16-bit registers POPAD Pop all 32-bit registers POPF Pop flags POPFD Pop EFLAGs
7
POP Example : POP BX The POP BX instruction, showing how data are removed from the stack. This instruction is shown after execution.
8
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.
9
Examples of Load-effective address instructions.
10
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 ; select SMALL model .DATA ; start of DATA segment DATA1 DW H ; define DATA1 DATA2 DW H ; define DATA2 .CODE ; start of CODE segment .STARTUP ; start of program LEA SI, DATA1 ; 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 ; exit to DOS END ; end of file
11
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.
12
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.
13
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.
14
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.
15
LODS Examples of the LODS instructions.
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 Operation LODSB LODSW LODSD LODS LIST LODS DATA1 LODS FROG AL = DS:[SI]; SI = SI ± 1 AX = DS:[SI];SI = SI ± 2 EAX = DS:[SI];SI = SI ± 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)
16
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.
17
STOS Examples of the STOS instructions.
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.
18
MOVS Examples of the MOVS instructions.
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 Operation MOVSB ES:[DI] = DS:[SI]; DI = DI ± 1; SI = SI ± 1 (byte transferred) MOVSW ES:[DI] = DS:[SI];D! = DI ± 2; SI = SI ± 2 (word transferred) MOVSD ES:[DI] = DS:[SI]; Dl = DI ± 4; SI = SI ± 4 (doubleword transferred) MOVS BYTE1,BYTE2 ES:[D!] = DS:[SI]; DI = DI ± 1;S! = SI ± 1 (if BYTE1 and BYTE2are bytes)
19
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 Operation XCHG AL, CL XCHG CX, BP XCHG EDX, ESI XCHG AL, DATA2 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
20
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
21
Example of IN and OUT instructions.
Note: p8 = an 8-bit I/O port number and DX = the 16-bit port address held in DX.
22
MOVSX and MOVZX Examples of the MOVSX and MOVZX instructions.
The MOVSX (move and sign-extend) and MOVZX (move and zero-extend) instructions are found in the 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 Operation MOVSX CX, BL Sign-extends BL into CX MOVSX ECX, AX Sign-extends AX into ECX MOVSX BX, DATA1 Sign-extends the byte at DATA1 into BX MOVSX EAX, [EDI] Sign-extends the word at the data segment memory location addressed by EDI into EAX MOVZX DX, AL Zero-extends AL into DX MOVZX EBP, DI Zero-extends Dl into EBP MOVZX DX, DATA2 Zero-extends the byte at data segment memory location DATA2 into DX
23
BSWAP The BSWAP (byte swap) instruction is available only in the 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 = H swaps bytes in EAX, resulting in EAX = H.
24
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.
25
Examples of the conditional move instructions.
26
Questions and Answers
27
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.
28
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,
29
Describe the operation of each of the following instructions: (a) PUSH
Describe the operation of each of the following instructions: (a) PUSH AX (b) POP ESI (c) PUSH [BX] (d) PUSHFD (e) POPDS (f) PUSHD 4 Instruction Operation PUSH AX Pushes the contents of AX onto the stack. POP ESI Remove a 32-bit number from the stack and places it into the ESI register. PUSH [BX] Pushes the 16-bit contents of the data segment memory location addressed by BX onto the stack. PUSHFD Pushes the EFLAG register onto the stack. POP DS Remove a 16-bit number from the stack and places it into the DS register. PUSHD 4 Pushes the 32-bit number 4 onto the stack.
30
Explain what happens when the PUSH BX instruction executes
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.) Stack after the instruction PUSH BX executed. Stack before the instruction PUSH BX executed.
31
Repeat the above question for the PUSH EAX
Stack after the instruction PUSH EAX executed. Stack before the instruction PUSH EAX executed.
32
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
33
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
34
Select an assembly language instruction that exchanges the contents of the EBX register with the ESI register. XCHG EBX, ESI
35
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 ;address array MOV DX, 3ACH ;address I/O CLD ; auto-increment MOV CX, 50 ; load count REP INSB ;input data
36
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 ;address array MOV DX, 3ACH ;address I/O CLD ; auto-increment MOV CX, ; load count REP OUTSB
37
Develop a sequence of instructions that exchange the contents of AX with BX, ECX with EDX, and SI with DI. XCHG AX, BX XCHG ECX, EDX XCHG SI, DI
38
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 ; select SMALL model .DATA ; start of DATA segment DATA1 DW H ; define DATA1 DATA2 DW H ; define DATA2 .CODE ; start of CODE segment .STARTUP ; start of program MOV SI, OFFSET DATA ; address DATA1 with SI MOV DI, OFFSET DATA ; address DATA2 with DI MOV BX, [SI] MOV CX, [DI] MOV [SI], CX MOV [DI], BX .EXIT ; exit to DOS END ; end of file
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.