Fog Creek Software
Discussion Board




Assembly Language

I'm just finishing up a university class where we studies computer architecture and assembly language. I've been programming in high-level languages (C++, Java, VB, etc.)for quite some time, and I really enjoyed getting down to the level of the actual processor instructions.

Unfortunately, though, the point of this class was just to use assembler as a method for teaching concepts, and, consequently, we only used 8086 assembler.

I'd like to learn some optimization techniques using some more modern instruction sets (MMX, 3DNow!, etc.), but I'm having trouble finding a good resource. The instruction set documentation from Intel and AMD is a little too terse. I'm looking for something that not only gives me a list of instructions, but tells me when it makes sense to use them.

Any recommendations?

jos@xxagg.com
Tuesday, December 03, 2002

One question first.  Why?

Assembly is almost never the solution to any real problem.

Bob

Robert Anderson
Tuesday, December 03, 2002

I'd look for an advanced textbook on compilers.  It's been years since I've taken a University course on compilers, but I know that optimisation was an important topic in textbooks years ago.  Modern compilers are generally very good at optimisation, so a modern text that covered optimisation for a modern example processor with a pipelined architecture would be a good place to learn the concepts and learn why you'd never want to do it by hand.

Another good way to get an understanding of an instruction set and optimisation is to get a compiler that will spit out assembly code, and see what the compiler does with some C code.  And again, you'll learn why you'll never want to do it by hand.

rg
Tuesday, December 03, 2002

But jas specifically mentioned proprietary vector processing extensions. If you are thinking about that you are thinking of optimizing for those special instructions in order to have the ultimate in speed in your cutting-edge competitive product. Which is when it's worthwhile indeed.

X. J. Scott
Tuesday, December 03, 2002

rg,
Sounds like he's comfartable with the code that comes out of the compiler. He's looking for info on the extensions.

jas,

I think the general idea is you are going to have to figure it out from the materials provided by the manufacturer. By the time books get written, using the proprietary vector processing instructions of type K will long since have been obsolete.

If you want more than that, you'll need to write the book yourself -- which can be a worthwhile experience, though I'd limit it to a journal article or two and get your name out there as a published author/consultant, which can be extremely valuable in its own right.

X. J. Scott
Tuesday, December 03, 2002

"Assembly is almost never the solution to any real problem."

..We don't speak Latin either, but knowing that language is of huge benefit in understanding most Western languages.

Granted, today it's rare that we need to crank out the assembler but it's always a worthwhile pursuit to understand it better and be proficient with it.

Mark Hoffman
Tuesday, December 03, 2002

"..We don't speak Latin either, but knowing that language is of huge benefit in understanding most Western languages."

And if you read what he wrote, you'd see that he was not interested in such things, which were taught in the course, but in coding for MMX and 3dNow!, etc.

That is hardly foundational.

Bob

Robert Anderson
Tuesday, December 03, 2002

Bob is right. I've decided to give up on assembler. Could we change the topic of this thread to "Boring Accounting Software?"

jos@xxagg.com
Tuesday, December 03, 2002

I did read his post. He says he is looking to "learn some optimization techniques using some more modern instruction sets" and then gives examples such as MMX, etc.

I think it's clear from his post that he wants to learn more and become a better programmer, not write the next killer app in assembly language.

If someone wants to hone their assembly language skills then why must you begrudge him that?
I don't believe he was asking if assembly was proper to study, but rather for resources for learning assembly.

Now that that is out of the way...

This is an old web site, but it has some info you might find interesting:
http://asmjournal.freeservers.com/

This has some nice links:
http://www.nuvisionmiami.com/kip/asm.htm

And from a Linux standpoint:
http://linuxassembly.org/

Hope that helps!

Mark Hoffman
Tuesday, December 03, 2002

Bob is right. I've decided to give up on assembler. Could we change the topic of this thread to "Boring Accounting Software?"

Ha.

I know it's obnoxious to question the question, but I do think it behooves everybody to ask themselves what problem they are trying to solve before scurrying in every which direction to learn this or that.

