程序代写代做代考 compiler assembly CS2421 Autumn 2013

CS2421 Autumn 2013

CSE 2421
X86-64Assembly Language – Part 2: data movement instructions, memory addressing modes, address calculations

Review
What is the size of a memory address on stdlinux???

Review
What is the size of a memory address on stdlinux???
8 bytes = 64 bits
What is the only suffix should we be using when we are calculating/moving addresses?

Review
What is the size of a memory address on stdlinux???
8 bytes = 64 bits
What is the only suffix should we be using when we are calculating/moving addresses?
q
What size registers should we be using when we are calculating addresses?

Review
What is the size of a memory address on stdlinux???
8 bytes = 64 bits
What is the only suffix should we be using when we are calculating/moving addresses?
q
What size registers should we be using when we are calculating addresses?
%rax,%rbx, %rcx, %rdx, %r12, etc.
Is there ever an exception to this?

Review
What is the size of a memory address on stdlinux???
8 bytes = 64 bits
What is the only suffix should we be using when we are calculating/moving addresses?
q
What size registers should we be using when we are calculating/moving addresses?
%rax,%rbx, %rcx, %rdx, %r12, etc.
Is there ever an exception to this?
Not ever!
(as long as we are working on a 64 bit processor.)

Normal (R) Mem[Reg[R]]
Register R specifies memory address
Aha! Pointer dereferencing in C

movq (%rcx),%rax

Displacement D(R) Mem[Reg[R]+D]
Register R specifies start of memory region
Constant displacement D specifies offset

movq 8(%rbp),%rdx
Simple Memory Addressing Modes

Simple Memory Addressing Modes
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

Simple Memory Addressing Modes
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 #No. must use %rcx
movl (%ecx),%eax
movb (%rax),%al

Simple Memory Addressing Modes
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 #No. must use %rcx
movl (%ecx),%eax #No. must use %rcx
# l suffix and dest
# of %eax is OK
movb (%rax),%al

Simple Memory Addressing Modes
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 #No. must use %rcx
movl (%ecx),%eax #No. must use %rcx
# l suffix and dest
# of %eax is OK
movb (%rax),%al #Yes! Address is 8
#byte reg, suffix #and dest are 1 byte

Example of Simple Addressing Modes
void swap(long *xp, long *yp){
long t0;
long t1;
t0 = *xp;
t1 = *yp;
*xp = t1;
*yp = t0;
}
swap:
movq (%rdi), %rax
movq (%rsi), %rdx
movq %rdx, (%rdi)
movq %rax, (%rsi)
ret
C language Program
Equivalent x86-64 Program

Example of Simple Addressing Modes
void swap(int *xp, int *yp){
int t0;
int t1;
t0 = *xp;
t1 = *yp;
*xp = t1;
*yp = t0;
}
swap:
movl (%rdi), %eax
movl (%rsi), %edx
movl %edx, (%rdi)
movl %eax, (%rsi)
ret
C language Program
Equivalent x86-64 Program

Complete Memory Addressing Modes
See Figure 3.3 page 181
Most General Form of the address expression
Imm(Rb,Ri,S) Mem[Imm+ Reg[Rb]+S*Reg[Ri]]
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 64 bit integer registers
Ri: Index register: Any 64 bit register except %rsp or %rbp
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

Complete Memory Addressing Modes
See Figure 3.3 page 181
Most General Form of the address expression
Imm(Rb,Ri,S) Mem[Imm+ Reg[Rb]+S*Reg[Ri]]
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
Special Cases
(Rb,Ri) Mem[Reg[Rb]+Reg[Ri]] 0+Rb+Ri*1
Imm(Rb,Ri) Mem[Reg[Rb]+Reg[Ri]+Imm] Imm+Rb+Ri*1
(Rb,Ri,S) Mem[Reg[Rb]+S*Reg[Ri]] 0+Rb+Ri*S
Imm(,Ri,S) Mem[S*Reg[Ri]+Imm] Imm+0+Ri*S

Complete Memory Addressing Modes

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.

Complete Memory Addressing Modes

Examples: These read a value in memory into a register
leaq 24(%rax,%rcx,4), %rdx
means compute 8-byte value : (%rax + 4*%rcx + 24)
and store it in %rdx
leal 24(%eax,%ecx,4), %edx
means compute 4-bytes value: (%eax + 4*%ecx + 24)
and store it in %edx
leaw 24(%ax,%cx,4), %dx
means compute 2 bytes value: (%ax + 4*%cx + 24)
and store it in %dx

Complete Memory Addressing Modes

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.

Expression Address Computation Address
0x8(%rdx)
(%rdx,%rcx)
(%rdx,%rcx,4)
0x80(,%rdx,2)

Address Computation Examples
Expression Address Computation Address
0x8(%rdx)
(%rdx,%rcx)
(%rdx,%rcx,4)
0x80(,%rdx,2)

