CS计算机代考程序代写 compiler OSU CSE 2421

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 contains start address of array (ar1), (e.g., 0x600400)
 %rcx contains index into ar1 (e.g., 15)
15* 4
 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
Address = Imm+Rb+Ri*S
Expression
Address Computation
Address
0x8(%rdx)
(%rdx,%rcx)
(%rdx,%rcx,4)
0x80(,%rdx,2)
J. E. Jones

OSU CSE 2421
%rdx 0xf000 %rcx 0x0100
Address = Imm+Rb+Ri*S
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
Address = Imm+Rb+Ri*S
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
Address = Imm+Rb+Ri*S
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
Address = Imm+Rb+Ri*S
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

OSU CSE 2421
 What if we want to compute an address, but not necessarily access value at 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,wherex&yarevariables;k=1,2,4,or8andC
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.
J. E. Jones

OSU CSE 2421

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,or8
 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
long m12(long x) {
Converted to ASM by compiler:
return x*12; }
leaq (%rdi,%rdi,2), %rax # t = x+x*2 =3x salq $2, %rax 3x * 4 = 12x # return t<<2 IMPORTANT! J. E. Jones OSU CSE 2421  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, Using a screwdriver as a hammer!!  wherex&yarevariables;  k=1,2,4,or8  andCissomeconstant  e. g. if %rdx contains a value x, then leaq 7(%rdx, %rdx,4), %rax sets %rax to 5x+7  Example long m12(long x) { Converted to ASM by compiler: return x*12; } leaq (%rdi,%rdi,2), %rax salq $2, %rax # t = x+x*2=3x # return t<<2 3x * 4 = 12x salq instruction == shlq instruction IMPORTANT! J. E. Jones OSU CSE 2421  Examples: These compute functions of the form Rb+ S*Ri + C 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 J. E. Jones OSU CSE 2421  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 J. E. Jones OSU CSE 2421  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 J. E. Jones OSU CSE 2421  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 J. E. Jones OSU CSE 2421  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 J. E. Jones OSU CSE 2421  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 J. E. Jones OSU CSE 2421  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 ◦ leaq5(%rdx,%rdx,4),%rax ◦ leaq7(%rcx,%rcx,2),%rax ◦ leaq10(%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 J. E. Jones OSU CSE 2421  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 ◦ leaq5(%rdx,%rdx,4),%rax ◦ leaq7(%rcx,%rcx,2),%rax ◦ leaq10(%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 J. E. Jones OSU CSE 2421  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 create that might make these valuable?
leaq(%rdi, %rdi,2), %rax leaq(%rdi, %rdi,8), %rbx addq %rbx, %rax
subq %rax, %rbx
# 3%rdi # 9%rdi # 12%rdi # 6%rdi
J. E. Jones

OSU CSE 2421
 Complete the leaq Carmen assignment no later than 15 minutes after class end.
 Late submissions/ 0 points
 15 minutes allocated
◦ 2 minutes * 3 = 6 minutes – most students
◦ 15 minutes allocated so 1.5x and 2.0x extended time students are included
◦ If you were working with us through last few slides, it’s a piece of cake
J. E. Jones