So you can fess up now and admit that you're interested in game programming.  No shame.

Bob

Robert Anderson
Tuesday, December 03, 2002

"I think it's clear from his post that he wants to learn more and become a better programmer, not write the next killer app in assembly language."

Actually, I think he wants to write games, but that is beside the point.

If you were right, the next logical question is "what do I need to learn to become a better programmer?"  My point is that "MMX assembly" is probably the wrong answer to that question for 99% of programmers.

Bob

Robert Anderson
Tuesday, December 03, 2002

Note, when you find yourself writing a compiler you will find that assembly comes in quite handy.

Also remember that for the people designing the next microprocessor (in microcode), assembly is a high level language.

I know 50% of people say 'assembler' but that is like saying today I did some coding in compiler.  Ok, that is just being too anal.

anonymous
Tuesday, December 03, 2002

Maybe not so "anal". I think it is important to use the correct terminology - even not having English as a native tongue, and sometimes stepping over the basics myself.

So, if anyone has any doubts:

*Assembly* is the "language", useful or not, that is being discussed here.

*Assembler* is the computer program that converts  mnemonic code (that "MOV AX,00" stuff) to binary files that can execute that code (ok, nowadays assemblers do *much* more than that, but you get the point). In that sense, you also have a "disassembler" (which makes the opposite conversion).

Chester
Tuesday, December 03, 2002

"Assembly is almost never the solution to any real problem."

I would say you must not be trying to solve any very interesting problems these days ...

George Leroy Tirebiter
Tuesday, December 03, 2002

""Assembly is almost never the solution to any real problem."

I would say you must not be trying to solve any very interesting problems these days ... "

Oh really?  Have you considered that the problems I'm solving are so interesting, that new computers are being built as we speak to help me solve them, and that I can't rely on any one instruction set as a result?

Bob

Robert Anderson
Tuesday, December 03, 2002

"Oh really?  Have you considered that the problems I'm solving are so interesting, that new computers are being built as we speak to help me solve them, and that I can't rely on any one instruction set as a result?"

No. ;)

I just had too
Tuesday, December 03, 2002

I used to read the following site closely.  Great machine code level info on various processors and extensions:

http://www.x86.org

Assembly is great when you need to say "it's as fast as it can get" - and mean it.  Besides, how many "great" programmers don't understand stacks and pointers?  Both are fundamentally routed in machine code.

Bill Carlson
Wednesday, December 04, 2002

X. J. Scott,

"rg,
Sounds like he's comfartable with the code that comes out of the compiler. He's looking for info on the extensions."

