Sunday, 16 February 2025

Data Movement Instructions

Data Movement Instructions:

This instruction type copies a word or byte from source to destination. The destination can be a register or memory. The source can be a register, a memory location or an immediate data. No flags are affected after execution of MOV instruction.

Types of Data Movement Instructions in 8086:

  1. Register to Register Transfers
  2. Memory to Register & Register to Memory Transfers
  3. Immediate to Register & Immediate to Memory Transfers
  4. I/O Transfers (Input/Output Instructions)
  5. Stack Operations (PUSH & POP)
  6. Flag Transfers
  7. Address Transfer Instructions

1. Register to Register Transfer

These instructions transfer data between general-purpose registers.
🔹 Example:

MOV AX, BX ; Copy contents of BX to AX

2. Memory to Register & Register to Memory Transfers

These instructions move data between memory and registers.
🔹 Examples:

MOV AX, [5000H] ; Load data from memory location 5000H into AX MOV [6000H], BX ; Store BX's content into memory location 6000H

📌 Note:

  • Memory-to-memory transfers are not allowed directly.
  • Only MOV, XCHG, and LEA can access memory directly.

3. Immediate to Register & Immediate to Memory Transfers

These instructions transfer an immediate value (constant) to a register or memory.
🔹 Examples:

MOV AX, 1234H ; Move 1234H into AX MOV [5000H], 78H ; Store 78H into memory location 5000H

4. I/O Transfers (Input/Output Instructions)

Used for communication with I/O devices.

In Instruction copies data from a port to destination which may AL or AX i.e. Accumulator register.

🔹 Examples:

IN AL, 60H ; Input byte from port 60H into AL OUT 70H, AL ; Output byte from AL to port 70H

5. Stack Operations (PUSH & POP)

PUSH
  • Stack operations store and retrieve data using the stack.
  • This instruction stores the contents of source on to the stack.
  • The source can be a general purpose register, segment register or memory.
  • No flags are affected by this instruction
POP

  • This instruction copies a word from stack segment pointed by SP to destination. 
  • The destination can be a general purpose register, a segment register or a memory location. 
  • After copying, SP is automatically incremented by 2.
  • No flags are affected by POP instruction

🔹 Examples:

PUSH AX ; Save AX register onto the stack
POP BX ; Retrieve the last pushed value into BX

PUSH AX ; Let AX = 1122H, SS = 2000H, SP = FFFFH

PUSH DS

PUSH [2000H]

POP AX ; Let SS = 2000H and SP = FFFDH

POP DS

POP CS ; This is not allowed

POP [5000H]

📌 Note: The stack works on LIFO (Last In, First Out) principle.


6. Flag Transfers

  • Moves data between flags and registers.

Pop flags from stack

  • This instruction is used to store a word from memory location at top of the stack
  • The SP (Stack Pointer) is Incremented by 2.

🔹 Example: POPF

PUSHF: Push flags to stack

General form: PUSHF

  • This instruction pushes the flag register contents on to the stack. Higher byte of flag is stored first and then lower byte of the flag is stored.
  • The SP is decremented by 2.
  • No flags are affected.

🔹Example: PUSHF

Load AH from lower byte of flag

General form: LAHF

  • This instruction copies lower byte of flag register to AH register.
  • No flags are affected.

🔹 Example: LAHF

SAHF: Store AH register to lower byte of flag register

General form: LAHF

  • This instruction copies contents of AH register to lower byte of flag register.
  • Depending upon the bits of AH register, the flags in the lower byte of flag register will be set or reset.

🔹 Example: SAHF

🔹 Examples:

PUSHF ; Push the flag register onto the stack
POPF ; Pop the flag register from the stack
LAHF ; Load AH with the lower byte of the flag register
SAHF ; Store AH into the lower byte of the flag register

7. Address Transfer Instructions

LEA - Load Effective Address

Used to load addresses into registers.

General form: LEA Register, source

  • This instruction loads the effective address of destination operand into the source register.
  • No flags are affected.

🔹 Example:

LEA BX, NUM1

LDS- Load Register and DS with words from memory

General form: LDS Register, Memory address of first word

  • This instruction copies a word from memory into register specified. It then copies a word from next memory locations into DS/ES.
  • No flags are affected.

🔹 Example:

LDS BX, [5000H]

🔹 Examples:

LEA SI, [ARRAY] ; Load effective address of ARRAY into SI LDS DX, [5000H] ; Load DS and DX from memory location 5000H LES BX, [6000H] ; Load ES and BX from memory location 6000H

📌 LEA (Load Effective Address) loads the address of a memory operand, whereas LDS & LES load a segment register along with an offset.

8) XCHG: Exchange

General form: XCHG destination, source

  • This instruction exchanges contents of source and destination.
  • Source and destination both cannot be memory locations.
  • Source and destination must be of same size (i.e. both must be bytes or both must be words).
  • Segment register cannot be used with this instruction.
  • No flags are affected by this instruction.

🔹 Examples:

XCHG AX, DX

XCHG BL, CL

XCHG [5000H], AX

9) IN: Copy data from a port

General form: IN Accumulator (AX or AL), Port

  • This instruction copies data from a port to AL or AX register.
  • If an 8 bit port is read, the data will go into AL.
  • If a 16 bit port is read, the data will go to AX.
  • No flags are affected.

🔹 Example 

1) MOV DX, 8000H ; move port address into DX

IN AL, DX

2) MOV DX, 8080H ; move port address into DX

IN AX, DX

10) OUT: Output a byte or word to a port

General form: OUT Port, Accumulator (AX or AL)

This instruction copies a byte from Al to a port or a word from AX to a port.

🔹 Examples:

OUT 81H, AL

OUT 46H, AX

No flags are affected by this instruction.

11) XLAT: Translate a byte in AL

General form: XLAT

  • This instruction is used to translate a byte from one code to another code.
  • It replaces a byte in AL register with a byte pointed by BX in a lookup table in memory.
  • Before using this instruction lookup table must be present in memory. Starting address of table is loaded in BX register. The byte to be translated is loaded in AL.
  • AL -> DS : [ BX + AL ]
  • The value in AL is added to BX. This new value will be used as pointer in data segment.
  • From the location, pointed by [BX+AL], data will be transferred to AL.
  • No flags are affected by this instruction. ow to find ASCII value of a decimal digit (0 – 9) present in AL.
Following example shows how to find ASCII value of a decimal digit (0 – 9) present in AL.

ASSUME CS: CODE, DS: DATA

DATA SEGMENT

ASCII_CODES DB 30H,31H,32H,33H,34H,35H,36H,37H,38H,39H

DIGIT DB 05 ; Find ASCII code of 5

RES DB ? ; To store the result i.e. ASCII code

DATA ENDS

CODE SEGMENT

MAIN:

MOV AX, DATA ; Initialize data segment

MOV DS, AX

MOV BX, OFFSET ASCII_CODES ; Find offset of table

MOV AL, DIGIT ; Move code to AL

XLAT ; Find new code

MOV RES, AL ; Store result

MOV AH, 4CH ; Terminate the program

INT 21H

CODE ENDS

END MAIN

No comments:

Post a Comment

Desktop Virtualisation

Desktop Virtualization ( DV ) Desktop Virtualization ( DV ) is a technique that creates an illusion of a desktop provided to the user. It d...