LinuxQuestions.org
Review your favorite Linux distribution.
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 03-07-2010, 11:57 AM   #91
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723

I don't understand.

Also, why do all the commands end in 'i' or 'q'?
 
Old 03-07-2010, 12:04 PM   #92
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
Originally Posted by MTK358 View Post
I don't understand.

Also, why do all the commands end in 'i' or 'q'?
I think movq is an old pentium MMX instruction.
BTW that is 64 bit code and you/the OP did not even state you had a 64 bit.
AFAIK More then half do not have a 64bit OS.
 
Old 03-07-2010, 12:10 PM   #93
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Didn't I say that I have 64-bit Linux in my OP?
 
Old 03-07-2010, 12:19 PM   #94
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,665
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
"ASM coding" might be interesting to a very very small number of engineers, but for the vast majority, it's a task that has been relegated to the compiler-writers (and the low level OS-kernel junkies).

Assembly code, these days, is found within "C" and even "C++" programs. Nearly all of the source-code doesn't consist of ASM: only the tiny portion that absolutely must do so.

I can count on the fingers of one hand the times when I have justifiably made the effort to re-code a particular "hot spot" routine in assembler, such that it made a human-measurable difference in the execution of the code. And it did, and I knew this to be true from actual emperical tests, which is the only reason why I did it. Before making the effort, I carefully hand-coded the routines in the high-level language of choice just to make sure. The routines in question were all of about fifty assembler instructions long when finished, and they were hot spots.

When you code such a "hot spot," you simply need to know how the compiler you are using will handle its entry-and-exit linkages. You also need to know what compiler options must be specified and how to write your source-code so that the program will "drop dead," instead of compiling, if it should be compiled with the wrong features or the wrong compiler. Such source code is not transportable, so you should design it such that "hot spot" assembly routines are optional, and the programming will work equally well without it.
 
Old 03-07-2010, 12:32 PM   #95
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
I was saying that this is mostly for fun and learning than for serious coding.
 
Old 03-07-2010, 02:27 PM   #96
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 MTK358 View Post
I don't understand.
Don't understand what?

Many posts ago, you posted an incorrect example in 32 bit x86 asm of code to raise an interger to an integer power:
http://www.linuxquestions.org/questi...76#post3887176
So I took that same task (integer to an integer power) for an example of how I suggest learning asm:
1) 64 bit rather than 32 bit
2) Function rather than whole program
3) Using standard library calls for I/O rather than direct requests to the Linux kernel.

Quote:
Also, why do all the commands end in 'i' or 'q'?
That is 'l' not 'i'.

Opcodes end in b, w, l or q to represent the size of the operation: 8, 16, 32 or 64 bits.

A few opcodes don't end in one of those because the operation doesn't have that kind of size and/or the size is implied and doesn't need to be specified.

Quote:
Originally Posted by MTK358 View Post
Didn't I say that I have 64-bit Linux in my OP?
Yes, but you can't expect people to read/remember everything said in a thread this long.

Quote:
Originally Posted by smeezekitty View Post
I think movq is an old pentium MMX instruction.
BTW that is 64 bit code and
I see you understand we are talking about 64 bit code. I think you also are aware we are giving examples in gnu asm syntax, not Intel. So please realize that most of what you know from 16 bit asm in Intel syntax will not be correct in 64 bit gnu asm syntax. I think you are confusing the OP or any other beginner reading the thread with incorrect (for the current context) statements, such as that one about MMX.

Quote:
Originally Posted by MTK358 View Post
I was saying that this is mostly for fun and learning than for serious coding.
I was well aware of that (so I don't think sundialsvcs's comment, however correct, was relevant).

But I would still think that learning asm as you would learn it if it were going to be useful, would be more fun and certainly better learning than focusing on more inherently useless aspects of the topic.

Certainly, the example I gave would not be useful. As long as you are going to call printf with the result, no time or space gain from brilliantly hand tweaked asm code (vs. what a compiler would create from C code) could possible be even a noticeable fraction of the total. Also, I wasn't trying to write the best possible hand tweaked code.

But at least the concepts demonstrated by that example are concepts relevant to serious asm coding. (And I think more importantly, concepts relevant to using knowledge of asm to improve your ability to code and debug in C).

Last edited by johnsfine; 03-07-2010 at 02:54 PM.
 
Old 03-07-2010, 03:13 PM   #97
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by johnsfine View Post
Don't understand what?

Many posts ago, you posted an incorrect example in 32 bit x86 asm of code to raise an interger to an integer power:
http://www.linuxquestions.org/questi...76#post3887176
Yes.

Quote:
Originally Posted by johnsfine View Post
1) 64 bit rather than 32 bit
That's what I want.

Quote:
Originally Posted by johnsfine View Post
3) Using standard library calls for I/O rather than direct requests to the Linux kernel.
Good, but how?

Quote:
Originally Posted by johnsfine View Post
That is 'l' not 'i'.
I know, I am posting some posts practicing ad different keyboard layout so I might be more prone to typos.