My point was, to *learn* how to use an instruction set (which can include extensions), take a look at what a compiler produces.  There are compilers which support the extensions mentioned (i.e. compilers which optimise your code to take advantage of the extended instruction set, the vector registers, etc.).  For example, VectorC (go to http://www.codeplay.com ) will optimise C code for MMX, 3DNow!, and SSE, and will also produce an assembly language listing of the code that it produces.

This will certainly give insight into how the vector instructions can be used.

rg
Wednesday, December 04, 2002

You might want to try your hand at PowerPC assembly. The instruction set is much cleaner than x86 and Altivec is the best vector-unit around.

Docs:
http://www-106.ibm.com/developerworks/library/l-ppc/?n-l-812
http://e-www.motorola.com/brdata/PDFDB/docs/MPCFPE32B.pdf
http://www-3.ibm.com/chips/techlib/techlib.nsf/techdocs/852569B20050FF7785256996007558C6
http://www.simdtech.org/apps/group_public/email/altivec/msg04523.html
http://www.simdtech.org/altivec

You can try out or buy an IDE with a PowerPC simulator:
http://www.windriver.com/download/details_reg.html?ID=4
http://www.metrowerksstore.com/ppcac.html

Wouter Zelle
Wednesday, December 04, 2002

Read anything by Michael Abrash.

Machine Code
Wednesday, December 04, 2002

I second the PowerPC assembler recommendation.  I also like the MindShare PowerPC book:

http://www.mindshare.com/addtech/powerpc_books_main.html

If you're going to understand something like page table layout, its nice to start with an architecture that has a clean design.

Regarding MMX optimization, take a lok at mpeg2dec:

http://libmpeg2.sourceforge.net/

for some very heavy use of mmx in a real world, highly optimized and robust video codec.

Why are assembler and machine architecture important?  Well, it seems to me, that:
1. Its interesting.
2. Its in very high demand.

You can carve out a very lucrative consulting niche if you're skilled in the art.  A hardware manufacturer cannot complete a board level design without this type of person.  Throw in experience using a Tektronix logic analyzer and you can name your price.

Nat Ersoz
Wednesday, December 04, 2002

Does anyone else miss the good ol' days of Z80, 6502 and 6800? It was nice having the instruction set small enough that you could convert from hex in your head...

James

James Shields
Wednesday, December 04, 2002

doing my part to take the thread further off topic...

I've always been interested in low level stuff, too. But, i've been employed doing standard XML/Java/Boring work. I have done relatively large projects in C++ (graphics visualization on NT, a while back) I have a degree in math, and took a machine architecture course, albiet 5 years ago.

Responding to Nat's post, is low level stuff _really_  "name your price?" I was always led to believe that anything low-level paid badly, and was the first thing to be outsourced to China. however, I know about three guys who do low level stuff, and all of them are very well off, and seem to really enjoy what they do. (one recently sold his company to intel!) So, I think low level stuff would be kind of fun, but I have no idea how to get into that sort of work.

How does one migrate towards that sort of thing, short of getting a degree in electrical engineering, and working as a shlub at HP for 15 years? Feel free to respond off-forum.

offtopica
Wednesday, December 04, 2002

Yes, James, the Z80, 6800, etc. were great, weren't they.  The 68000 had a nice, clean 32 bit achitecture also.

Just for kicks, what is the smallest possible instruction set that allows a programmers to do everything?

I think it can be paired down to one instruction, CBAJIBS (compliment bit and jump if bit set).  There are two arguments, a data address (of a bit to flip) and a jump target for the conditional jump.

From combinations of this instructions, you can create "SET BIT", "CLEAR BIT", "UNCONDITIONAL JUMP", "OR BIT", "AND BIT", etc.  Combinations of these combo-instructions could create accumulators, byte arithmetic, etc.

Isn't this really all you need?

Bill Carlson
Wednesday, December 04, 2002

I guess in that sense, no, all you *really* need is a cold dank cave with a drip and some rock salt and malodorous roots to chew on.

Sarain H.
Wednesday, December 04, 2002

Bob: No, assembly isn't useful for anything.

Just the core systems of any number of banks I'm familiar with.

Nothing important at all...

Rodger Donaldson
Thursday, December 05, 2002

Forgive me for appearing dumb, but I thought that low-level programming was for the vast majority of chips  inside cars, mobile phones, fridges and so on, when you'd have to pare things down.

Stephen Jones
Thursday, December 05, 2002

The most comprehensive book on compilers I have seen is "Compilers - Principles, Techniques and Tools" by Aho, Sethi and Ullman.

It may not help with games or MMX programming though.

Justin
Thursday, December 05, 2002

The Z80 and the 68000 were nice, but the 6502, come on ...

Just me (Sir to you)
Thursday, December 05, 2002

"Bob: No, assembly isn't useful for anything.

Just the core systems of any number of banks I'm familiar with.

Nothing important at all... "

What is this, a straw-man contest?  How is MMX and 3dNow! relevant to bank systems?

Bob

Robert Anderson
Thursday, December 05, 2002

Assembler is less necessary than it used to be even just a few years ago.  Thankfully, you can accomplish most embedded software tasks using C.  If you're writing a boot loader or operating system component, or debugger then you're going to have to do some assembler. 

What is required to become a successful consultant in the low level?  I'm not one, but I'll guess:
> 10 years of experience
> 5 technically successful products
Knowledge of x86 architecture + one other RISC core (MIPS, ARM, PowerPC)
C language expert
VHDL/Verilog experience

Got all that?  You can contract for $100/hour and maintain a healthy client list.  FWIW.

Nat Ersoz
Thursday, December 05, 2002

Thanks. Just checking to see if it was anything beyond the obvious. FWIW, if you have 10 years of experience, and 5 discrete points of success, and a healthy client list, you can make $100+/hr doing anything (VB programming, psychotherapy, full body massage,  etc).

offtopica.
Thursday, December 05, 2002

In my 20 year career, I have met fewer than 5 people that contracted at that rate.  In any field.

Nat Ersoz
Thursday, December 05, 2002

You must not know the right people. My aunt is a psychologist (not "psychiatrist.") She works with developmentally disabled kids in Dallas. She charges $100+/hr. My dad is a doctor in an underserved area, he is salaried by a hospital, but if he works additional ER shifts he makes about $130/hr. My friend's dad is an optometrist in manhattan, who makes about $200/hr, but that is because he set up his practice so that he can do 2 patients an hour. A friend of mine who is a second year at a manhattan law firm makes $100/hr. Another friend of mine who has been practicing law for quite a while makes about $200/hr , in Boca Raton, FL.
My friend's ex-girlfriend photoshops out the flab of supermodels for press release and cover shots, and she charges about $175/hr. Another friend of mine is an illustrator for advertising firms, who makes about $100/hr, although he charges per illustration.
My friend's brother is a cisco CIEE in pittsburgh, he makes about $125/hr. One of my best friends from high school dropped out of high school because he was making $150/hr doing unix systems administration around manhattan. (he now manages his own company of sysadmins) A woman I just met in a continuing education course I'm taking, makes $50 every half hour giving massages. Another woman I recently met makes $60 every half hour doing acupuncture, and $40 every half hour giving piano lessons (you can't easily do both of those in the same hour, so that is a bit of a straw man).

Due to market anomalies, I made between from $75-$100 an hour doing oracle DBA and unix sysadmin, during the boom. And I had about a year of experience. Now I make about $70 grand a year. Anyway, the point is, I'm getting old, and with all these upper-middle class friends and family, I'm feeling a bit insecure about my lack of financial success. I'm into mathematics and arcana, so I would certainly like to make $100/hr as a compiler hacker, but if will take an additional 10 years, I might be better off becoming a radiologist...

offtopica
Thursday, December 05, 2002

>>The Z80 and the 68000 were nice, but the 6502, come on ...

The 6502 was equal to the Z80 in terms of performance, it didn't have all the registers of the Z80 but did give you easy access to the first 256 bytes of memory.

Tony E
Thursday, December 05, 2002

Guess we don't got none of them high paying jobs down here in the double-wide...

Nat Ersoz
Thursday, December 05, 2002

>>The Z80 and the 68000 were nice, but the 6502, come on...

I remember learning coding in assembly on the Sinclair ZX-81 (Z80 processor).

Assembler: none

Seriously, the tape storage was so unrealiable that I had to assemble instructions by hand and type them in. If I recall, the custom was to make the first line in you BASIC program a REM statement with enough characters to hold the program. You could then POKE values into address 16514 and on. I think the beginning of video RAM was a 16 bit pointer stored at 16396.

Then Christmas came one year and a VIC-20 arrived. Rejoice! Now to learn a 6502 processor...

I remember having to know the difference between indirect indexed addressing and indexed indirect addressing.

The Z80 was simpler to code in all.

I still have that ZX-81, with a soldered on RAM pack...

Soon as it comes back in fashion, it'll be worth a fortune!

Germus
Friday, December 06, 2002

I was purely refering to the nice orthogonal instructions set of the Z80, not implying the value or quality of the machines build around it as opposed to those using different processors at the time.
I realy liked my ZX Spectrum though.

Just me (Sir to you)
Monday, December 09, 2002

*  Recent Topics

*  Fog Creek Home