Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider the following source code, where NR and NC are macro expressions declar

ID: 3786077 • Letter: C

Question

Consider the following source code, where NR and NC are macro expressions declared with #define that compute the dimensions of array A in terms of parameter n. This code computes the sum of the elements of column j of the array.

1    long sum_col (long n, long A[NR(n) ] [NC(n) ] , long j) {

2             long I;

3             long result =0;

4             for (i = 0; i < NR(n); i++)

5                 result += A[i] [j];

6             return result;

7    }

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

    long sum_col (long n, long A [NR(n) ] [NC(n) ], long j)

    n in %rdi, A in %rsi, j in %rdx

1 sum_col:

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           compq %rdi , %rdx

16           jne         .L3

17           rep ; ret

18 .L4:

19           mol        $0 , &eax

20           ret

Use your reverse engineering skills to determine the definitions of NR and NC.

Explanation / Answer

NR(n) gives the number of rows in the array which also decides the range of values for array index .

NC(n) is the size of the offset value for increment which is size of type * number of columns which is size of long * n in the above case .

These are used for memory allocation by the compiler and return a pointer to the start of the memory called base address . For row major arrangement :