views:

173

answers:

5

I'm looking at textbooks for an undergraduate course in machine-level programming. If the perfect book existed, this is what it would look like:

  • Uses examples written in C or assembly language, or both.

  • Covers machine-level operations such as two's-complement integer arithmetic, bitwise operations, and floating-point arithmetic.

  • Explains how caches work and how they affect performance.

  • Explains machine instructions or assembly instructions. Bonus if the example assembly language includes x86; triple bonus if it includes x86-64 (aka AMD64).

  • Explains how C values and data structures are represented using hardware registers and memory.

  • Explains how C control structures are translated into assembly language using conditional and unconditional branch instructions.

  • Explains something about procedure calling conventions and how procedure calls are implemented at the machine level.

Books I might be interested in would probably have the words "machine organization" or "computer architecture" in the title.

Here are some books I'm considering but am not quite happy with:

  • Computer Systems: A Programmer's Perspective by Randy Bryant and Dave O'Hallaron. This is quite a nice book, but it's a book for a broad, shallow course in systems programming, and it contains a great deal of material my students don't need. Also, it is just out in a second edition, which will make it expensive.

  • Computer Organization and Design: The Hardware/Software Interface by Dave Patterson and John Hennessy. This is also a very nice book, but it contains way more information about how the hardware works than my students need. Also, the exercises look boring.

    Finally, it has a show-stopping bug: it is based very heavily on MIPS hardware and the use of a MIPS simulator. My students need to learn how to use DDD, and I can't see getting this to work on a simulator. Not to mention that I can't see them cross-compiling their code for the simulator, and so on and so forth. Another flaw is that the book mentions the x86 architecture only to sneer at it. I am entirely sympathetic to this point of view, but news flash! You guys lost!

  • Write Great Code Vol I: Understanding the Machine by Randall Hyde. I haven't evaluated this book as thoroughly as the other two. It has a lot of what I need, but the translation from high-level language to assembler is deferred to Volume Two, which has mixed reviews. My students will be annoyed if I make them buy a two-volume series, even if the price of those two volumes is smaller than the price of other books.

I would really welcome other suggestions of books that would help students in a class where they are to learn how C-language data structures and code are translated to machine-level data structures and code and where they learn how to think about performance, with an emphasis on the cache.

+1  A: 

Probably not a great core textbook for what you describe, but you might find some value in Hacker's Delight, by Henry Warren. It's got a collection of gems of assembly programming, and it might be a good source for exercises, or examples you could go over in class.

paul.meier
I love Hank's book, but it's way too advanced for my class.
Norman Ramsey
+2  A: 

I beg to differ about Computer Organization and Design: The Hardware/Software Interface. It covers every single topic you've mentioned, and much more. You can carefully pick up what your students need to read and what not, but it's all there. It's a truly terrific book. The sections that explain how various C constructs (data structures and control statements) and calling conventions are translated into assembly are excellent, and unparalleled in any other book I've ever seen.

I don't understand the "show stopping bug" you mention. Using a simulator is a convenient way to learn architecture and write assembly code. By DDD do you mean a debugger? This is just a single tool! Let them read a tutorial or something, it's not what I would expect a textbook to teach.

Eli Bendersky
Eli: Don't get me wrong; I like the book. But my colleagues are expecting me to teach *Intel* assembly language, and my students need to *practice* using the debugger (as well as valgrind). I don't need the textbook to cover the debugger, but I do need to run on the actual hardware we have, not a simulator.
Norman Ramsey
@Norman: fair enough. Many universities, however (especially the higher-level ones, if you notice) decided to skip x86 and teach a saner architecture that presents the topics in a more normal manner to the students. The students are then expected to take this knowledge and apply it to x86. x86 is a sucky architecture to teach.
Eli Bendersky
@Eli: YOU are telling ME???? :-) Actually x86-64 is dramatically better than x86 alone, but still...
Norman Ramsey
+1  A: 

As a student, I have been using Assembly Language For Intel-Based Computers by Kip Irvine for 2 semesters now. It is more of an introductory book than anything else, however.

It is not a book that places emphasis on the areas you want to cover in depth, however it does cover all of the areas you mentioned above.

Dinoo
@Dinoo: how do you like it?
Norman Ramsey
@Norman: I love the book, as I am very interested in low-level programming. It explains everything from PC architecture, bitwise arithmetic, to basic and advanced assembly, floating point arithmetic, and embedding assembly code in C/C++.The last few chapters of book focus on MS-DOS, Protected, Real-mode, and graphical programming, as well as further concepts related to hardware such as better memory management and accessing disk sectors.It is a very well written book and explains everything thoroughly. Definitely keeping it after I'm done with the class needing it.
Dinoo
A: 

I am pretty sure the way C code gets translated to machine code is very compiler specific. Even with the same compiler, it is dependant on the configuration switches passed to it before compilation. That would complicate such a book, unless it was written about a specific compiler (eg: gcc). You might want to look at some books written on compilers themselves, they might be worthwhile.

In gcc at least it's as simple at passing in the -S switch:

gcc -S source.c

Would created a source.s file in same directory with the actual assembler output.

You can play around with some gcc switches to see how it affects the assembler output:

gcc -O3 -S source.c

Which enables deep optimisation.

Hope this helps a little even if it doesn't directly answer the question.

joemoe
Actually almost everything about it is quite generic, although there are some small things that vary more by machine than by compiler. (Compilers and code generation are things I have studied for over 20 years.) For example, a comparison translates to a two-instruction sequence, compare and then branch, except on sensible architectures with 'branch less than' and 'branch equal' instructions. Once you can do that, there's really only one way to translate `if` and only two ways to translate `while` (test at top or bottom).
Norman Ramsey
A: 

Randy Hyde's book should be good for you - he has spent a lot of time teaching and even wrote his own intermediate level language called HLA to gradually ease his students into pure assembly. (HLA abstracts out some of the high level constructs such as if statements and function calls).

Explains how C control structures are translated into assembly language using conditional and unconditional branch instructions.

If you are using a half decent compiler then this should vary by flavour/model of cpu - for example there were some big differences between the P2, P3 and P4 which meant the same control structure could run blazing hot on one but like a dog on the other (due to branch prediction, pipeline stalls, etc.). This may actually be a good example to show your students.

Explains machine instructions or assembly instructions. Bonus if the example assembly language includes x86; triple bonus if it includes x86-64 (aka AMD64).

You can get these types of manuals for free from Intel and AMD - and they will contain the 64 bit instructions as well.

slugster