
Ask HN: How did you learn x86-64 assembly? - spacechild1
I&#x27;m an experienced C&#x2F;C++ programmer and I occasionally look at the generated assembly to check for optimizations, loop unrolling, vectorization, etc. I understand what&#x27;s going on the surface level, but I have a hard time understand what&#x27;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.<p>Are there any good books or tutorials you can recommend which go beyond the very beginner level?
======
sitkack
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/](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](https://en.wikipedia.org/wiki/GotoBLAS)

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

[3] [https://stackoverflow.com/questions/3867721/is-there-any-
c-c...](https://stackoverflow.com/questions/3867721/is-there-any-c-compiler-
to-show-you-linked-program-in-asm)

------
moon2
Programming from Ground Up, by Jonathan Bartlett.

[https://download-
mirror.savannah.gnu.org/releases/pgubook/Pr...](https://download-
mirror.savannah.gnu.org/releases/pgubook/ProgrammingGroundUp-1-0-booksize.pdf)

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](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.

------
ecaradec
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](https://www.agner.org/optimize/#manuals)

------
hackermailman
This gives you somewhat of an idea of what that highly optimized assembly is
doing [https://ocw.mit.edu/courses/electrical-engineering-and-
compu...](https://ocw.mit.edu/courses/electrical-engineering-and-computer-
science/6-172-performance-engineering-of-software-systems-fall-2018/) and the
lectures are recorded.

~~~
spacechild1
this is gold, thank you!

------
recursivedoubts
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/](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.

~~~
spacechild1
thanks, this looks really nice and condensed.

------
stevekemp
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/math-compiler/)

[https://github.com/skx/bfcc/](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.

------
jmalicki
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...](https://www.amazon.com/Hackers-Delight-2nd-Henry-
Warren/dp/0321842685)

~~~
spacechild1
oh, this looks like a fun book!

------
enjoy-your-stay
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.

------
CawCawCaw
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/](https://beginners.re/)

[2] [https://www.amazon.com/Practical-Malware-Analysis-Hands-
Diss...](https://www.amazon.com/Practical-Malware-Analysis-Hands-
Dissecting/dp/1593272901)

------
Jugurtha
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.

------
westurner
High Level Assembly (HLA)
[https://en.wikipedia.org/wiki/High_Level_Assembly](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/](https://plantation-
productions.com/Webster/)

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

Portable, Opensource, IA-32, Standard Library:
[https://sourceforge.net/projects/hla-
stdlib/](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/...](https://plantation-
productions.com/Webster/www.artofasm.com/Linux/HTML/MixedLanguageProgramming3.html#1030674)

... 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...](https://www.google.com/search?q=ollydbg+site%3Agithub.com+inurl%3Aawesome)

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

[https://software.intel.com/sites/landingpage/IntrinsicsGuide...](https://software.intel.com/sites/landingpage/IntrinsicsGuide/)

[http://ref.x86asm.net/](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.

------
blackbear_
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/](https://www.cs.yale.edu/flint/cs422/doc/art-of-asm/pdf/)

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

------
thekingofh
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.

------
oceanghost
It's a bit outdated now... but

[http://www.s100computers.com/Software%20Folder/6502%20Monito...](http://www.s100computers.com/Software%20Folder/6502%20Monitor/The%20Telemark%20Assembler%20Manual.pdf)

------
kkirsche
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

