137k views
4 votes
The following code transposes the elements of an M × M array, where M is a

constant defined by #define:
void transpose(Marray_t A) {
int i, j;
for (i = 0; i < M; i++)
for (j = 0; j < i; j++) {
int t = A[i][j];
A[i][j] = A[j][i];
A[j][i] = t;
}
}
When compiled with optimization level -O2, gcc generates the following code for
the inner loop of the function:
1 .L3:
2 movl (%ebx), %eax
3 movl (%esi,%ecx,4), %edx
4 movl %eax, (%esi,%ecx,4)
5 addl $1, %ecx
6 movl %edx, (%ebx)
7 addl $52, %ebx
8 cmpl %edi, %ecx
9 jl .L3
A. What is the value of M?
B. What registers hold program values i and j?
C. Write a C code version of transpose that makes use of the optimizations
that occur in this loop. Use the parameter M in your code rather than numeric
constants.

User Abhinava
by
9.0k points

1 Answer

7 votes

Final answer:

The code does not specify the value of M. i is held in the %ecx register and j is held in the %ebx register. Here is a C code version of transpose that uses the optimizations of the provided loop.

Step-by-step explanation:

A. The value of M is not given in the code provided. It is represented by the constant M in the code snippet and is defined by #define. The value of M should be specified elsewhere in the code.

B. In the code snippet, the program values i and j are held in the registers %ecx and %ebx, respectively.

C. Here is a C code version of transpose that makes use of the optimizations that occur in the provided loop:

void transpose(Marray_t A, int M) {
int i, j;
for (i = 0; i < M; i++) {
for (j = 0; j < i; j++) {
int t = A[i][j];
A[i][j] = A[j][i];
A[j][i] = t;
}
}
}

User Davidahines
by
8.2k points