%rdx 0xf000
%rcx 0x0100

19

Expression Address Computation Address
0x8(%rdx)
(%rdx,%rcx)
(%rdx,%rcx,4)
0x80(,%rdx,2)

Address Computation Examples
Expression Address Computation Address
0x8(%rdx) 0xf000 + 0x8 0xf008
(%rdx,%rcx)
(%rdx,%rcx,4)
0x80(,%rdx,2)

%rdx 0xf000
%rcx 0x0100

20

Expression Address Computation Address
0x8(%rdx)
(%rdx,%rcx)
(%rdx,%rcx,4)
0x80(,%rdx,2)

Address Computation Examples
Expression Address Computation Address
0x8(%rdx) 0xf000 + 0x8 0xf008
(%rdx,%rcx) 0xf000 + 0x100 0xf100
(%rdx,%rcx,4)
0x80(,%rdx,2)

%rdx 0xf000
%rcx 0x0100

21

Expression Address Computation Address
0x8(%rdx)
(%rdx,%rcx)
(%rdx,%rcx,4)
0x80(,%rdx,2)

Address Computation Examples
Expression Address Computation Address
0x8(%rdx) 0xf000 + 0x8 0xf008
(%rdx,%rcx) 0xf000 + 0x100 0xf100
(%rdx,%rcx,4) 0xf000 + 4*0x100 0xf400
0x80(,%rdx,2)

%rdx 0xf000
%rcx 0x0100

22

Expression Address Computation Address
0x8(%rdx)
(%rdx,%rcx)
(%rdx,%rcx,4)
0x80(,%rdx,2)

Address Computation Examples
Expression Address Computation Address
0x8(%rdx) 0xf000 + 0x8 0xf008
(%rdx,%rcx) 0xf000 + 0x100 0xf100
(%rdx,%rcx,4) 0xf000 + 4*0x100 0xf400
0x80(,%rdx,2) 2*0xf000 + 0x80 0x1e080

%rdx 0xf000
%rcx 0x0100

23

Address computations
What if we want to compute an address, but not necessarily access that address just yet?
What if we want to compute an address and put it in an 8-byte register to use later?
What if we just want to compute a formula of the form:
x + k*y + C, where x & y are variables; k =1,2,4,or 8, and C is some constant
There’s a way to do this with an x86-64 instruction, but unless you are paying attention, you will confuse it with the mov instruction.

Address Computation Instruction
leaq Src, Dst
Load Effective Address
Src is address mode expression (i.e. Imm(Rb,Ri,S) )
Set Dst to address denoted by expression (since suffix is q, Dst MUST be an 8 byte register)
Doesn’t affect condition codes
http://stackoverflow.com/questions/1658294/whats-the-purpose-of-the-lea-instruction
Uses
Computing addresses without a memory reference
E.g., translation of p = &x[i];
Computing arithmetic expressions of the form x + k*y + C,
where x & y are variables;
k = 1, 2, 4, or 8
and C is some constant
e. g. if %rdx contains a value x, then leaq 7(%rdx, %rdx,4), %rax sets %rax to 5x+7
Example

3x * 4 = 12x
long m12(long x)
{
return x*12;
}
leaq (%rdi,%rdi,2), %rax # t = x+x*2
salq $2, %rax # return t<<2 Converted to ASM by compiler: IMPORTANT! Address Computation Instruction leaq Src, Dst Load Effective Address Src is address mode expression (i.e. Imm(Rb,Ri,S) ) Set Dst to address denoted by expression (since suffix is q, Dst MUST be an 8 byte register) Doesn’t affect condition codes http://stackoverflow.com/questions/1658294/whats-the-purpose-of-the-lea-instruction Uses Computing addresses without a memory reference E.g., translation of p = &x[i]; Computing arithmetic expressions of the form x + k*y k = 1, 2, 4, or 8 e. g. if %rdx contains a value x, then leaq 7(%rdx, %rdx,4), %rax sets %rax to 5x+7 Example 3x * 4 = 12x long m12(long x) { return x*12; } leaq (%rdi,%rdi,2), %rax # t = x+x*2 salq $2, %rax # return t<<2 Converted to ASM by compiler: Using a screwdriver as a hammer!!  Computations with leaq Consider: leaq (%rdi, %rdi,1), %rax => %rdi + 1*%rdi = 2%rdi
leaq (%rdi, %rdi,2), %rax => %rdi + 2*%rdi = 3%rdi
leaq (%rdi, %rdi,4), %rax => %rdi + 4*%rdi = 5%rdi
leaq (%rdi, %rdi,8), %rax => %rdi + 8*%rdi = 9%rdi
What kind of multiplication problems can you come up with that might make these valuable?
leaq(%rdi, %rdi,2), %rax # 3%rdi
leaq(%rdi, %rdi,8), %rbx # 9%rdi
addq %rbx, %rax # 12%rdi

