Hacker News new | past | comments | ask | show | jobs | submit login
Ask HN: How did you learn x86-64 assembly?
48 points by spacechild1 on July 23, 2020 | hide | past | favorite | 20 comments
I'm an experienced C/C++ programmer and I occasionally look at the generated assembly to check for optimizations, loop unrolling, vectorization, etc. I understand what's going on the surface level, but I have a hard time understand what's going on in detail, especially with high optimization levels, where the compiler would do all kinds of clever tricks. I experiment with code in godbolt.org and look up the various opcodes, but I would like to take a more structured way of learning x86-64 assembly, especially when it comes to common patterns, tips and tricks, etc.

Are there any good books or tutorials you can recommend which go beyond the very beginner level?




It sounds like you are off to a great start. Afaik there really isn't any 400 or 500 level material for advanced assembly programming. You could read Goto BLAS [1] or mkl-dnn [2]

I find reading assembly output from compilers to be a good start, it also helps you develop a mental model of what compilers expect in terms of stack hygiene.

Before godlbolt, https://godbolt.org/ there was `gcc -S` and now there is [3]

    gcc -Wa,-adhln -g <file.c>
for interleaving source and assembly output. Keep the reference manual close and write lots of little experiments to confirm your findings.

[1] https://en.wikipedia.org/wiki/GotoBLAS

[2] uses macros and intrinsics https://github.com/oneapi-src/oneDNN

[3] https://stackoverflow.com/questions/3867721/is-there-any-c-c...


Programming from Ground Up, by Jonathan Bartlett.

https://download-mirror.savannah.gnu.org/releases/pgubook/Pr...

I was able to learn a lot about low level programming. The problems with this book: the examples are in Intel syntax (I find AT&T's syntax better to read and it's more common to use), and not in x86-64.

Also, Hacking: The Art of Exploitation (https://nostarch.com/hacking2.htm) have a nice introduction on Assembly, from the standpoint of a person doing reverse engineering, debugging with GDB or shellcoding.


Http://agner.org is a good resource once you know the basics. It explains a lot of how microprocessors interpret instructions, caches, decoding, microinstructions. Assembler is not really a language, it’s deeply connected to the material. Agner explains a lot of those things.

This might be what you want : https://www.agner.org/optimize/#manuals


This gives you somewhat of an idea of what that highly optimized assembly is doing https://ocw.mit.edu/courses/electrical-engineering-and-compu... and the lectures are recorded.


this is gold, thank you!


First of all I learned Z80 assembly, because BASIC was too slow. Then I moved to Intel and 8086 assembly!

That's not so helpful, so more seriously the way that I started was to spend a lot of time reading tutorials and writing sample programs. Back in the day I read virus "magazines" like 40Hex, because they had decent examples of intel assembly and often useful discussion.

These days I've been revisiting things writing a couple of simple compilers:

https://github.com/skx/math-compiler/

https://github.com/skx/bfcc/

The first was mostly written because I'd not done anything recently with floating-point, and the second because compiling brainfuck programs to assembly seemed like it would result in fast programs.

Simple projects like those above could be written quite quickly I think, because they only involve writing a very small collection of "primitives" (such as "write string to STDOUT", or "sin(x)"). They're almost template-based programs.


This is a pretty good intro to NASM, which is one of the nicer assembly languages to use:

https://cs.lmu.edu/~ray/notes/nasmtutorial/

x86-64 (aka x64) is less insane than x86-32 so once you get your feet under you it is easier to understand.


thanks, this looks really nice and condensed.


A good debugger that shows the registers and flags, inspect memory and single step instructions is also a must when you're starting out. I spent many an hour staring at STmon on my atari ST.


When I was writing some assembly last summer, I found:

https://software.intel.com/sites/landingpage/IntrinsicsGuide...

http://ref.x86asm.net/

I don't have AMD, so didn't need to look up AMD specific stuff. Would be amazing if AMD had something like Intel's interactive Intrinsics Guide.


For some of the "clever tricks" at higher levels, Hacker's Delight is a great treasure trove - it's not x86-64 specific, as a lot of the tricks are applicable to many CPUs, but it helps understand some of the seemingly bizarre tricks.

https://www.amazon.com/Hackers-Delight-2nd-Henry-Warren/dp/0...


oh, this looks like a fun book!


I started programming when I was about 9. When I was a teenager, around 13 or 14, I became interested in how to influence the behavior of software after it was written. I had also taken an interest in reading and writing source code of programs that relied on other programs to continue to live, flourish, and fulfill their goal, which meant that sometimes they had to hide, or present themselves in ways that made them accepted.


Worked my way through "the art of assembly language" [1] a Summer of many years ago. It is perhaps a bit outdated but it has all the fundamentals you need to get started.

[1]: https://www.cs.yale.edu/flint/cs422/doc/art-of-asm/pdf/


I think what I am looking for is basically a modern equivalent to this text... (and without all the HLA :-)



High Level Assembly (HLA) https://en.wikipedia.org/wiki/High_Level_Assembly

> HLA was originally conceived as a tool to teach assembly language programming at the college-university level. The goal is to leverage students' existing programming knowledge when learning assembly language to get them up to speed as fast as possible. Most students taking an assembly language programming course have already been introduced to high-level control flow structures, such as IF, WHILE, FOR, etc. HLA allows students to immediately apply that programming knowledge to assembly language coding early in their course, allowing them to master other prerequisite subjects in assembly before learning how to code low-level forms of these control structures. The book The Art of Assembly Language Programming by Randall Hyde uses HLA for this purpose

Web: https://plantation-productions.com/Webster/

Book: "The Art of Assembly Language Programming" https://plantation-productions.com/Webster/www.artofasm.com/

Portable, Opensource, IA-32, Standard Library: https://sourceforge.net/projects/hla-stdlib/

"12.4 Programming in C/C++ and HLA" in the Linux 32 bit edition: https://plantation-productions.com/Webster/www.artofasm.com/...

... A chapter(s) about wider registers, WASM, and LLVM bitcode etc might be useful?

... Many awesome lists link to OllyDbg and other great resources for ASM; like such as ghidra: https://www.google.com/search?q=ollydbg+site%3Agithub.com+in...


Apart from the resources already mentioned, the book by Yurichev [1] was very helpful. Also, Practical Malware Analysis by Sikorski and Honig [2].

[1] https://beginners.re/

[2] https://www.amazon.com/Practical-Malware-Analysis-Hands-Diss...


Start by writing very simple C code and compiling that to assembly with the C comments. Then follow the System V ABI to learn what registers you need to pay attention to when calling functions. Start small. Once you learn the simple stuff, the more complex stuff is a matter of using it when you run into it.


securitytube linux assembly expert

Focused on offensive security techniques and concepts but taught me a ton and has both x86 and x86-64 architectures. Briefly touched on ARM as well




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: