Learning x86 for Non-x86 Assembler Programmers? 64
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?"
Brute force it (Score:1, Redundant)
Joke aside, what about checking Intel's pages? I seem to remember there were quite a few documents there on assembly programming...
Intel's pages (Score:2, Informative)
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.
Helpful website (Score:3, Informative)
Assembly on a modern proccessor? (Score:1, Offtopic)
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.
Re:Assembly on a modern proccessor? (Score:1)
Granted, you wouldn't write a word processing suite in assembler, but for graphic routines, graphics libraries, maths and 3D processing and similar tasks that use much processing power.
Take a look at programs like dnetc @ distributed.net [distributed.net]. I wonder why it's not programmed in Visual Basic, but actually in assembler (The core, atleast). I'd like to see dnetc and seti@home programmed in a high-level language, and still be efficient.
Assembly language word processing? (Score:2, Interesting)
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. [fitnesoft.com]
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.)
--JoeRe:Assembly on a modern proccessor? (Score:2)
fighting chance (Score:1)
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)
proccessor that runs 2.5GHz in 640MB ram (Score:1)
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.
Re:proccessor that runs 2.5GHz in 640MB ram (Score:4, Interesting)
Re:Assembly on a modern proccessor? (Score:1)
Re:Assembly on a modern proccessor? (Score:2)
In this age and time, asm is particularily suited to computationally intensive portions of programs. Yes, it's probably not going to be faster if you write the whole thing in asm, but it'll not only be apt to be bug prone, but the development time will likely take longer.
Re:Assembly on a modern proccessor? (Score:2)
Didn't think so. :)
Re:Assembly on a modern proccessor? (Score:1)
Oh my $DEITY, that is sweet, Thanks for posting that.
Re:Assembly on a modern proccessor? (Score:2)
$
42
$ wc -c a.out
7 a.out
$
Can you guess the contents of a.out?
$ cat a.out; echo
exit 42
$
(the "; echo" is there because a.out doesn't have a trailing newline - every byte counts!)
Stuart.
Re:Assembly on a modern proccessor? (Score:2)
Which kind of defeats the purpose. :)
Re:Assembly on a modern proccessor? (Score:2)
If the size of bash is going to be counted towards my seven-byte program, you'll have to count the size of the kernel towards his 45-byter. Sure, I still lose because bash+kernel+7 is greater than kernel+45, but at least the ratio isn't so bad as bash+7 vs 45
Re:Assembly on a modern proccessor? (Score:2)
Re:Assembly on a modern proccessor? (Score:2)
It's a wonder than Linux will sneeze on it... (Score:2)
"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.
Re:Assembly on a modern proccessor? (Score:1)
A
xor ax,ax
ret
[esc]
R CX
3
N tiny.com
W
Q
tiny.com
I can make one 3 bytes!
Re:Assembly on a modern proccessor? (Score:1)
Re:Assembly on a modern proccessor? (Score:3, Interesting)
That, is why ASM is better then any HLL. I think the best quote I got from one of my Computer Engineering book was (paraphrasing) "Modern compilers with their optimizations are on the road to becoming almost as good as hand writen assembler."
Now, would I write word processor in ASM? Not bloody likely, HLLs make it much easier to do. But, when you are writing code for some type of embeded system that doesn't have a whopping 2 GHz processor, ASM will beat any HLL hands down. Unfortunately, too many people think ASM is dead, never learn it, write their embedded code in C and when it isn't fast enough, tell their supervisors that it needs a faster processor. Consider this scenario (stolen from one of my profs):
Re:Assembly on a modern proccessor? (Score:1)
And if you want the ultimate in hand-written optimisation, check out The Story of Mel. [pbm.com]
Always makes me feel humble every time I read it
Re:Assembly on a modern proccessor? (Score:1)
cmp Smaller,Faster (Score:4, Insightful)
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
Re:cmp Smaller,Faster (Score:1)
As always, YMMV.
byte aligned function &co (Score:1)
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.
Yours is the attitude! (Score:1)
Yours is the attitude that require word processors to need 40MB of ram to run!
Wednesday (Score:2, Informative)
Re:Wednesday (Score:2)
Too many aspect (Score:3, Insightful)
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!
Re:Too many aspect (Score:2)
Look asm is not needed to write M$ apps. It is needed for highly computative tasks. While Fortran does a good job in most cases sometimes it needs hand crafted asm to get the job done in time. When all you have is a hammer everything looks like a nail.
Re:Too many aspect (Score:1)
Judging by the variety of platforms the poster has coded for, I would say it's likely that most of his assembler is done on EMBEDDED platforms, where assembly language IS still used fairly often and where optimizations WOULD come in handy.
First, get the reference books (Score:5, Informative)
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.
Zen of Assembly Language (Score:3, Informative)
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.
Re:Zen of Assembly Language (Score:1)
Re:Zen of Assembly Language (Score:1)
Micheal Abrash (Score:2)
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 [slashdot.org], but doesn't seem to link there anymore, perhaps someone else would have an idea where the find the download.
It's all in the blend... (Score:2, Funny)
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.
Re:It's all in the blend... (Score:1)
Zen of Code Optimization (Score:1)
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
For Linux (Score:4, Informative)
You mean "assembly"? (Score:1, Troll)
You don't code in "assembler" language, you code in "assembly" language.
Re:You mean "assembly"? (Score:2)
-Peter
Re:You mean "assembly"? (Score:2)
Webster (Score:2)
Webster -- The Place on the Net to Learn Assembly Language Programming [ucr.edu]
See The Art of Assembly Language Programming [ucr.edu]
The Visible Computer/286/386/486/... (Score:1, Informative)
The Visible series not only teaches assembly, it does so from a novice perspective and includes an emulator to try out the new commands.
You sound like a real programmer. (Score:1, Funny)
A more serious answer ... (Score:2, Informative)
1. The Zen of Code Optimization by Michael Abrash. [amazon.com]
2. Agner Fog's Assembly Resources [agner.org]
3. The Athlon Optimization Guide [amd.com]
4. Intel's IA32 Optimization Guide [intel.com]
5. The Aggregate Magic Algorithms [aggregate.org]
These sources will give you everything you need to know about code optimization for x86.
x86 Assembly (Score:1)
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)
best x86 resource (Score:3, Interesting)
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.
Another somewhat outdated book (Score:2)
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.)