DevX10
DevX10

Reputation: 505

C and Assembly Quiz

I'm trying to understand better how the assembly x86 64 works. And the best way is solving some exercises, I've tried to solve some but now I'm currently stuck understanding this one:

enter image description here

The main operation here is addl mat(%rdx, %rcx), %eax. It's the same as sum += mat[y][x]. By taking a look at the assembly I'm pretty sure that %rdx is x and %rcx is y.

So to find out what MAT_X is we can see how y behaves. And in this case we increment y by 172 each end of the first loop until we reach 2924.

This means that MAT_X = 2924/172 = 17 (Is the reasoning correct ?). And for the other I would have said MAT_Y = 10, since the x loops from 0 to 10 each time.

The solution for this problem that MAT_X = 17 but MAT_Y = 172/4 = 43.

But I don't understand why actually, where I'm I wrong?

Upvotes: 1

Views: 534

Answers (1)

Al Kepp
Al Kepp

Reputation: 5980

Stay calm, you are close. :-)

You can see number 2924 at the end of program. It is the size of the whole array. In other words it's like MAT_X * MAT_Y = 2924. But this value is in bytes! C/C++ languages count everything in variable size, this time it is array of int, i.e. its size is 2924 bytes = 2924/4 = 731 int's.

172 is added at the end of each line, and this number is size of last dimension of the array, i.e. the basis of MAT_Y. But again, it is in bytes in assembler, so you need to count MAT_Y = 172/4 = 43.

Let's verify it:

MAT_X = total size / MAT_Y = 731/43 = 17
MAT_X * MAT_Y * 4 = 17*43*4 = 2924

Upvotes: 3

Related Questions