程序代写代做代考 assembly C compiler X86-64Assembly Language – Part 2: data movement instructions, memory addressing modes, address calculations

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.)

Simple Memory Addressing Modes
— 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
— 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
movb (%rax),%al
# l suffix and dest # of %eax is OK

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
#No. must use %rcx #No. must use %rcx # l suffix and dest
# of %eax is OK #Yes! Address is 8
#byte reg, suffix #and dest are 1 byte

Example of Simple Addressing Modes
C language Program
Equivalent x86-64 Program
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

Example of Simple Addressing Modes
C language Program
Equivalent x86-64 Program
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

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:
— Ri:
— S:
— This form is seen often when referencing elements of arrays
Base register: Any of the16 64 bit integer registers
Index register: Any 64 bit register except %rsp or %rbp Scale: Only 1, 2, 4, or 8 (why these numbers?)
— 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]] 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]
Mem[Reg[Rb]+S*Reg[Ri]]

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.

Address Computation Examples
%rdx
0xf000
%rcx
0x0100
Expression
Address Computation
Address
0x8((%%rdrxd)x)
(%rddxx,%,r%crxc)x)
(%rddxx,%,r%crxc,4x),4)
0x800(,(%,r%drx,d2x),2)

Address Computation Examples
%rdx
0xf000
%rcx
0x0100
Expression
Address Computation
Address
0x8((%%rdrxd)x)
0xf000 + 0x8
0xf008
(%rddxx,%,r%crxc)x)
(%rddxx,%,r%crxc,4x),4)
0x800(,(%,r%drx,d2x),2)

Address Computation Examples
%rdx
0xf000
%rcx
0x0100
Expression
Address Computation
Address
0x8((%%rdrxd)x)
0xf000 + 0x8
0xf008
(%rddxx,%,r%crxc)x)
0xf000 + 0x100
0xf100
(%rddxx,%,r%crxc,4x),4)
0x800(,(%,r%drx,d2x),2)

Address Computation Examples
%rdx
0xf000
%rcx
0x0100
Expression
Address Computation
Address
0x8((%%rdrxd)x)
0xf000 + 0x8
0xf008
(%rddxx,%,r%crxc)x)
0xf000 + 0x100
0xf100
(%rddxx,%,r%crxc,4x),4)
0xf000 + 4*0x100
0xf400
0x800(,(%,r%drx,d2x),2)

Address Computation Examples
%rdx
0xf000
%rcx
0x0100
Expression
Address Computation
Address
0x8((%%rdrxd)x)
0xf000 + 0x8
0xf008
(%rddxx,%,r%crxc)x)
0xf000 + 0x100
0xf100
(%rddxx,%,r%crxc,4x),4)
0xf000 + 4*0x100
0xf400
0x800(,(%,r%drx,d2x),2)
2*0xf000 + 0x80
0x1e080

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
IMPORTANT!
— Example
Converted to ASM by compiler:
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 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 Using a screwdriver as a hammer!!J — Example Converted to ASM by compiler: 3x * 4 = 12x — 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 long m12(long x) { return x*12; } leaq (%rdi,%rdi,2), %rax # t = x+x*2 salq $2, %rax # return t<<2 Computations with leaq — Consider: leaq (%rdi, %rdi,1), %rax leaq (%rdi, %rdi,2), %rax leaq (%rdi, %rdi,4), %rax leaq (%rdi, %rdi,8), %rax => =>
=>
=>
%rdi + 1*%rdi = 2%rdi %rdi + 2*%rdi = 3%rdi
%rdi + 4*%rdi = 5%rdi
%rdi + 8*%rdi = 9%rdi
can you come up with that might
# 3%rdi # 9%rdi # 12%rdi
— What kind of multiplication problems make these valuable?
leaq(%rdi, %rdi,2), %rax leaq(%rdi, %rdi,8), %rbx addq %rbx, %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
— 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
— 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
x + y

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
x + y x + 4y

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
x + y
x + 4y
5 + x + 4x = 5x + 5

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
x + y
x + 4y
5 + x + 4x = 5x + 5 7 + y + 2y = 3y + 7

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
x + y
x + 4y
5 + x + 4x = 5x + 5
7 + y + 2y = 3y + 7
10 + x + 8y = x + 8y + 10

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
x + y
x + 4y
5 + x + 4x = 5x + 5
7 + y + 2y = 3y + 7
10 + x + 8y = x + 8y + 10 0x4000 + 8x