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.
Doesn't rely on any OS. For a given hardware architecture, it will run without a requirement for any particular OS. In retrospect, 'agnostic' is probably the wrong choice of word.
As far as using assembler to write C functions, I guess that may be useful, if only as an academic exercise. I can't say it is never done, but from where I see things, it is extremely uncommon. Using assembler to write low-level components of OSs like interrupt service routines seems to be a bit of a large bite for a newbie to chew on. It does have a practical aspect.
I am not advocating that the PC architecture is something one should learn, however that the use of assembler does go hand in hand with programming to bare metal. Any bare metal, and the concepts are largely transferable across architectures. Old PCs are well documented, widely understood, easily and cheaply acquired, relatively easy to use, and quite consistent in makeup. These things contribute to eligibility as a trainer platform. If the learner has the opportunity to poke around with a scope or other test gear, and accidentally releases the magic smoke, very little will be lost.
As far as using assembler to write C functions, I guess that may be useful, if only as an academic exercise. I can't say it is never done, but from where I see things, it is extremely uncommon.
Any non "academic exercise" use of asm is extremely uncommon.
I expect real professional (or serious open source) use of asm for C callable functions is currently written more often and by more software engineers than asm for OS's.
Professionally, I have written an entire OS in PDP11 asm, and a different entire OS in 16-bit x86 asm, and a different entire OS in mixed 32-bit x86 asm and C. I have written asm functions callable from high level code in a wide variety of architectures including all three bit sizes of X86.
My hobbyist use of asm has been far more widely varied.
My advice is based on having worked on asm in all the different forms that have been discussed. It is very much not the usual "this is how I did it, so I suggest others do the same". I did this every way, and I am sure about my suggestions of which way would be best for a beginner today.
Well, x86 segment registers are a source of genuine disgust. So I'd suggest some other architecture.
If you had read the rest of my post, you would realize that this response is totally superfluous. When you use the "tiny" model your program is totally contained within a 64 K space and the segment registers are not used.
With NASM you can compile straight to a binary file which is executable in DOS(Box).
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,233
Rep:
Quote:
Originally Posted by Sergei Steshenko
Well, I do not quite agree with that. For example, for an interrupt service routine one needs to save registers on stack (if we are talking about a stack machine), and then to restore the registers at the routine exit. High level languages do not have CPU register notion ("C" "register" keyword doesn't count - it's something different).
The amount of code which needs to be written in assembly is really tiny compared to the whole kernel, but still the code is necessary.
i agree with that, even the linux kernel has some bits of assembly code for each architecture with which it is compatible, in fact it's that assmebly code that in large part IS responsible for compatiblity with said architecture.
Actually, I would retract my assertion about using assembler to write C callable (or any other language callable) functions, as I've actually done that many times. However, I have never done it in the context of a host capable of running a real OS (DOS isn't one of those, in this context), and I doubt that it has much place there.
For microcontroller class CPUs (16-bit and smaller), it is almost exclusively the method I favor, but the functions tend to get very close to the hardware in those cases. For bigger hardware, I cannot see any really practical use for assembler at the application programming level. I don't think the OP is likely to be doing OS-internal level programming as a beginner.
Clearly we have all used assembler for differnt purposes. I confess that seeing anything that I have not done may seem somewhat off-normal to me. mea culpa
The OP could probably help himself/herself by explaining what purpose is behind wanting to learn assembler.
If you had read the rest of my post, you would realize that this response is totally superfluous. When you use the "tiny" model your program is totally contained within a 64 K space and the segment registers are not used.
With NASM you can compile straight to a binary file which is executable in DOS(Box).
The whole notion of tiny, and other (do I remember correctly that there are two more of them - large and huge ?) memory model is an unnecessary complication for a beginner.
And, of course, there is that nasty PAE extension on 32 bits ...
NASM doesn't use those memory models. It's just a straight source to executable compilation without any other complications. The only time you use segment registers is when you want to access more than 64K addressable space (unlikely for a beginner). Nothing could be simpler.
Wow so many responses thanks for all the help! It's great that some of you provided documentation sources which is really helpful.
I'm leaning against x86 assembly since it seems more standard and common in computers. Would like to write anything useful like a driver, audio decoder, porting software, or anything that may give me assembly know-how on x86 computers (and x86_64 assuming they're almost the same).
Based on reading and google searches many programmers refer to high level languages (python or something new for instance) so assembly guides aren't always convincing; only exception is: http://en.wikibooks.org/wiki/X86_Assembly . It'd be great to hear more on how some of you began assembly (especially x86) and got familiar with it.
That has a lot of good content in it. But it also has a lot of distracting legacy content. I'm not sure a beginner would know which parts are useful.
Also, addressing modes are a very important topic in asm. The addressing modes section of that book is hard to find and nearly useless.
Some other sections of the book show both Intel syntax and gas syntax, and present the information either specific to 32-bit or general enough to infer most of the 16-bit, 32-bit and 64-bit possibilities. The addressing modes section is just 16-bit. But addressing modes is an area where there are maximum differences between 16-bit and 32-bit as well as significant differences between 32-bit and 64-bit.
I learned "real mode" 16-bit x86 when it wasn't called that because it was the only x86 asm that existed. As each new extension was added to the x86 family of CPU's, I learned that. But I am quite sure learning the obsolete stuff first is not a good path for a beginner today.
I already knew many different asm architectures before the 8086 was ever invented. So I never had any need for any book teaching any version or aspect of x86. Intel and AMD published reference manuals and for me those were sufficient for learning each kind of x86 asm.
The AMD reference manuals (free downloadable pdf files) are still the best source for information on x86-64. I think a smart enough beginner could learn using those as the primary resource and never need a book that teaches x86 asm. A good introductory book would be a great help if it exists, but I've only seen bad introductory books.
I don't have any stable URL's for those AMD reference manuals. But I just tried a google search for "support.amd.com/us/Processor_TechDocs/" which seems to give a list of pdf's you can get from that directory (the directory listing is password protected, so you can get files from there only if you know the file name). Most of those pdf files are specialized content mainly for BIOS writers.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.