Data Movement Instructions

Data movement instructions are among the most frequently used instructions.

The MOV Set of Instructions

Instruction   Effect Description
MOV S,D S  –> D Move
    movb Move byte
    movw Move word
    movl Move double word
    movq Move quad word
movabsq I,R I –> R Move absolute quad word
  • S can be an immediate value, a register or a memory location
    • If S is a register, it’s size must match the operation (eg. %ax for movb)
  • D can be a register or memory location
  • S and D can not both be memory locations.

Copying a value from one memory location to another requires two operations

  • Copy value from memory to a register
  • Copy value from register to a memory location

5 Possible Mov Operand Combinations

1 movl $0x405, %eax Copy constant value to a register
2 movb $-17,(%esp) Copy constant value to a memory location
3 movw %bp, %sp Copy value in register to another register
4 movq %rax, -12(%rbp) Copy value in register to a memory location
5 movb (%rdi,%rcx), %a1 Copy value in memory to a register

Zero Extending Data Movement Instructions (MOVZ)

MOVZ instructions will fill out the remaining bytes of the destination with zeros.

MOVZ   S,R ZeroExtend(S) –> R
    movzbw Move zero-extended byte to word
    movzbl Move z.e byte to double word
    movzbq Move z.e byte to quad word
    movzwl Move z.e word to double word
    movzwq Move z.e word to quad word

S is either a register or a memory location, and R is a register.

Notice there is no movzlq – it doesn’t exist.  Instead we use movl which copies a double word to a quad word while setting the 4 higher order bytes to 0’s.

Sign-extended Data Movement Instructions (MOVS)

S is either a register or a memory location, and R is a register.

MOVS S, R SignExtend(S) –> R
    movsbw
    movsbl
    movsbq
    movswl
    movswq
    movslq
cltq SignExtend(%eax) –> %rax

cltq has no operands.  It simply sign-extends %eax so that the value can be accessed via %rax.  It has the exact same result as movslq %eax, %rax.

Data Movement Example

Arguments are passed to functions in registers and return values are passed out of functions via %rax.  For example, for the function exchange(), the return value x is immediately stored in %rax.

C code

long exchange(long *xp, long y){
    long x = *xp;
    *xp = y;
    return x;
}

Assembly code

Note: xp is in %rdi, y is in %rsi

exchange:
    movq  (%rdi), %rax   // store value at xp 
                         // in the return register %rax
    movq  %rsi, (%rdi)   // store y at xp
    ret                  // return %rax

Pushing and Popping Stack Data

  • In x86-64, the stack is stored in memory.
  • The stack grows downward so that the top element has the lowest memory address of all stack elements.
  • The register %rsp holds the address of the top of the stack. Growing the stack requires decrementing the stack pointer.
  • The following instructions push and pop data on and off the stack.
    • pushq S                      // pushes data in S onto the stack
    • popq D                       // pops data from the stack into D

A pushq %rbp instruction

  • subtracts 8 bytes from %rsp.
  • copies the data %rbp to the address stored in %rsp.

Equivalently,

subq $8, %rsp
movq %rbp, (%rsp)

Push Assembly Instruction

A popq %rax instruction

  • copies the contents at %rsp to %rax
  • adds 8 to %rsp

Equivalently,

movq (%rsp),%rax
addq $8,%rsp

A program can access arbitrary elements in the stack.

movq 8(%rsp), %rax       // copies into %rax the data 8 bytes higher in memory from %rsp.

© 2017 – 2018, Eric. All rights reserved.