#Assembly Code

7 messages · Page 1 of 1 (latest)

limpid lanceBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

valid mist
#

!f

limpid lanceBOT
#

So i have questions about this problem:

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;
    }
}

and im given the following assembly code for the inner loop:

.L6 : movq(% rdx), % rcx movq(% rax), % rsi movq % rsi, (% rdx) movq % rcx,
    (% rax) addq $8, % rdx addq $120, % rax cmpq % rdi, % rax jne.L6

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
I know the answers but I dont understand them.

shengzhu
valid mist
#

I know the answers but I don't understand them
Provide these answers and why you don't understand them.

Also probably better suited for #asm-arch-osdev

wide linden
# limpid lance So i have questions about this problem: The following code transposes the eleme...

when you look at the first 4 instructions, you can see that it's a standard swap:

(rdx) -> rcx -> (rax)
and at the same time:
(rax) -> rsi -> (rdx)

rdx and rax hold pointers to things, and the contents those pointers are pointing to are swapped. looking at the latter instructions, you can see that one pointer rax is incremented by 8, while the other pointer rdx is incremented by 120. since rax is incremented by a smaller amount, it must be the term where the least significant index is incrememted A[i][j] and rdx must be the term where the most significant index is incremented A[j][i]

#

the distance between A[i][j] and A[i][j+1] is sizeof(long) whereas the distance between A[j][i] and A[j+1][i] is M*sizeof(long)

limpid lanceBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.