
Learning C and ASM, what's the way to go? - hilarious
I'd like to learn C and ASM (on x86-64) The documentation I have varies between old books from the 70's, and tutorials from websites with the grammar of 16 year olds, from miserable sites.  I'm not sure how best to attack this problem.  I'd like to learn how to program in C and in ASM, I've read basic things, and understand their usefulness, but I don't know what to read! I have a number of people tell me that they've read near to nothing, but I don't think I have this sort of mindset.<p>Kind elders of Y, give me guidance.<p>Thanks
======
CaptainMorgan
I'd like to second the K&R book.

In addition, I've found if you're taking an engineering approach,
"Applications Programming in ANSI C" by Johnsonbaugh & Kalin was to me a very
valuable learning tool.

Do not go without the "C: A Reference Manual" by Harbison & Steele, which to
me has been the most valuable reference book to have on my person if I
couldn't get or chose not to fancy a good Internet reference.

For Assembly, my coverage has been limited to MIPS, which was in comparison to
Intel's version, a lot easier to understand and thus learn from. Check out
"Computer Architecture and Design" by Patterson & Hennesy. While of course
mostly a book on architecture, it's rooted in learning MIPS to understand
architecture and comes with the SPIM simulator software(which I assume could
be had online). I also own the book by Bryant & O'Hallaron, "Computer Systems:
A Programmer's Perspective" and personally I found that learning MIPS first
brought home Intel's ASM a little softer.

~~~
hilarious
Lovely note, a question, however -- I er, got "C: A Reference Manual" for my
birthday, (which got me into this whole business), but it looks very advanced,
shall I read it as my first book anyway? and ask questions where I falter? Or,
should I go for something more basic?

Thanks again!

~~~
CaptainMorgan
My advice is absolutely not. References are just that - to be looked up for
something you've lost or just don't have the capacity for in your own memory;
they're not made to be read back to back, or at least I could not do that.
Question to ask might be, would you read the English dictionary back to back?
Kudos to those that can or would. :)

You talked about learning C and ASM, and K&R and the other books (excluding
C:ARM and CS:APP) are fine _learning_ tools. So to answer your question, do
not read it first, but have it by your side when something isn't clear...
think of it like a dictionary to look up functions that were not well
addressed (or even talked about at all) in the learning books.

~~~
hilarious
Oh, okay -- Well, I'll make sure I have it handy :)

------
cperciva
If you want to learn x86-64 assembly language, I recommend a historical
approach: Start by learning 8086 assembly language, then learn 80386 (i.e.,
32-bit x86) assembly language, then learn MMX/SSE assembly language; and only
once you've mastered all of those, start on x86-64.

The x86 instruction set is kludges built upon kludges, and you're never going
to understand it fully if you try to jump in at the end without seeing how it
developed.

~~~
jacquesm
Agreed, the biggest switch is probably from 286 to 386.

486 and Pentium didn't really do much instruction set wise (sure there were
changes), then the next big change is the 64 bit systems.

edit: What do you mean kludges ? It's the most orthogonal instruction set
known to man!

~~~
tptacek
Forget everything about the code, data, and stack segments.

~~~
hilarious
What do you mean?

~~~
jacquesm
He means that you don't need those anymore.

You used to need them (badly), especially on the smaller CPUs because
otherwise you were severely limited in memory.

Check out the 'mixed models' that were pretty common usage in the 80's.

There were lots of them:

    
    
      code          data            model name
      under 64KB	under 64KB	Small (-ms) or Tiny (-mt)  
      over 64KB	under 64KB	Medium (-mm) 
      under 64KB	over 64KB	Compact (-mc) 
      over 64KB	over 64KB	Large (-ml) 
    

I was _so_ happy when I finally got out of 'model hell' and could use 'flat'
mode using DJGPP. Finally C programming without the headaches.

<http://en.wikipedia.org/wiki/DJGPP>

------
sangaya
For C, absolutely K&R.

For Asm, I concur with cperciva that the best way to go is to start at 8086
and work your way towards more modern assembly.

