Skip to main content
Engineering LibreTexts

7.2: Data Movement

  • Page ID
    19898
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Typically, data must be moved into a CPU register from RAM in order to be operated upon. Once the calculations are completed, the result may be copied from the register and placed into a variable. There are a number of simple formulas in the example program that perform these steps. This basic data movement operation is performed with the move instruction.

    The general form of the move instruction is:

    mov  <dest>, <src>
    

    The source operand is copied from the source operand into the destination operand. The value of the source operand is unchanged. The destination and source operand must be of the same size (both bytes, both words, etc.). The destination operand cannot be an immediate. Both operands cannot be memory. If a memory to memory operation is required, two instructions must be used.

    截屏2021-07-19 下午10.18.08.png

    When the destination register operand is of double-word size and the source operand is of double-word size, the upper-order double-word of the quadword register is set to zero. This only applies when the destination operand is a double-word sized integer register.

    Specifically, if the following operations are performed,

    mov    eax, 100        ; eax = 0x00000064
    mov    rcx, -1         ; rcx = 0xffffffffffffffff
    mov    ecx,eax         ; ecx = 0x00000064
    

    Initially, the rcx register is set to -1 (which is all 0xF's). When the positive number from the eax register (10010) is moved into the rcx register, the upper-order portion of the quadword register rcx is set to 0 over-writing the 1's from the previous instruction.

    The move instruction is summarized as follows:

    Instruction Explanation
    mov <dest>, <src> Copy source operand to the destination operand.
    Note 1, both operands cannot be memory.
    Note 2, destination operands cannot be an immediate.
    Note 3, for double-word destination and source operand, the upper-order portion of the quadword register is set to 0.
    Examples:
    mov    ax, 42
    mov    c1, byte [bvar]
    mov    dword [dVar], eax
    mov    qword [qVar], rdx
    

    A more complete list of the instructions is located in Appendix B. For example, assuming the following data declarations:

        dValue    dd    0
        bNum      db    42
        wNum      dw    5000
        dNum      dd    73000
        qNum      dq    73000000
        bAns      db    0
        wAns      dw    0
        dAns      dd    0
        qAns      dq    0
    

    To perform, the basic operations of:

        dValue = 27
        bAns = bNum
        wAns = wNum
        dAns = dNum
        qAns = qNum
    

    The following instructions could be used:

        mov      dword [dValue], 27        ; dValue = 27
        mov al, byte [bNum] 
        mov byte [bAns], al                ; bAns = bNum
        mov ax, word [wNum] 
        mov word [wAns], ax                ; wAns = wNum
        mov eax, dword [dNum] 
        mov dword [dAns], eax              ; dAns = dNum
        mov rax, qword [qNum] 
        mov qword [qAns], rax              ; qAns = qNum 
    

    For some instructions, including those above, the explicit type specification (e.g., byte, word, dword, qword) can be omitted as the other operand will clearly define the size. In the text it will be included for consistency and good programming practice.


    This page titled 7.2: Data Movement is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by Ed Jorgensen.

    • Was this article helpful?