MOV Instruction and Assembly Memory Operations in Assembly Language

Overview of the MOV Instruction

  • The MOV instruction is recognized as one of the most frequently utilized commands within Assembly programming.
  • The primary function of the instruction is to copy the data from the second operand, known as the source, into the first operand, known as the destination.
  • The fundamental syntax follows the structure: MOV destination, sourceMOV\ \text{destination},\ \text{source}
  • Key Operation Mechanic: After the execution of the command, the source remains unchanged, and the destination contains the same data as the source. For example, in the command MOV AX, BXMOV\ AX,\ BX, data is transferred from register BXBX (source) to register AXAX (destination). Post-execution, both registers will hold identical values.

Rules and Operational Features of MOV

  • Operand Types:
    • The source operand can be an immediate value (constant), a general-purpose register, or a memory location.
    • The destination operand can be a general-purpose register or a memory location.
  • Size Consistency: A critical requirement for the MOVMOV instruction is that both operands must be of equal size. This can be either a byte (8-bit) or a word (16-bit).
  • Data Declarations and Specific Register Matching:
    • DB (Define Byte): Represents an 8-bit size. This must be used with 8-bit registers such as ALAL, AHAH, BLBL, BHBH, CLCL, CHCH, DLDL, or DHDH.
    • DW (Define Word): Represents a 16-bit size. This must be used with 16-bit registers such as AXAX, BXBX, CXCX, or DXDX.

Supported Operand Combinations

  • Register to Register: Copies the contents of one register to another.
    • Example: MOV AX, BXMOV\ AX,\ BX copies the contents of BXBX to AXAX.
    • Trace: If before execution AX=0000hAX = 0000h and BX=1234hBX = 1234h, after execution AX=1234hAX = 1234h and BX=1234hBX = 1234h.
  • Immediate to Register: Loads a constant value into a register.
    • Example 1: MOV AX, 05HMOV\ AX,\ 05H loads the constant 05h05h into AXAX.
    • Example 2: MOV CX, 0100hMOV\ CX,\ 0100h results in CX=0100hCX = 0100h.
  • Memory to Register: Copies a value stored in memory into a register.
    • Example: MOV AX, [NUM]MOV\ AX,\ [NUM] or MOV AX, [BX]MOV\ AX,\ [BX].
    • If NUMNUM is defined as DB 50DB\ 50, then MOV AL, [NUM]MOV\ AL,\ [NUM] is appropriate for the 8-bit size.
    • If NUMNUM is defined as DW 50DW\ 50, then MOV AX, [NUM]MOV\ AX,\ [NUM] is appropriate for the 16-bit size.
  • Register to Memory: Stores the contents of a register into a specific memory location.
    • Example: MOV NUM, AXMOV\ NUM,\ AX or MOV [BX], AXMOV\ [BX],\ AX.
  • Immediate to Memory: Stores a constant value directly into a memory address.
    • Example: MOV NUM, 10MOV\ NUM,\ 10 or MOV [BX], 05HMOV\ [BX],\ 05H.
  • Segment Register to Register: Copies the value from a segment register to a general-purpose register.
    • Example: MOV AX, DSMOV\ AX,\ DS copies the Data Segment value to AXAX.
  • Register to Segment Register: Loads a value from a general-purpose register into a segment register.
    • Example: MOV DS, AXMOV\ DS,\ AX loads the content of AXAX into DSDS.

Memory Addressing and Data Segments

  • Valid Registers for Memory Addressing: Only Base Registers (BXBX, BPBP) and Index Registers (SISI, DIDI) are permitted for specifying memory addresses.
    • Legal examples include: MOV [bx], axMOV\ [bx],\ ax, MOV ax, [bp]MOV\ ax,\ [bp], MOV [si], cxMOV\ [si],\ cx, and MOV dx, [di]MOV\ dx,\ [di].
  • Addressing Combinations: Base and index registers can be combined for memory addressing.
    • Examples: MOV [bx+si], axMOV\ [bx+si],\ ax, MOV ax, [bp+si]MOV\ ax,\ [bp+si], MOV [bp+di], cxMOV\ [bp+di],\ cx, and MOV dx, [bx+di]MOV\ dx,\ [bx+di].
  • Default Segment: DSDS (Data Segment) is the default segment used for memory access.
    • The CPU interprets an instruction like MOV AX, [NUM]MOV\ AX,\ [NUM] as DS:NUMDS:NUM.
    • The calculation for the Physical Address is: Physical Address=DS+Offset of NUMPhysical\ Address = DS + Offset\ of\ NUM.

Constraints and Illegal MOV Operations

  • Memory to Memory Transfers: The instruction MOV [num], [bx]MOV\ [num],\ [bx] is not allowed. To move data between memory locations, a register must be used as a temporary intermediary.
    • Correct procedure: MOV AX, [num]MOV\ AX,\ [num] followed by MOV [bx], AXMOV\ [bx],\ AX.
  • Immediate to Segment Register: Loading a constant directly into a segment register is illegal, such as MOV DS, 1000MOV\ DS,\ 1000.
    • Correct procedure: MOV AX, 1000hMOV\ AX,\ 1000h followed by MOV DS, AXMOV\ DS,\ AX.
  • Code Segment Limitation: The Code Segment register (CSCS) cannot be loaded or modified using the MOVMOV instruction.
  • Size Mismatch: Moving data between operands of different sizes is illegal. For example, MOV AL, BXMOV\ AL,\ BX is illegal because ALAL is 8-bit and BXBX is 16-bit.