For 8086 I highly recommend "The 80x86 IBM PC and Compatible Computers
(Volumes I & II): Assembly Language, Design, and Interfacing". I learned from
this book and thought it was very helpful.

For more modern stuff I really enjoyed "Computer Systems: A Programmer's
Perspective". It will be most beneficial after reading through K&R and being
familiar with ASM. It focuses a lot on programming techniques, tips, and
tricks so that compiled C/C++ code is turned into efficient assembly.

Finally, if you'd like to build a whole system end to end, the Motorola 68HC11
microcontroller is a fun little piece of hardware to play around with.
"MC68HC11: An Introduction to Software and Hardware Interfacing" is a good
book for this purpose.

Best wishes on your journey into the low-level programming world!

PS. I'm biased. I have read and own all of the above mentioned books. Thanks
for getting me to brush off the dust again ;)

~~~
gte910h
The best way to learn assembly is to do it on a risc chip, then work up to a
CISC chip.

So pic, mips, etc first, then X86

------
device
It might help to take a step back and ask why first. Are you working on a
project that requires both?

If you just want mercenary ability with C and ASM, you'll have no problem
finding the resources - just a matter of finding the _best_ book. I'm sure
you'll get plenty of recommendations here.

It sounds you want to enrich your general programming knowledge though. In
that case I'd recommend learning computer architecture concepts - once you
have that down, learning C and ASM will be a pretty transparent process. In
fact, I'd recommend it even if you just need it to hack on something specific.
I took an architecture course at uni that used this book, and it was pretty
decent: [http://www.amazon.com/Computer-Organization-Design-Fourth-
Ar...](http://www.amazon.com/Computer-Organization-Design-Fourth-
Architecture/dp/0123744938/ref=sr_1_1?ie=UTF8&s=books&qid=1256345500&sr=1-1)

~~~
hilarious
I'll take a look at that, thankyou :)

------
tptacek
My favorite C book is "C Interfaces and Implementations" (CII) by Hanson. It's
an excellent bridge from Perl/Python to C; in particular, it gives you
resizeable arrays and hash tables, which are the two things you will miss most
from high level languages. My advice is, learn to code C for a couple months
using CII, then tackle assembly. When you see how C programs are decomposed
into functions, and how arguments are passed to functions, you'll understand
much more about how assembly should work as well.

I've taught a couple people basic x86 assembly using a programmable assembler
we wrote in Ruby. A really great place to start with assembly is to be able to
define a array of instructions and be able to "jump in" to them and see what
they do.

~~~
hilarious
CII looks like it'll be an amazing book! Shame it's not available online, I
wish it were, I wouldn't sleep tonight!

I'll probably make a purchase when I get my paycheck though, thanks!

------
pan69
OK. Depending on your programming background you're facing a steep learning
curve. That's why I recommend a bottom up approach for you.

First read "Code" by Charles Petzold. This book will get you "in the mood" and
in the right frame of mind: [http://www.amazon.com/Code-Language-Computer-
Hardware-Softwa...](http://www.amazon.com/Code-Language-Computer-Hardware-
Software/dp/0735611319/ref=sr_1_1?ie=UTF8&s=books&qid=1256345283&sr=8-1-spell)

Then I suggest you pick up a good book on Assembler. This might be a good
choice: [http://www.amazon.com/Professional-Assembly-Language-
Program...](http://www.amazon.com/Professional-Assembly-Language-
Programmer/dp/0764579010/ref=pd_sim_b_2)

Start writing some drivers for Linux. Like a memdrive or something. Do it all
in Assembler! Oh, you need to read other books on how to do this...

Then pick up the K&R book on C. Now write your memdrive driver in C.

That should get you started. I think it will take you at least up to two years
before you're passed the learning curve and to be comfortable with this level
of programming.

Oh, you need to be willing to do it for the love of it because it's highly
unlikely that you will make a living using these sort of technologies
(nowadays).

Good luck!

PS: I miss the old days...

~~~
jacquesm
I wouldn't start off by writing a driver in assembler, probably better to
write it in C first, then use the -S flag to get the intermediate and study
that until you drop.

