Professional Documents
Culture Documents
Fill in the following table describing the element size, the total size, and the address of element i for each
of these arrays.
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.
Assign. 3.
Consider the following source code, where M and N are constants declared with
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
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;
}