Quote:
Originally Posted by johnsfine View Post
Opcodes end in b, w, l or q to represent the size of the operation: 8, 16, 32 or 64 bits.

A few opcodes don't end in one of those because the operation doesn't have that kind of size and/or the size is implied and doesn't need to be specified.
OK, but what do the letters stand for? I'm guessing something like "byte", "word", "long", and "quadruple".

Quote:
Originally Posted by johnsfine View Post
Yes, but you can't expect people to read/remember everything said in a thread this long.
So I should say that I use 64-bit Linux on every post? that's just silly.

Quote:
Originally Posted by johnsfine View Post
I see you understand we are talking about 64 bit code. I think you also are aware we are giving examples in gnu asm syntax, not Intel. So please realize that most of what you know from 16 bit asm in Intel syntax will not be correct in 64 bit gnu asm syntax. I think you are confusing the OP or any other beginner reading the thread with incorrect (for the current context) statements, such as that one about MMX.
Yes, I assume all exapmles here are 64-bit GNU assembler, 32-bit if stated so.
 
Old 03-07-2010, 03:49 PM   #98
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
Originally Posted by johnsfine View Post
3) Using standard library calls for I/O rather than direct requests to the Linux kernel.
Good, but how?
Code:
movl 'C',%eax
pushl %eax
call _putchar
 
Old 03-07-2010, 04:02 PM   #99
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by smeezekitty View Post
Code:
movl 'C',%eax
pushl %eax
call _putchar
OK, but do you have to "include" stdio in some way to do this?
 
Old 03-07-2010, 04:11 PM   #100
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
Originally Posted by MTK358 View Post
OK, but do you have to "include" stdio in some way to do this?
No, you have to link with the standard libraries.
You may have to put
Code:
.def _putchar
At the end of the cod but i am not sure.
 
Old 03-07-2010, 04:16 PM   #101
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
And how do you link it against stdio?
 
Old 03-07-2010, 05:03 PM   #102
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 johnsfine View Post
3) Using standard library calls for I/O rather than direct requests to the Linux kernel.
Quote:
Originally Posted by MTK358 View Post
Good, but how?
You can see how I used printf from asm code in post #90 of this thread. So that is an example of how. What I described there should be enough to tell you how to call any standard library function passing any combination of up to six integer or pointer parameters. That should be enough for a fair amount of practice. Try writing a few functions. Passing floats and other data types and/or more than six integers/pointers isn't very hard either, but keep the amount you learn before practicing more manageable.

Quote:
Originally Posted by MTK358 View Post
And how do you link it against stdio?
I used
gcc test.c foo.s
and let gcc figure out how to link against standard libraries. It worked.

If you want to know more (actual ld command used) there are swiches to get gcc to tell you what commands (including the ld command) it used.

Quote:
Originally Posted by smeezekitty View Post
You may have to put
Code:
.def _putchar
At the end of the cod but i am not sure.
I'm actually not sure myself why you don't need to declare printf as an external symbol in my example .s file to get as to understand it. I've done asm in too many different environments to remember those environment specific details.

But I think Smeezekitty is still giving you advice based on some other environment.

Last edited by johnsfine; 03-07-2010 at 05:15 PM.
 
Old 03-07-2010, 05:06 PM   #103
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
smeezekitty showed me how to do that.

I am asking if there are any special instructions for assembling/linking?
 
Old 03-07-2010, 05:22 PM   #104
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 MTK358 View Post
smeezekitty showed me how to do that.
I edited my own post after you wrote the above, so I lost track of what you mean by "that".

But I think whatever Smeezekitty showed you at best applies to 32 bit x86 not 64 bit x86 and more likely doesn't even get 32 bit x86 right.

Quote:
I am asking if there are any special instructions for assembling/linking?
I thought I had that explained well in my previous post even before I edited it:

I usually let GCC figure all that out for me. Even when you write in asm, you can use gcc commands. GCC recognizes the .s for asm code and invokes the assembler for you. It also invokes the linker for you.

If you want to know the as and ld commands to issue yourself, either read a lot of as and ld documentation, or use GCC once to find out what commands it would use, even if you don't intend to use it later.

Last edited by johnsfine; 03-07-2010 at 05:49 PM.
 
Old 03-07-2010, 05:25 PM   #105
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
I am asking if there are any special instructions for assembling/linking?
Quote:
Originally Posted by johnsfine
I used
gcc test.c foo.s
and let gcc figure out how to link against standard libraries. It worked.
@johnsfine
Quote:
But I think Smeezekitty is still giving you advice based on some other environment.
i am talking about GNU assembler.
 
  


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
ASM or C++? Hb_Kai Programming 16 01-20-2010 09:12 AM
Is ASM dangerous? MrCode Programming 37 11-18-2009 08:29 AM
ASM x32 vs ASM x64 Tegramon Programming 3 02-27-2008 02:26 PM
I/O in ASM Mercurius Programming 10 11-16-2006 07:02 PM
ASM question zWaR Programming 2 06-26-2004 11:42 AM

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

All times are GMT -5. The time now is 08:42 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