~~~
pan69
I can assure you that if you want to learn C AND Assembler and you start with
C, you will never ever get to the Assembler bit. A memdrive isn't that
difficult and if you never solve a 'real' problem, you never learn anything
because you don't have to push yourself. Just my 2 cents...

~~~
jacquesm
It really is your two cents, what goes for you does not go for everybody else.

Some people will learn just for the fun of learning.

~~~
pan69
You are obviously missing my point. But that's alright...

------
masterchief
The K&R book is the classic C reference. I'd also recommend the K.N. King book
... C programming: a modern approach.

------
pfedor
I recently decided I wanted to learn assembly language. So far I haven't
started but I spent an hour or two perusing books on assembly language on
Amazon, reading product descriptions and users' reviews, and I decided to
start from this one: [http://www.amazon.com/Professional-Assembly-Language-
Program...](http://www.amazon.com/Professional-Assembly-Language-
Programmer/dp/0764579010/)

The reasons I chose it are

(1) It's introductory, i.e., a good first book for a complete noob. (2) It's
very Linux-centric. Most good books on assembly language assume you use
Windows or even DOS it seems. (3) One of the reviews says, "you will get a
good workout using the gdb debugger." Sounds like fun.

------
parse_tree
I highly, highly recommend the book "Programming from the Ground Up". It is
freely available, and full of small asm sample programs to try out yourself.

Here's a link:

<http://savannah.nongnu.org/projects/pgubook/>

~~~
hilarious
Oh, I've been missing out, haven't seen this one, thanks!

~~~
springrider
highly, highly recommand +1, I'm reading it, it's better than any other
program books I've ever read. It tells you what, how, why, and it tells in an
interesting way.

------
markerdmann
The classic C book is The C Programming Language by Kernighan and Ritchie
([http://www.amazon.com/Programming-Language-2nd-Brian-
Kernigh...](http://www.amazon.com/Programming-Language-2nd-Brian-
Kernighan/dp/0131103628)).

------
zimbu668
I worked through Assembly Language for the PC([http://www.amazon.com/Assembly-
Language-Brady-programming-li...](http://www.amazon.com/Assembly-Language-
Brady-programming-
library/dp/1566860164/ref=sr_1_1?ie=UTF8&s=books&qid=1256402206&sr=1-1)) in
highschool. You start with the basics and at then end you have a working hex
editor, for DOS anyway. In terms of programming knowledge gained it was more
than any college class I took, heck more than most college semesters.

------
bbg
I second the choice of K&R for learning C -- where better to learn C than from
the authors themselves.

For assembly I've had a good experience with K. Irvine's _Assembly Language
for Intel-Based Computers_ , available on Amazon here:

[http://www.amazon.com/gp/product/0132304686?ie=UTF8&tag=...](http://www.amazon.com/gp/product/0132304686?ie=UTF8&tag=begrsucdewe-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=0132304686)

------
gte910h
Do all the projects in K&R, then buy a pic board and pic compiler and get on
it.

------
jacquesm
For assembly I can recommend Lance Leventhals books.

80386 Programming Guide

And the intel reference manuals.

------
korch
When I was 18 and decided to first try programming, I picked assembly to start
with since I had read somewhere that all other languages could themselves be
written in terms of assembly. The book I learned assembly from was Randy
Hyde's online version of his textbook Art of Assembly. I used the DOS version,
because I didn't know what Unix even was at that time; but here's the newest
Linux version:
[http://homepage.mac.com/randyhyde/webster.cs.ucr.edu/www.art...](http://homepage.mac.com/randyhyde/webster.cs.ucr.edu/www.artofasm.com/Linux/HTML/AoATOC.html)

Like other commentors, K&R would be my best recommendation for the second
book. You might as well start off on the right path towards enlightenment.

~~~
hilarious
Not trying to rub you the wrong way but I'm not interested in learning HLA,
which is what AoA covers, purely LLA.

Thanks!

~~~
drhowarddrfine
You are exactly right even though Randy won't like you (or me) saying that.

