CSCI 2122 - Systems Programming - A4


  • 作业标题:CSCI 2122 - Systems Programming - A4
  • 课程名称:Dalhouse University CSCI 2122 Systems Programming
  • 完成周期:2天

1. Description

(1) The following code transposes the elements of an M × M array, where M is a constant defined by # define:

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

When compiled, with optimization level −O1, GCC generates the following code
for the inner loop of the function.

1 .L1:
2 movq (%rdx), %rcx
3 movq (%rax), %rsi
4 movq %rsi, (%rdx)
5 movq %rcx, (%rax)
6 addq $8, %rdx
7 addq $120, %rax
8 cmpq %rdi, %rax
9 jne .L1

We can see that GCC has converted the array indexing to pointer code.

(a) Which register holds a pointer to array element A[i][j]?

(b) Which register holds a pointer to array element A[j][i]?

(c) What is the value of M?

(2) (a) The assembly code of the table below 2c was generated by compiling the C
code of Table 1.

(b) Find the expression for the unknown macro expressions (A) and (B) appearing
in Table 1. [Hint: they are both only in terms of variable ‘n’.]

(c) Identify the line numbers in the assembly code that correspond to the following
operations from the C code:

  • long result = 0;
  • result += A[i][j];
  • i < NR(n);

Assembly code for mystery function. Note that the line numbers in the first
column have no functional value, but are supplied to help you answer the problem

1. col sum:
2. leaq 1(,%rdi, 4), %r8
3. leaq (%rdi, %rdi, 2), %rax
4. movq %rax, %rdi
5. testq %rax, %rax
6. jle .L4
7. salq $3, %r8
8. leaq (%rsi, %rdx, 8), %rcx
9. movl $0, %eax
10. movl $0, %edx
11. .L3
12. addq (%rcx), %rax
13. addq $1, %rdx
14. addq %r8, %rcx
15. cmpq %rdi, %rdx
16. jne .L3
17. ret
18. .L4
19. movl $0, %eax
20. ret

Table 1. C code for column-sum function. (A) and (B) represent the unknown definitions for the macro expressions that you are required to find.

#define NR(n) ( (A) )
#define NC(n) ( (B) )

long col sum(long n, long A[NR(n)][NC(n)], long j)
{
long i;
long result = 0;
for (i = 0; i < NR(n); i++)
result += A[i][j];
return result;
}

文章作者: IT神助攻
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 IT神助攻 !
  目录