You are on page 1of 4

Assign. 1.

Consider the following declarations:

Fill in the following table describing the element size, the total size, and the address of element i for each
of these arrays.

Array Element size Total size Start address Element i


P 4 bytes 32 bytes X_P X_P + i*4
Q 2 bytes 4 bytes X_Q X_Q + i*2
R 8 bytes 80 bytes X_R X_R + i*8
S 8 bytes 80 bytes X_S X_S + i*8
T 8 bytes 16 bytes X_T X_T + i*8

Assign. 2.

Suppose x p, the address of short integer array P, and long integer index i are stored in registers %rdx and
%rcx, respectively. For each of the following expressions, give its type, a formula for its value, and an
assembly-code implementation. The result should be stored in register %rax if it is a pointer and register
element %ax if it has data type short.

Expression Type Value Assembly code


P[1] short M[xp+2] movw 2(%rdx),%rax
P+3+i short * xp+6+2i leaq 6(%rdx,%rcx,2),%rax
P[i * 6 - 5] short M[xp+12i-10] movw -10(%rdx,%rcx,12),%ax
P[2] short M[xp+4] movw 4(%rdx),%ax
&P[i + 2] short * xp+2i+4 leaq 4(%rdx,%rcx,2),%rax

Assign. 3.
Consider the following source code, where M and N are constants declared with

In compiling this program, gcc generates the following assembly code:


Use your reverse engineering skills to determine the values of M and N based on this assembly code.

 Answer
By examining the code, we observe that matrix P is accessed at byte offset 8 using the formula (8i
+ j), while matrix Q is accessed at byte offset 8 using the formula (6j + i). Based on this analysis,
we can conclude that matrix P has 8 columns, whereas matrix Q has 6 columns. Therefore, we
have M = 6 and N = 8.

Assign. 4.
Given following C code

/* Compute i,k of fixed matrix product */


#define N 8
typedef int fix_matrix[N][N];
int fix_prod_ele (fix_matrix A, fix_matrix B, long i, long k) {
long j;
int result = 0;
for (j = 0; j < N; j++)
result += A[i][j] * B[j][k];
return result;
}

When compiled with optimization level -O1, gcc generates the following assembly code:
#A in %rdi, B in %rsi, i in %rdx, k in %rcx
fix_prod_ele:
.LFB25:
.cfi_startproc
endbr64
salq $5, %rdx # Multiply i by 32 (shift left by 5 bits)
addq %rdx, %rdi # Calculate address of A[i][0]
leaq (%rsi,%rcx,4), %rdx # Calculate address of B[0][k]
leaq 32(%rdi), %rsi # Calculate address of A[i+1][0]
movl $0, %ecx # Initialize result to 0
.L5:
movl (%rdi), %eax # Load A[i][j]
imull (%rdx), %eax # Multiply by B[j][k]
addl %eax, %ecx # Add to result
addq $4, %rdi # Increment the address of A[i][j] by 4
addq $32, %rdx # Increment the address of B[j][k] by 32
cmpq %rsi, %rdi # Check if we reached the end of the row
jne .L5 # If not, loop again
movl %ecx, %eax # Move result to %eax
ret
.cfi_endproc
Add comments for every line of above assembly code to explain the purpose of each line.
Assign. 5.
Consider the following structure declaration:

The following procedure (with some expressions omitted) operates on this structure:
void st_init(struct test *st) {
st->s.y = _________;
st->p = __________;
st->next = __________;
}
A. What are the offsets (in bytes) of the following fields: p, s.x, s.y, next?
B. How many total bytes does the structure require?
C. The compiler generates the following assembly code for st_init:

On the basis of this information, fill in the missing expressions in the code for st_init.
 Answer
A. Offsets:
p: 0 bytes
s.x: 8 bytes
s.y: 10 bytes
next: 12 bytes
B.It uses 20 bytes.
C.The C code as follows:
void st_init(struct test *st)
{
st->s.y = st->s.x;
st->p = &(st->s.y);
st->next = st;
}

You might also like