ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I got this homework assignment and I've spent the past few hours trying to understand it but I'm getting nowhere. Could someone explain this to me?
Code:
Consider the source code below, where M and N are constants declared with #define.
unsigned int array1[M][N];
unsigned int array2[N][M];
unsigned int copy(unsigned int i, unsigned int j)
{
array1[i][j] = array2[j][i];
}
Suppose the above code (when compiled) generates the following assembly code:
copy:
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %edx
movl 12(%ebp), %eax
leal (%eax,%edx,8), %ecx
sall $5, %eax
addl %edx, %eax
movl array2(,%eax,4), %eax
movl %eax, array1(,%ecx,4)
popl %ebp
ret
What are the values of M and N?
I assume that you have some kind of book or reference that you are using in the course----to explain the assembler syntax. Specifically, what is the notation "%ebp, %esp, etc.?"
Regardless, what is it that you don't understand? Do you know what the C code does?
I got this homework assignment and I've spent the past few hours trying to understand it but I'm getting nowhere.
You're not supposed to ask us to do your homework for you, and we're not supposed to do it if you ask.
There are several things you need to understand in order to do that assignment. I'm not sure which of these are topics of your current course (what you should be learning now) vs. which are prerequisites (what you should have learned in introductory courses).
1) The meaning of the C code.
2) The C ABI for 32 bit x86 (how parameters are passed etc.)
3) The 32 bit x86 assembly language (what the instructions and operand modes mean).
4) The GAS assembler syntax (how registers and operand modes are represented, the fact that the basic operand sequence is source,destination etc.).
This example is simple enough you could guess at enough of 2 and 4 to answer the question if you had a good grasp of 1 and 3. You could even guess at 1 with some though and an understanding of just 3.
If you have some specific questions inside one of those topics, I expect someone here will help you.
Many of us know what M and N are from just a quick glance as the asm code. But I hope no one will tell you. You're supposed to learn something.
If you have significant experience in other assembly languages, you should be able to learn enough of (3) by skimming through the instruction set documentation on Intel or AMD web sites to solve the whole question. Otherwise, you need to actually read whatever materials were assigned by the instructor.
I wasn't really asking what M and N were, more so how I would go about finding them. I understand the C code no problem but the asm is confusing. My book doesn't explain it very well and neither did my teacher.
This is what I get from it(could be completely wrong)
copy:
pushl %ebp // push %ebp onto stack
movl %esp, %ebp // point %esp to the %ebp location
movl 8(%ebp), %edx // move %edx to location 8 from %ebp
movl 12(%ebp), %eax // move %eax to location 12 from %ebp
leal (%eax,%edx,8), %ecx // this kinda throws me off. I believe it means that %ecx = %eax + 8*%edx
sall $5, %eax // shift %eax 5 bits to the left. %eax = 32*%eax
addl %edx, %eax // add value at %edx to %eax. %32*eax = 32*%eax + %edx
This is where I get completely lost.
movl array2(,%eax,4), %eax
movl %eax, array1(,%ecx,4)
popl %ebp
ret
Johnsfine has given you correct information and an explanation of the code (which is what you asked for, I don't see that you asked for an answer to your homework ?). I think that he read your posted homework question -which does ask for M and N- and he mistakenly thought...
Anyway, if this is a misunderstanding I hope it doesn't discourage you from posting questions to this site. It's a great site with many VERY knowledgeable people participating, johnsfine being one of them.
There is a book titled "Professional Assembly Language" by Richard Blum that was put out by Wrox at the time. This book really helped me and is my favorite on *nix assembly.
On the net you can find "Programming from the ground up" by Jonathan Bartlett. Its free in PDF format and is also a very good place to start.
I think that he read your posted homework question -which does ask for M and N- and he mistakenly thought...
It doesn't matter whether William wanted the homework answer or whether he just wanted a blanket "explain this" referring to the entire assignment. Neither is an acceptable way to ask for homework help in this forum.
Notice the difference in William's second post. There he tried to explain each line of the asm code. Effort always counts, so just trying changed things. But more importantly, it showed which parts of the problem required explanation (mainly GAS syntax issues). That is the right way to ask for homework help.
Quote:
I hope it doesn't discourage you from posting questions to this site.
I have told a lot of people in this forum that they asked a question badly. I don't know a way of saying that, which doesn't sound negative and at least slightly rude. But it is never my intention to discourage them from asking questions, just to guide them in asking questions in a way that will bring better answers.
Some people are insulted and/or discouraged by my criticisms of their questions. You can't please everyone. If William had been insulted or discouraged, I expect he wouldn't have asked the question better the second time.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.