Learning x86 for Non-x86 Assembler Programmers?
An anonymous reader asks: "I've done assembler for the 6809, 68000, 8085, MIPS and ARM architectures over the years. But - I've never learned assembler for the most common architecture out there. I would like to change that. I can roughly follow my way around x86 disassemblies, but I'm not as good at optimizing/fine tuning bits of assembler because I am not intimately familiar with all of the addressing modes etc. I would like a book that is targetted at people like me. I would like to be able to fine tune, say a blitter in x86 assembler. One thing I do not in a book is something that is trying to teach me assembler programming in general. Most assembler books seem to fall in the latter category. Are there books out there that might prove useful to me?"
It's simple! Just use a semi-intelligent program to generate random opcodes, and see which ones compile, and which ones actually run!
Joke aside, what about checking Intel's pages? I seem to remember there were quite a few documents there on assembly programming...
A website that could come in handy for learning about x86 assembly language is DDJ Microprocessor Center. In specific, the On-line Intel Documentation links are almost invaluable when learning to code for the x86 architecture. Being Intel reference manuals, they tend to cut to the case relativly quickly.
Assembly on a proccessor that runs 2.5GHz? Isn't that a bit like calculating the trajectory of every piece of dust in a desert in a five mile radius of a nuclear blast, by hand?
Asm is great to understand, which you already do, and it's essential for certain applications... modern x86 proccessors, however... you don't build modern jets with slide rules "Standard" measurement units... you get caught in the details and lose sight of the project.
I'm as mimsy as the next borogove but your mome raths are completely outgrabe.
How about the book that was reviewed here on Wednesday?
Thats a great suggestion, all the low level documenation is downloadable from Intel's site in PDF format.
You can also request a free CD with everything you'd need on it.
AMD also have documentation up on there site and I think the x86-64 documentation is also available for free on a CD.
thank God the internet isn't a human right.
I would not recommend anyone to optimize modern x86 asm by hand. If you know your way around disassembled code you know enough to find any (rare) compiler mistakes. Any other operation is usually done better by a compiler. (Please don't yell at me with small, hand optimized special cases, compilers do a good job today if your application isn't very special.)
If you would try to hand optimize asm code for a modern cpu you must concider many issues, among them the reordering of instruction in the processor, the different layouts of the pipelines in different processor models (even intels differ to other intels), cache effects (I suppose that you must link everything statically and control where in memory your code will end up)...
You must also unroll loops, change the access patterns to 2D data structures to improve cache performance, avoid inner loop data dependencies, etc. It is simply too much to handle by hand.
As you probably know a higher level language such as C/C++ and don't write a highly optimized operating system (or work without an OS) you do not need, and should not want to, optimize your asm code by hand!
Is equivelent to a 250Mhz processor with 128MB of ram, running only code writen in ASM.
ASM can often make applications hundreds of time faster so you 2.5Ghz processor running Joes bloaty crapware is like my P100 running ollies carefully crafted and optimized assembler.
thank God the internet isn't a human right.
Shh! It's a secret, but Intel offers 4 very nice books at a great price: free.
They aren't tutorials, so there isn't the same hand-holding that you would get in a book from Barnes & Noble, but they explain things well enough that a seasoned assembly programmer should be able to follow with no problem at all. I think they are exactly what you want.
Time flies like an arrow. Fruit flies like a banana.
Yeah! Soup nazi!
:-)
An old time favorite
I'm surprised no one has mentioned this book already, because it's exactly what you're looking for. The only problem is that it's dated - it considers the 80386 to be a new processor. There was a time when no self-respecting assembly programmer would be caught dead without it. Alas, I sold mine a couple years ago, since I already learned everything I could from it.
The only problem is that it (like all of Abrash's books) has been out of print for a long time, and so it will be very hard to find.
And the men who hold high places must be the ones who start
To mold a new reality... closer to the heart
Most recent printing would be in Graphics programming black book, but that too has lapsed out of print.
Slashdot article a while ago linked to a download of the complete text, ie Slashdot, but doesn't seem to link there anymore, perhaps someone else would have an idea where the find the download.
/* TODO: Spawn child process, interest child in technology, have child write a new sig */
So you've been doing assembly for well-designed modern architectures and now you want to learn it for that bit of kludgery we call "x86"? My advice is to run your brain through a kitchen blender and then pour it back into your head. The x86 architecture may start making sense after that.
Sheesh, evil *and* a jerk. -- Jade
Someone mentioned "Zen of Assembly Programming" by Michael Abrash. I can recommend "Zen of Code Optimization", also by Michael Abrash. The latter book might be more to your taste, since it focuses on the fine-tuning rather than assembly language itself.
However, the book is somewhat outdated since it only includes processors up to the Pentium I w/o MMX.
On the other hand, today's processors are so advanced at doing their own optimization (out-of-order execution, branch prediction, R-OPs, and whatnot), and the different processors do it so differently (AMD vs. Intel) that hand-optimization has far less impact than in the olden days. So you might want to consider if you want to do fine-tuning, except for the obvious (unrolling loops, quadword-aligning your data, to name two things). Also, I think a far greater speed-up can be accomplished by optimizing the algorithm you use. Putting some thought into the algorithm can do wonders, and Abrash agrees with me in "Zen of Code Optimization".
HTH,
Michael
If you're in Linuxland, you might find linuxassembly.org helpful. I've done some assembly before (only a semester's worth, though), and the site was rather useful to me. If you've never done x86, though, there might not be enough there for you . . .
Al Qaeda has ninjas!
One assembles assembly with an assembler.
You don't code in "assembler" language, you code in "assembly" language.
May we never see th
"Of course, half of the values in this file violate some part of the ELF standard, and it's a wonder than Linux will even consent to sneeze on it, much less give it a process ID. This is not the sort of program to which one would normally be willing to confess authorship."
LOL. This guy should edit everyone else's documentation. A good quick quide to the mechanics of getting started with ASM.
Webster -- The Place on the Net to Learn Assembly Language Programming
See The Art of Assembly Language Programming
It might not be in print anymore, though I found it invaluable when I had to learn how to debug system BIOSes.
The Visible series not only teaches assembly, it does so from a novice perspective and includes an emulator to try out the new commands.
Your 'fighting chance' is exactly why people should learn ASM so they know whats going on.
Frequently it's hard to see how a complicated algorythm can be optimised, until you hand convert it to ASM.
(there's no need to convert to ASM realy, but it's an easy way to make sure you've been thorough)
thank God the internet isn't a human right.
You sound like a real programmer - What are you doing reading slashdot? Slashdot is a site for posers, wannabes, and sysadmins.
Yours is the attitude that require word processors to need 40MB of ram to run!
Fight Spammers!
And Smaller == Faster.
Not always. While I have been known to drop into assembly, it should never be the first recourse when you are trying to speed things up. If it is, you are likely to miss out on the biggest savings. My rough priority list:
-- MarkusQ
Smaller != faster if you generate a page fault because your data or a function weren't byte aligned then you app will run slow as a dog.
Unrolled loops have a larger footprint but have a smaller execution path.
It's always a good idea to try and make core functionality run compleatly in the CPU cache, if an unrolled loop takes the application core out of cache then don't unroll it, this is were hand assembled code can outstrip compiled code.
If you wan't really fast code then write it in decent Java and have a profilling just-in-time compiler.
thank God the internet isn't a human right.
Since its optimization you are concerned with I have a few choices you will be interested in:
1. The Zen of Code Optimization by Michael Abrash.
2. Agner Fog's Assembly Resources
3. The Athlon Optimization Guide
4. Intel's IA32 Optimization Guide
5. The Aggregate Magic Algorithms
These sources will give you everything you need to know about code optimization for x86.
I have done a little programming in x86 assembly and I must say that it has given me an exelent understanding of how the processor works. I think it's very neat. I also find programming in assembly fun! Yeah... I'm crazy. I agree. The person who said put your brain into a blender and pour it back into your head was right... You'd have to be nuts to WANT to program in assembly for normal tasks. Well, I'm nuts =) WEEEEEEE!
But anywho, to answer your question, use the internet... It's invaluable. Fire up good ol' Google and start searching for x86 assembly or x86 ASM. There are plenty of resources out there including lists of opcodes: what they do, how to use 'em, interrupts, even lists of ASM->machine code translations... Just in case you ever wanted to know that 26D26DFF is shr byte [es:di-1],cl. Take a look at NASM (http://www.cryogen.com/Nasm) if you are looking for an assembler... It's free and works nicely =) There is huge list of interrupts (http://www.pobox.com/~ralf, plus a Windows program, http://www.via.nl/~dms/freeware/rbilviewer/, to read the lists and display them all pretty).
Hope this helps. (No URLs have been checked, but I doubt they've changed)
http://grc.com/smgassembly.htm
Yep, Gibson writes gui Win32 windows apps in pure x86 assembly. He's nuts, but his apps are tiny and run fast. Lots of good resources there.
Shhhh! Don't tell that to these guys:
...or...
Also, from what I understood, the WordPerfect Corporation actually required that all programs be written in assembler. BTW, some more interesing WP history.
Oh, you mean, you wouldn't use assembler to write a word processing suite nowadays. Ok, I getcha. Yeah, I think you're right. After all, WordPerfect Corp has been out of business for how long? (Well technically, bought out and resold, and resold... They're just a name now.)
--JoeProgram Intellivision!
It's from 1994 and thus the pre-MMX, pre-P6 world, and it is not especially well written (although not bad), but there is a good discussion of segmentation and of which instructions pair in the Pentium's different pipelines, and it really walks you though optimizing a bunch of code.
I think some of it might well be useful relative to the ten bucks it's going for on half.com. (Actually, I found it remaindered for $4.00 at the Cambridge, Mass Micro Center, so you may do a little better too.)