69.2k views
4 votes
Given a following C program, where M and N are constant declared with #define long P[M][N]; long Q[N][M]; long sum_element(long i, long j) { return P[i][j] + Q[j][i]; } In compiling this program, gcc generates the following assembly code: long sum_element(long i, long j) sum_element: leaq 0(,%rdi,8), %rdx subq %rdi, %rdx addq %rsi, %rdx leaq (%rsi,%rsi,4), %rax addq %rax, %rdi movq Q(,%rdi,8), %rax addq P(,%rdx,8), %rax ret What are the values of M and N?

User Uentee
by
6.1k points

1 Answer

6 votes

Answer:

Check the explanation

Step-by-step explanation:

The first, second and third instruction together set %rdx = 3*%rdi + %rsi. The fourth and fifth instruction set %rdi = %rdi+9*%rsi.

Now %rdi is used to address Q, while %rdx is used to address P. Note that P is addressed by column and then by row. Hence M = 3 . Also Q is addressed by row and then column, hence N = 9 .

User Mardoz
by
5.3k points