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.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339
Rep:
Quote:
Originally Posted by MTK358
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.
"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.
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
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
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
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).
3) Using standard library calls for I/O rather than direct requests to the Linux kernel.
Good, but how?
Quote:
Originally Posted by johnsfine
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
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
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
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.
3) Using standard library calls for I/O rather than direct requests to the Linux kernel.
Quote:
Originally Posted by MTK358
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
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
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.