CS计算机代考程序代写 assembler compiler PowerPoint Presentation

PowerPoint Presentation

CSE 2421
Structure Memory Access Computations

Review
leaq
Load effective address, a variant of movq
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 value x, then
leaq 3(%rdx, %rdx, 4), %rax
will set register %rax to 5x +3

Data Alignment
We looked at data alignment yesterday
X86-64 hardware will work correctly regardless of the alignment of data
However, Intel recommends that data be aligned to improve memory system performance
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…

Accessing struct members in X86-64
struct members are usually 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.

Example:
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?

Example 1
struct S1 {
short m1; 2 byte; p+0
short m2; 2 byte; p+2 60 bytes/
int m3; 4 bytes; p+4 structure aligns on
int m4; 4 bytes; p+8 4-byte boundary
int m5[12]; 48 bytes; p+12
} S1_array[200];

How would we access S1_array[i].m4 if %rdi contains $S1_Array and %rsi contains i?

Example 1
struct S1 {
short m1; 2 byte; p+0
short m2; 2 byte; p+2
int m3; 4 bytes; p+4
int m4; 4 bytes; p+8
int m5[12]; 48 bytes; p+12
} 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
leaq 8(%rdi, %rsi), %rcx # $S1_Array +(60*i) + 8
movl (%rcx), %eax # read int from memory to %eax

Example 1
struct S1 {
short m1; 2 byte; p+0
short m2; 2 byte; p+2
int m3; 4 bytes; p+4
int m4; 4 bytes; p+8
int m5[12]; 48 bytes; p+12
} 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
leaq 8(%rdi, %rsi), %rcx # $S1_Array +(60*i) + 8
movl (%rcx), %eax # read int from memory to %eax

movl 8(%rdi, %rsi), %eax

Example 1
struct S1 {
short m1; 2 byte; p+0
short m2; 2 byte; p+2
int m3; 4 bytes; p+4
int m4; 4 bytes; p+8
int m5[12]; 48 bytes; p+12
} S1_array[200];

How would we access S1_array[i].m4 if %rdi contains $S1_Array and %rsi contains i?

imulq $60, %rsi
movl 8(%rdi, %rsi), %eax

Can we do it without using imulq?
60 factors to (2 * 2 * 3 * 5). I think we can use that!

leaq (%rsi, %rsi, 2), %rax # 3*%rsi = 3*i
leaq (%rax, %rax, 4), %rax # 5*(3*%rsi) = 15*i
shlq $2, %rax # 4*(5*3*%rsi) = 60*i
movl 8(%rdi, %rax), %eax # read int from (%rdi + (5*3*2*2*%rsi) + 8)

movl 8(%rdi, %rax, 4), %eax

Example 1
struct S1 {
short m1; 2 byte; p+0
short m2; 2 byte; p+2
int m3; 4 bytes; p+4
int m4; 4 bytes; p+8
int m5[12]; 48 bytes; p+12
} S1_array[200];

leaq (%rsi, %rsi,2), %rax # 3*%rsi
leaq (%rax, %rax,4), %rax # 5*3*%rsi This calculates
shlq $2, %rax # 4*(5*3*%rsi) S1_array[i].m4
movl 8(%rdi, %rax), %eax # read int from (%rdi + (5*3*2*2*%rsi) + 8)

How would we access S1_array[i].m5[2]?

Example 1
struct S1 {
short m1; 2 byte; p+0
short m2; 2 byte; p+2
int m3; 4 bytes; p+4
int m4; 4 bytes; p+8
int m5[12]; 48 bytes; p+12
} S1_array[200];

leaq (%rsi, %rsi,2), %rax # 3*%rsi
leaq (%rax, %rax,4), %rax # 5*3*%rsi This calculates
shlq $2, %rax # 4*(5*3*%rsi) S1_array[i].m4
movl 8(%rdi, %rax), %eax # read int from (%rdi + (5*3*2*2*%rsi) + 8)

How would we access S1_array[i].m5[2]?
p+12 => m5[0]
p+16 => m5[1]
p+20 => m5[2]

Example 1
struct S1 {
short m1; 2 byte; p+0
short m2; 2 byte; p+2
int m3; 4 bytes; p+4
int m4; 4 bytes; p+8
int m5[12]; 48 bytes; p+12
} S1_array[200];

leaq (%rsi, %rsi,2), %rax # 3*%rsi
leaq (%rax, %rax,4), %rax # 5*3*%rsi This calculates
shlq $2, %rax # 4*(5*3*%rsi) S1_array[i].m4
movl 8(%rdi, %rax), %ecx # read int from (%rdi + (5*3*2*2*%rsi) + 8)

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

Example 1
struct S1 {
short m1; 2 byte; p+0
short m2; 2 byte; p+2
int m3; 4 bytes; p+4
int m4; 4 bytes; p+8
int m5[12]; 48 bytes; p+12
} S1_array[200];

How would we access S1_array[i].m5[j], if %rdx contains j?

Example 1
struct S1 {
short m1; 2 byte; p+0
short m2; 2 byte; p+2
int m3; 4 bytes; p+4
int m4; 4 bytes; p+8
int m5[12]; 48 bytes; p+12
} S1_array[200];

How would we access S1_array[i].m5[j], if %rdx contains j? Option 1
leaq (%rsi, %rsi,2), %rax # 3*i
leaq (%rax, %rax,4), %rax # 5*(3*i)
shlq $2, %rax # 4*(5*3*i)
leaq 12(%rdi, %rax,1), %rax # %rax = &S1_array[i].m5[0]
shlq $2, %rdx # j*4
movl (%rax, %rdx ), %eax # read from (&S1_array[i].m5[0]+ j*4)

Example 1
struct S1 {
short m1; 2 byte; p+0
short m2; 2 byte; p+2
int m3; 4 bytes; p+4
int m4; 4 bytes; p+8
int m5[12]; 48 bytes; p+12
} S1_array[200];

How would we access S1_array[i].m5[j], if %rdx contains j? Option 2
leaq (%rsi, %rsi,2), %rax # 3*i
leaq (%rax, %rax,4), %rax # 5*(3*i)
shlq $2, %rax # 4*(5*3*i)
leaq 12(%rdi, %rdx,4), %rdi # &S1_array[0].m5[j]
movl (%rdi, %rax ), %eax # read from (&S1_array[i].m5[j])

/docProps/thumbnail.jpeg