Live data from Hacker News

Ask HN: What's the best resource for learning modern x64 assembly?

news.ycombinator.com

31–40 of 112 posts

Re: Ask HN: What's the best resource for learning modern x64 assembly?

#31

There's no magic anywhere and 99.1% of it is documented. IA-32e/AMD64 is basically an extension of IA-32/x86. What you're looking for is to understand the difference between real mode, 386 protected mode, PAE, paging and long mode because the registers, instructions, addressing and sizes of structures differ. You can learn all of this by reading the Intel software manuals and digging into it yourself. Tools you need…

> IA-32e

I’ve never seen this name before. Where does it come from?

AMD call it AMD64. Intel call it Intel 64.

Why do we need even more names for the same thing beyond these two?!

x86_64, x86-64, x64, AMD64, amd64, Intel 64, EM64T, all mean the same thing!

Re: Ask HN: What's the best resource for learning modern x64 assembly?

#32
I plan on making some videos on this once I get through web vulns. I've written x64 on Windows and Linux. On Windows you need to get ml64.exe, it gets installed with visual studio. You can invoke it from the command line. On Linux I usually use NASM I think, cause it supports Intel syntax.

The only really annoying thing is the stack alignment for calls. I usually just make a macro to check rsp and align if needed. Other than that I think it is similar enough to x86 just with different registers.

Re: Ask HN: What's the best resource for learning modern x64 assembly?

#33

Here is a book on computer architecture that has a good section on x86-64 assembly language. Please note that I have edited this comment to reflect a change suggested by a cold comment. This book (the third edition) introduces x86-64 assembly very well. https://csapp.cs.cmu.edu/

Thanks for the recommendation. I hadn't come across this one before. But wow, I forgot how expensive text books can be.

I picked up the international edition on Amazon a while back for $20 or so, significantly less than what they’re asking for now. I don’t see that one on amazon today, but I’ve seen that edition on other sites. I’d look for it.

I haven’t finished the 3rd edition, but I made it about 3/4 of the way through the 2nd edition and loved it. I picked up the 3rd specifically for the x64 material.

Re: Ask HN: What's the best resource for learning modern x64 assembly?

#35

Earlier quoted context omitted.

> The old floating point stuff is gone. Do you mean x87? I’m not sure it’s true that it’s gone is it? The instructions are still documented? Do they not work?

I guess I stand corrected. I was under the impression that C compilers don't tend to use it anymore when compiling for amd64. (Because you can assume SSE support on amd64, unlike a binary that might run on an old Pentium.) Then I misread that situation as it not working. It does take effort for an OS kernel to save and restore x87 state, so I could imagine somebody dropping support.

> It does take effort for an OS kernel to save and restore x87 state, so I could imagine somebody dropping support.

x86 makes it pretty easy on the kernel devs with XSAVE.

Re: Ask HN: What's the best resource for learning modern x64 assembly?

#36
It really depends on your starting point.

If you know how to write low-level C (i.e. with direct Win32/POSIX API calls), that's a good start. If you don't know what that means, you need to master this first. So much of assembly is built to support higher-level programming, things like segments, indirect references, system calls, etc., so it pays to know the WHY of all of it (e.g. do you know what a frame pointer is? why it's useful and can sometimes be omitted)? Learn this stuff first.

Once you have a good handle on C, you need to start learning how operating systems and memory management work, at least the "client side" of them accessible from userspace.

Then you might want to dip your toe into how CPUs are built, with pipelines, registers, caches, all of that.

If you've mastered those things, gcc -S will provide everything else you need (another comment suggested this).

I learned this stuff in a traditional university computer engineering program. It helped a lot. But for context, this question feels a little like, "can someone explain quantum physics"? It's a huge topic but only like 5% is the actual thing you're asking about, the other 95% is the conceptual machinery (calculus, probability, mechanics) it's built on. Mastering all the other stuff is actually the hard part. For all intents and purposes, assembly is just a notational convenience for expressing program structure in the only way a CPU can understand it. At least 90% of the difficulty is understanding how CPUs work, and how to think at sufficiently low level that you can express human-useful work at such a low level of abstraction.

It would also be nice to know why you want to know this. Writing a device driver is going to be different from writing an operating system, which will be different from writing tight numerical loops in assembly. And in any case, dollars to donuts you won't be able to beat a modern optimizing compiler performance-wise.

Hope this helps.

Re: Ask HN: What's the best resource for learning modern x64 assembly?

#37
post #9

Honestly? Learn old-style 8086 assembly, then 386 code. The new stuff may be architecturally simpler in many ways, but it sits as an edge case on top of a very thick historical stack that seems completely insane if you look at it a priori. But the early CPUs were actually quite simple! At the time "CISC" was a good thing because it provided straightforward mechanisms for expressing things (e.g. "push", "call", load a…

I think segmented memory really hampered my ability to learn 8086 assembly well. It really made a mess of just about everything.

It... kinda didn't though. I mean, yes, it seems complicated when you look back and ask "why can't it just have been a flat space", etc... But the same point persists: you have to look at what came before.

Take a look at the complexities of 8080/Z80 or 6502 addressing modes, or the kind of tricks "big" 16 bit architectures like the PDP/11-70 were playing to stretch addressible memory. The 8086 was a breath of fresh air! Your code could be separate from your stack and from your heap, and all three could be a full 64k without any crazy bank switching or copying! And all you had to do was set up 4 segment selectors and then ignore them. And everything you were used to running did so in a clean, unconstrained environment.

Really, read that 8086 datasheet again (it's like 8 pages), it was great stuff at the time.

Post reply on HN