Computations with leaq
Since we know that leaq Imm(Rb,Ri,S), %Rd calculates Imm + Rb + Ri*S and puts the result in %Rd

If %rdx contains some value x and %rcx contains some value y, then what value does %rax contain in these examples? (in terms of x & y)
leaq (%rdx, %rcx), %rax
leaq (%rdx, %rcx,4), %rax
leaq 5(%rdx, %rdx,4), %rax
leaq 7(%rcx, %rcx,2), %rax
leaq 10(%rdx, %rcx,8), %rax
leaq 0x4000(,%rdx, 8), %rax

Computations with leaq
Since we know that leaq Imm(Rb,Ri,S), %Rd calculates Imm + Rb + Ri*S and puts the result in %Rd

If %rdx contains some value x and %rcx contains some value y, then what value does %rax contain in these examples? (in terms of x & y)
leaq (%rdx, %rcx), %rax x + y
leaq (%rdx, %rcx,4), %rax
leaq 5(%rdx, %rdx,4), %rax
leaq 7(%rcx, %rcx,2), %rax
leaq 10(%rdx, %rcx,8), %rax
leaq 0x4000(,%rdx, 8), %rax

Computations with leaq
Since we know that leaq Imm(Rb,Ri,S), %Rd calculates Imm + Rb + Ri*S and puts the result in %Rd

If %rdx contains some value x and %rcx contains some value y, then what value does %rax contain in these examples? (in terms of x & y)
leaq (%rdx, %rcx), %rax x + y
leaq (%rdx, %rcx,4), %rax x + 4y
leaq 5(%rdx, %rdx,4), %rax
leaq 7(%rcx, %rcx,2), %rax
leaq 10(%rdx, %rcx,8), %rax
leaq 0x4000(,%rdx, 8), %rax

Computations with leaq
Since we know that leaq Imm(Rb,Ri,S), %Rd calculates Imm + Rb + Ri*S and puts the result in %Rd

If %rdx contains some value x and %rcx contains some value y, then what value does %rax contain in these examples? (in terms of x & y)
leaq (%rdx, %rcx), %rax x + y
leaq (%rdx, %rcx,4), %rax x + 4y
leaq 5(%rdx, %rdx,4), %rax 5 + x + 4x = 5x + 5
leaq 7(%rcx, %rcx,2), %rax
leaq 10(%rdx, %rcx,8), %rax
leaq 0x4000(,%rdx, 8), %rax

Computations with leaq
Since we know that leaq Imm(Rb,Ri,S), %Rd calculates Imm + Rb + Ri*S and puts the result in %Rd

If %rdx contains some value x and %rcx contains some value y, then what value does %rax contain in these examples? (in terms of x & y)
leaq (%rdx, %rcx), %rax x + y
leaq (%rdx, %rcx,4), %rax x + 4y
leaq 5(%rdx, %rdx,4), %rax 5 + x + 4x = 5x + 5
leaq 7(%rcx, %rcx,2), %rax 7 + y + 2y = 3y + 7
leaq 10(%rdx, %rcx,8), %rax
leaq 0x4000(,%rdx, 8), %rax

Computations with leaq
Since we know that leaq Imm(Rb,Ri,S), %Rd calculates Imm + Rb + Ri*S and puts the result in %Rd

If %rdx contains some value x and %rcx contains some value y, then what value does %rax contain in these examples? (in terms of x & y)
leaq (%rdx, %rcx), %rax x + y
leaq (%rdx, %rcx,4), %rax x + 4y
leaq 5(%rdx, %rdx,4), %rax 5 + x + 4x = 5x + 5
leaq 7(%rcx, %rcx,2), %rax 7 + y + 2y = 3y + 7
leaq 10(%rdx, %rcx,8), %rax 10 + x + 8y = x + 8y + 10
leaq 0x4000(,%rdx, 8), %rax

Computations with leaq
Since we know that leaq Imm(Rb,Ri,S), %Rd calculates Imm + Rb + Ri*S and puts the result in %Rd

If %rdx contains some value x and %rcx contains some value y, then what value does %rax contain in these examples? (in terms of x & y)
leaq (%rdx, %rcx), %rax x + y
leaq (%rdx, %rcx,4), %rax x + 4y
leaq 5(%rdx, %rdx,4), %rax 5 + x + 4x = 5x + 5
leaq 7(%rcx, %rcx,2), %rax 7 + y + 2y = 3y + 7
leaq 10(%rdx, %rcx,8), %rax 10 + x + 8y = x + 8y + 10
leaq 0x4000(,%rdx, 8), %rax 0x4000 + 8x

/docProps/thumbnail.jpeg