Questions & Discussion

  • Determining the Legality and Type of MOV Instructions:

    1. MOV AX, BXMOV\ AX,\ BX: Legal (Register to Register).
    2. MOV CL, 25MOV\ CL,\ 25: Legal (Immediate to Register).
    3. MOV DX, 1234hMOV\ DX,\ 1234h: Legal (Immediate to Register).
    4. MOV AL, [num]MOV\ AL,\ [num]: Legal (Memory to Register).
    5. MOV [num], DLMOV\ [num],\ DL: Legal (Register to Memory).
    6. MOV [num], 50MOV\ [num],\ 50: Legal (Immediate to Memory).
    7. MOV [n1], [n2]MOV\ [n1],\ [n2]: Illegal (Direct Memory to Memory move is not permitted).
    8. MOV AX, DSMOV\ AX,\ DS: Legal (Segment Register to Register).
    9. MOV DS, AXMOV\ DS,\ AX: Legal (Register to Segment Register).
    10. MOV DS, 2000hMOV\ DS,\ 2000h: Illegal (Segment registers cannot be loaded directly with immediate values).
    11. MOV ES, BXMOV\ ES,\ BX: Legal (Register to Segment Register).
    12. MOV BX, ESMOV\ BX,\ ES: Legal (Segment Register to Register).
    13. MOV CS, AXMOV\ CS,\ AX: Illegal (The Code Segment register cannot be targets of a MOV instruction).
    14. MOV AX, [BX]MOV\ AX,\ [BX]: Legal (Memory to Register).
    15. MOV [BX], AXMOV\ [BX],\ AX: Legal (Register to Memory).
    16. MOV SI, DIMOV\ SI,\ DI: Legal (Register to Register).
    17. MOV AL, BXMOV\ AL,\ BX: Illegal (Size mismatch: 8-bit vs 16-bit).
    18. MOV CL, AMOV\ CL,\ 'A': Legal (Immediate value to Register).
    19. MOV BL, 010111111bMOV\ BL,\ 010111111b: Legal (Immediate to Register).
    20. MOV [BX], CXMOV\ [BX],\ CX: Legal (Register to Memory).
  • Encoding Example 1 (Addition):

    • Data segment:
      • num1 DB 25num1\ DB\ 25 (Hex value: 19h19h)
      • num2 DB 15num2\ DB\ 15 (Hex value: 0Fh0Fh)
      • result DB ?result\ DB\ ? (Initial value: 00)
    • Logic Trace:
      • MOV AL, num1MOV\ AL,\ num1: ALAL becomes 19h19h.
      • MOV BL, num2MOV\ BL,\ num2: BLBL becomes 0Fh0Fh.
      • ADD AL, BLADD\ AL,\ BL: AL=19h+0Fh=28hAL = 19h + 0Fh = 28h.
      • MOV result, ALMOV\ result,\ AL: resultresult becomes 28h28h.
    • Memory Addresses:
      • Memory address for num1num1: 0700:01020700: 0102.
      • Memory address for num2num2: 0700:01030700: 0103.
    • Discussion on Memory Addition: The CPU cannot directly add two memory locations (e.g., ADD num1, num2ADD\ num1,\ num2) because it violates the rule that both operands cannot be memory operands; one must be a register.
  • Encoding Example 2 (Transfer):

    • Data segment:
      • value1 DB 20value1\ DB\ 20 (Hex value: 14h14h)
      • value2 DB ?value2\ DB\ ? (Initial value: 00)
    • Logic Trace:
      • MOV AX, @DATAMOV\ AX,\ @DATA and MOV DS, AXMOV\ DS,\ AX initialize the data segment.
      • MOV AL, value1MOV\ AL,\ value1: ALAL becomes 14h14h.
      • MOV value2, ALMOV\ value2,\ AL: value2value2 becomes 14h14h.
    • Memory Addresses:
      • value1value1 address: 0700:01020700: 0102.
      • value2value2 address: 0700:01030700: 0103.
  • Encoding Example 3 (Arithmetic Results):

    • Given num1=20num1 = 20 (14h14h) and num2=30num2 = 30 (1Eh1Eh):
    • sum=num1+num2=14h+1Eh=32hsum = num1 + num2 = 14h + 1Eh = 32h.
    • difference=num2num1=1Eh14h=0Ahdifference = num2 - num1 = 1Eh - 14h = 0Ah.
    • Final Variable Values:
      • num1=14hnum1 = 14h
      • num2=1Ehnum2 = 1Eh
      • sum=32hsum = 32h
      • difference=0Ahdifference = 0Ah