LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-17-2009, 04:09 AM   #1
WilliamR
LQ Newbie
 
Registered: Dec 2009
Posts: 2

Rep: Reputation: 0
Could use some help understanding assembly


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?
 
Old 12-17-2009, 06:45 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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?
 
Old 12-17-2009, 07:56 AM   #3
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by WilliamR View Post
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.

Last edited by johnsfine; 12-17-2009 at 07:57 AM.
 
Old 12-17-2009, 11:23 AM   #4
WilliamR
LQ Newbie
 
Registered: Dec 2009
Posts: 2

Original Poster
Rep: Reputation: 0
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


So could someone explain how those work for me?
 
1 members found this post helpful.
Old 12-17-2009, 11:38 AM   #5
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by WilliamR View Post
movl %esp, %ebp // point %esp to the %ebp location
GAS uses source,destination sequence, so the above copies the contents of the esp register to the ebp register.

Quote:
movl 8(%ebp), %edx // move %edx to location 8 from %ebp
As with the previous instruction, you have source and destination backwards.

Quote:
leal (%eax,%edx,8), %ecx // this kinda throws me off. I believe it means that %ecx = %eax + 8*%edx
Right (it didn't throw you off). Once you figured that one out, applying the same logic to everything else should have given you the whole answer.


Quote:
This is where I get completely lost.

movl array2(,%eax,4), %eax
1) Compute the address array2 plus 4 times the contents of the eax register.
2) copy the value stored at that address into the eax register.

Last edited by johnsfine; 12-17-2009 at 11:40 AM.
 
1 members found this post helpful.
Old 12-17-2009, 12:22 PM   #6
scurveedog
LQ Newbie
 
Registered: May 2009
Posts: 4

Rep: Reputation: 0
Wow WilliamR,

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 hope you really enjoy asm programming

scurveedog
 
Old 12-17-2009, 12:58 PM   #7
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by scurveedog View Post
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.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Practical Assembly: Help Understanding gcc -S for simple.c? jhwilliams Programming 2 11-30-2009 05:30 PM
understanding DOS vs Linux assembly language shortname Programming 10 03-07-2007 10:04 PM
assembly ? blackzone Programming 3 10-15-2004 02:36 AM
Assembly jinksys Programming 3 09-14-2003 04:33 PM
assembly genghis Programming 2 06-12-2003 07:46 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration