J.E.Jones
OSU CSE 2421
OSU CSE 2421
leaX Imm(Base, Index, Scale), Dest
◦ Load effective address, a variant of movX
◦ Has form of an instruction that reads from memory to a register, but it does not reference memory at all.
◦ Instruction is used to generate addresses for later memory references
◦ Compilers often find other “clever” uses that have nothing to do with address computations
◦ If %rdx contains the 8-byte value x, then leaq 3(%rdx, %rdx, 4), %rax
will set register %rax to 5x +3
◦ lea instruction is valid with the ‘q’ suffix and the ‘l’ suffix only.
J. E. Jones
OSU CSE 2421
◦ If %rdx contains the 8-byte value x, then leaq 3(%rdx, %rdx, 4), %rax
will set register %rax to 5x +3
◦ If %edx contains the 4-byte value x, then leal 3(%edx, %edx, 4), %eax
will set register %eax to 5x +3
J. E. Jones
OSU CSE 2421
◦ If %rdx contains the 8-byte value x, then leaq 3(%rdx, %rdx, 2), %rax
will set register %rax to 5x +3
◦ If %edx contains the 4-byte value x, then leal 3(%edx, %edx, 4), %eax
will set register %eax to 5x +3
Can’t be used for address calculations but can be used for other computations.’
J. E. Jones
OSU CSE 2421
We looked at data alignment last class
x86-64 hardware will work correctly regardless of the
alignment of data
However, Intel recommends that data be aligned to improve memory system performance
In addition, when working with mixed C/x86 language programs, data alignment must be used because C compiler does.
Given this (and potential changes in policy as processors upgrade to 128-bit processors in the future) data alignment is implemented within the compilers/assemblers as follows…
J. E. Jones
OSU CSE 2421
struct members are accessed by using a pointer to the first byte of the struct and a displacement.
This is one of the common uses of displacements.
See the example below for an illustration of how this is done.
J. E. Jones
OSU CSE 2421
struct S1 { short m1;
short m2; int m3;
int m4;
int m5[12];
} S1_array[200];
How would we access S1_array[i].m4 if %rdi contains $S1_array and %rsi contains i?
J. E. Jones
OSU CSE 2421
Step 1: determine offset of each member from beginning of the structure and size of the structure
struct S1 { short m1;
2 byte; p+0
2 byte; p+2
4 bytes; p+4
4 bytes; p+8 48 bytes; p+12
short m2; int m3;
int m4;
int m5[12];
60 bytes/
structure aligns on 4-byte boundary
} S1_array[200];
How would we access S1_array[i].m4 if %rdi contains $S1_Array and %rsi contains i?
J. E. Jones
OSU CSE 2421
struct S1 {
short m1;
2 byte; p+0
2 byte; p+2
4 bytes; p+4
4 bytes; p+8 48 bytes; p+12
short m2; int m3;
int m4;
int m5[12];
60 bytes/ structure aligns on 4-byte boundary
} S1_array[200];
How would we access S1_array[i].m4 if %rdi contains $S1_Array and %rsi contains i?
imulq $60, %rsi
leaq 8(%rdi, %rsi), %rcx movl (%rcx), %eax
# 60 * i
# $S1_Array +(60*i) + 8 =&(S1_array[i].m4) # read int from memory to %eax
J. E. Jones
OSU CSE 2421
struct S1 {
short m1;
2 byte; p+0
2 byte; p+2
4 bytes; p+4
4 bytes; p+8 48 bytes; p+12
imulq $60, %rsi
# 60 * i
# $S1_Array +(60*i) + 8 = &(S1_array[i]) # read int from memory to %eax
short m2; int m3;
int m4;
int m5[12];
60 bytes/ structure aligns on 4-byte boundary
} S1_array[200];
How would we access S1_array[i].m4 if %rdi contains $S1_Array and %rsi contains i?
leaq 8(%rdi, %rsi), %rcx movl (%rcx), %eax
movl 8(%rdi, %rsi), %eax
J. E. Jones
OSU CSE 2421
struct S1 {
movl 8(%rdi, %rsi), %eax
short m1; short m2; int m3;
int m4;
int m5[12];
2 byte; p+0
2 byte; p+2
4 bytes; p+4
4 bytes; p+8 48 bytes; p+12
60 bytes/ structure aligns on 4-byte boundary
} S1_array[200];
How would we access S1_array[i].m4 if %rdi contains $S1_Array and %rsi contains i?
imulq $60, %rsi # 60 * i
Can we do it without using imulq? Option 1: 60 factors to (2 * 2 * 3 * 5).
I think we can use that!
leaq (%rsi, %rsi, 2), %rax leaq (%rax, %rax, 4), %rax shlq $2, %rax
movl 8(%rdi, %rax), %eax
# 3*%rsi = 3*i
# 5*(3*%rsi) = 15*i
# 4*(5*3*%rsi) = 60*i
# read int from (%rdi + (5*3*2*2*%rsi) + 8)
movl 8(%rdi, %rax, 4), %eax
J. E. Jones
OSU CSE 2421
struct S1 {
movl 8(%rdi, %rsi), %eax
short m1; short m2; int m3;
int m4;
int m5[12];
2 byte; p+0
2 byte; p+2
4 bytes; p+4
4 bytes; p+8 48 bytes; p+12
60 bytes/ structure aligns on 4-byte boundary
} S1_array[200];
How would we access S1_array[i].m4 if %rdi contains $S1_Array and %rsi contains i?
imulq $60, %rsi # 60 * i
Can we do it without using imulq?
Option 2: 60 = 0x3C = 0b 0011 1100, so n=5, m=2 Remember (x<<(n+1)) – (x<
p+16 => m5[1]
p+20 => m5[2]
p+24 => m5[3]… p+56 -> m5[11]
J. E. Jones
OSU CSE 2421
struct S1 {
} S1_array[200];
leaq (%rsi, %rsi,2), %rax leaq (%rax, %rax,4), %rax shlq $2, %rax
movl 8(%rdi, %rax), %eax
# 3*%rsi
# 5*3*%rsi
# 4*(5*3*%rsi)
# read int from (%rdi + (5*3*2*2*%rsi) + 8)
This calculates S1_array[i].m4
short m1; short m2; int m3;
int m4;
int m5[12];
2 byte; p+0
2 byte; p+2
4 bytes; p+4
4 bytes; p+8 48 bytes; p+12
How would we access S1_array[i].m5[2]? p+12 => m5[0]
p+16 => m5[1] p+20 => m5[2]
movl 20(%rdi, %rax), %ecx
J. E. Jones
OSU CSE 2421
struct S1 {
} S1_array[200];
short m1; short m2; int m3;
int m4;
int m5[12];
2 byte; p+0
2 byte; p+2
4 bytes; p+4
4 bytes; p+8 48 bytes; p+12
How would we access S1_array[i].m5[j], if %rdx contains j?
J. E. Jones
OSU CSE 2421
struct S1 {
} S1_array[200];
short m1; short m2; int m3;
int m4;
int m5[12];
2 byte; p+0
2 byte; p+2
4 bytes; p+4
4 bytes; p+8 48 bytes; p+12
How would we access S1_array[i].m5[j], if %rdx contains j? Option 1
leaq (%rsi, %rsi,2), %rax leaq (%rax, %rax,4), %rax shlq $2, %rax
leaq 12(%rdi, %rax,1), %rax shlq $2, %rdx
# 3*i
# 5*(3*i)
# 4*(5*3*i)
# %rax = &S1_array[i].m5[0]
# j*4
# read from (&S1_array[i].m5[0]+ j*4)
movl (%rax, %rdx), %eax
J. E. Jones
OSU CSE 2421
struct S1 {
} S1_array[200];
short m1; short m2; int m3;
int m4;
int m5[12];
2 byte; p+0
2 byte; p+2
4 bytes; p+4
4 bytes; p+8 48 bytes; p+12
How would we access S1_array[i].m5[j], if %rdx contains j? Option 2
leaq (%rsi, %rsi,2), %rax leaq (%rax, %rax,4), %rax shlq $2, %rax
leaq 12(%rdi, %rdx,4), %rdi movl (%rdi, %rax ), %eax
# 3*i
# 5*(3*i)
# 4*(5*3*i)
# &S1_array[0].m5[j]
# read from (&S1_array[i].m5[j])
J. E. Jones
OSU CSE 2421
35?? 5*7 —-5*(5+2)
leaq (%rsi, %rsi, 4), %rax
shlq $1, %rsi
addq %rax, %rsi
leaq (%rsi,%rsi,4), %rax
movb 30(%rdi, %rax), %r8b
#i*5
#i*2
# 5i+2i =7i #7i*5
J. E. Jones
OSU CSE 2421
19 = (18 + 1) = (2*3*3) +1 leaq (%rsi, %rsi, 2), %rax leaq (%rax, %rax, 2), %rax shlq $1, %rax
#i*3
#i*9
#2(i*9) – i*18 # i*18 + i = 19i
addq %rsi, %rax
leaq (%rdi, %rax), %rax
J. E. Jones
OSU CSE 2421
struct S57{
char ar1[50];
57 = 60-3
char ar2[7]; }S57_ar[300};
leaq (%rsi, %rsi,2), %rax movq %rax, %r8
leaq (%rax, %rax,4), %rax shlq $2, %rax
# 3*i
#copy %rax # 5*(3*i)
# 4*(5*3*i)
subq %r8, %rax
J. E. Jones
OSU CSE 2421
struct S43 { char a[40];
char b[3]; }S43_array[200];
40 + 3? (2*2*2*5) + 3
leaq (%rsi, %rsi, 4), %rax #5*i
shlq $3, %rax
leaq (%rsi, %rsi, 2), %rsi addq %rax, %rsi
leaq (%rdi, %rsi), %rax
# 8*(5*i) = 40*i #3*i
# 43*i
J. E. Jones
OSU CSE 2421
23 = 24 -1 = (3*2*2*2) -1
leaq (%rsi, %rsi, 2), %rax leaq (%rax, %rax, 8), %rax subq %rsi, %rax
leaq (%rdi, %rax), %rax
#i*3
#i*24
# i*24 – i = 23i
J. E. Jones
OSU CSE 2421
47 = 48 – 1 = (2*3*2*2*2) – 1
leaq (%rsi, %rsi, 2), %rax shlq $1, %rax
shlq $3,%rax
subq %rsi, %rax
#i*3
#6i
#(8(6*i)) = 48i # i*48 – i = 47i
leaq (%rdi, %rax), %rax
J. E. Jones
OSU CSE 2421
50 5*5*2
leaq (%rsi, %rsi, 4), %rax #5*i
leaq (%rax, %rax, 4), %rax #5*5*i leaq (%rdi, %rax, 2), %rax
movl 8(%rdi, %rax), %r9d
J. E. Jones