OSU CSE 2421
X86-64Assembly Language – Part 2: data movement instructions, memory addressing modes, address calculations
J.E.Jones
OSU CSE 2421
Turn on your microphones and just answer…
J. E. Jones
OSU CSE 2421
What is the size of a memory address on stdlinux???
J. E. Jones
OSU CSE 2421
What is the size of a memory address on stdlinux???
What is the only suffix should we be using when we are calculating/moving addresses?
J. E. Jones
OSU CSE 2421
What is the size of a memory address on stdlinux???
What is the only suffix should we be using when we are
calculating/moving addresses?
What size registers should we be using when we are calculating addresses?
J. E. Jones
OSU CSE 2421
What is the size of a memory address on stdlinux???
What is the only suffix should we be using when we are
calculating/moving addresses?
What size registers should we be using when we are calculating addresses?
Is there ever an exception to this?
J. E. Jones
OSU CSE 2421
What is the size of a memory address on stdlinux???
What is the only suffix should we be using when we are
calculating/moving addresses?
What size registers should we be using when we are calculating/moving addresses?
Is there ever an exception to this?
J. E. Jones
OSU CSE 2421
Normal (R) Mem[Reg[R]] ◦ Register R specifies memory address
◦ Aha! Pointer dereferencing in C
movq (%rcx),%rax [rax = *rcx]
Displacement D(R) Mem[Reg[R]+D] ◦ Register R specifies start of memory region
◦ Constant displacement D specifies offset
movq 8(%rbp),%rdx [rdx = *(rbp+8)]
J. E. Jones
OSU CSE 2421
Normal (R) Mem[Reg[R]] ◦ Register R specifies memory address
movq (%rcx),%rax [rax = *rcx]
◦ Are any of these a valid instruction on stdlinux? movq (%ecx),%rax
movl (%ecx),%eax
movb (%rax),%al
J. E. Jones
OSU CSE 2421
Normal (R) Mem[Reg[R]] ◦ Register R specifies memory address
movq (%rcx),%rax
◦ Are any of these a valid instruction on stdlinux? movq (%ecx),%rax
movl (%ecx),%eax
movb (%rax),%al
J. E. Jones
OSU CSE 2421
}
t1 = *yp; *xp = t1; *yp = t0;
C language Program
Equivalent x86-64 Program
void swap(long *xp, long *yp){ register long t0;
register long t1;
t0 = *xp;
swap:
movq (%rdi), %r8 movq (%rsi), %rdx movq %rdx, (%rdi) movq %r8, (%rsi) ret
J. E. Jones
OSU CSE 2421
}
C language Program
Equivalent x86-64 Program
void swap(long *xp, long *yp){ long t0;
long t1;
t0 = *xp;
swap:
movq (%rdi), %rax movq (%rsi), %rdx movq %rdx, (%rdi) movq %rax, (%rsi) ret
t1 = *yp; *xp = t1; *yp = t0;
Wait! How do we know that (%rdi) will read from long *xp? How do we know (%rsi) will read long *yp?
J. E. Jones
OSU CSE 2421
Most General Form of the address expression Imm(Rb,Ri,S) Mem[Imm+ Reg[Rb]+S*Reg[Ri]]
where:
or Address = Imm+Rb+Ri*S
◦ Imm: Constant “displacement”
It’s often a “displacement” of 1, 2, 4 or 8 bytes, but can be any constant value
◦ Rb: Base register: Any of the16 integer registers
Since we are dealing with addresses, it’s got to be one of the 8-byte registers
◦ Ri: Index register: Any register except %rsp
Since it’s being added to an 8-byte register, it has to be one as well.
◦ S: Scale: Only 1, 2, 4, or 8 (why these numbers?)
◦ This form is seen often when referencing elements of arrays
◦ DON’T CONFUSE Imm and S!! Imm can be *any* constant
S can only be 1, 2, 4, or 8
One of the bigger mistakes students make!
J. E. Jones
OSU CSE 2421
Imm(Rb,Ri,S) Mem[Imm+ Reg[Rb]+S*Reg[Ri]] or Address = Imm+Rb+Ri*S
int ar1[60];
If %rdx -> start address of array (ar1), (e.g., 0x600400)
%rcx -> index into ar1 (e.g., 15)
movl (%rdx, %rcx, 4), %eax
Read from address (0x600400 + 60 (0x3c) )= 0x60043c and put the 4-byte value in register eax
J. E. Jones
OSU CSE 2421
Most General Form of the address expression Imm(Rb,Ri,S) Mem[Imm+ Reg[Rb]+S*Reg[Ri]]
Special Cases (Rb,Ri)
Mem[Reg[Rb]+Reg[Ri]] Mem[Reg[Rb]+Reg[Ri]+Imm]
0+Rb+Ri*1 Imm+Rb+Ri*1 0+Rb+Ri*S Imm+0+Ri*S
Imm(Rb,Ri)
(Rb,Ri,S)
Imm(,Ri,S) Mem[S*Reg[Ri]+Imm]
or Address = Imm+Rb+Ri*S
Each field has a default value to use if it is missing from the expression above: ◦ Imm:0,Rb:0,Ri:0,S:1
Mem[Reg[Rb]+S*Reg[Ri]]
J. E. Jones
OSU CSE 2421
Examples: These read a value in memory into a register movq 24(%rax,%rcx,4), %rdx
means read 8 bytes from this address: (%rax + 4*%rcx + 24) and store it in %rdx
movl 24(%rax,%rcx,4), %edx
means read 4 bytes from this address: (%rax + 4*%rcx + 24) and store it in %edx
movw 24(%rax,%rcx,4), %dx
means read 2 bytes from this address: (%rax + 4*%rcx + 24) and store it in %dx
movb 24(%rax,%rcx,4), %dl
means read 1 byte from this address: (%rax + 4*%rcx + 24) and store it in %dl
Note that only suffix and destination register size change. Suffix and destination register size (on a read from memory) must match. Address calculations are always 8 bytes.
J. E. Jones
OSU CSE 2421
Examples: These write a register value to a place in memory movq %rdx, 24(%rax,%rcx,4)
means write 8 bytes to this address: (%rax + 4*%rcx + 24) from %rdx
movl %edx, 24(%rax,%rcx,4)
means write 4 bytes to this address: (%rax + 4*%rcx + 24) from %edx
movw %dx, 24(%rax,%rcx,4)
means write 2 bytes to this address: (%rax + 4*%rcx + 24) from %dx
movb %dl, 24(%rax,%rcx,4)
means write 1 byte to this address: (%rax + 4*%rcx + 24) from %dl
Note that only suffix and source register size change. Suffix and source register size (on a write to memory) must match . Address calculations are always 8 bytes.
J. E. Jones
OSU CSE 2421
%rdx 0xf000 %rcx 0x0100
Expression
Address Computation
Address
0x8(%rdx)
(%rdx,%rcx)
(%rdx,%rcx,4)
0x80(,%rdx,2)
J. E. Jones
OSU CSE 2421
%rdx 0xf000 %rcx 0x0100
Expression 0x8(%rdx) (%rdx,%rcx) (%rdx,%rcx,4) 0x80(,%rdx,2)
Address Computation
Address
0xf000 + 0x0008
0xf008
J. E. Jones
OSU CSE 2421
%rdx 0xf000 %rcx 0x0100
Expression 0x8(%rdx) (%rdx,%rcx) (%rdx,%rcx,4) 0x80(,%rdx,2)
Address Computation 0xf000 + 0x0008 0xf000 + 0x100
Address 0xf008 0xf100
J. E. Jones
OSU CSE 2421
%rdx 0xf000 %rcx 0x0100
Expression 0x8(%rdx) (%rdx,%rcx) (%rdx,%rcx,4) 0x80(,%rdx,2)
Address Computation 0xf000 + 0x0008 0xf000 + 0x100 0xf000 + 4*0x100
Address 0xf008 0xf100 0xf400
J. E. Jones
OSU CSE 2421
%rdx 0xf000 %rcx 0x0100
Expression 0x8(%rdx) (%rdx,%rcx) (%rdx,%rcx,4) 0x80(,%rdx,2)
Address Computation 0xf000 + 0x0008 0xf000 + 0x100 0xf000 + 4*0x100 0x0+ 2*0xf000 + 0x80
Address 0xf008 0xf100 0xf400 0x1e080
J. E. Jones