Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Hmm...

First of all, https://godbolt.org/. C/C++/Rust to assembler, very useful.

Learn the calling conventions, basic arithmetic, flags, conditional branching. Understand stack management.

Single step through functions in a debugger to see how things work, and for example how a stack frame is set up. Pay attention how registers and flags are affected by the execution. See how conditional branches are affected by the flags.

When you're looking at the code, remember that there are often weird looking details, like unused portions in stack and in compiled function codegen (loops, entry points) for alignment purposes — modern CPUs hate unaligned things.

Note: Some x86 instructions have implicit register use that might not be directly obvious. Like PUSH, POP, IMUL, IDIV, LOOP, STOS[BWD], MOVS[BWD], etc. They can affect registers that are not mentioned in the instruction operands.

In general, if the things look weird, just google the instruction. Much less surprises in other mainstream architectures, like ARM. All architectures do have vector instructions that might confuse you at first, like x86 vectored double precision add, VADDPD. Again, just web search them. No one remembers all of the instructions by heart, there's just no point.

Web search for assembler tutorials and simulators. They're too many to list, just pick something suitable for your taste.

In short, play around. Don't get scared by something weird, just look it up.

Don't stress if you don't understand everything, you can always look it up or try it out in a debugger. Even a little bit can help quite a bit.

JITs usually have some way to display generated assembler. For example, to see the native x86/ARM/whatever code generated by JVM you'd say something like:

  java -Xbatch -XX:-TieredCompilation -XX:+PrintCompilation
Maybe throw in -Xcomp for maximum optimization. I don't remember the details, just look them up. :-)

Other JITs have similar ways, once you know this kind of thing exists it should be easy to look it up.



> Again, just web search them. No one remembers all of the instructions by heart, there's just no point.

Another good tip: download the official architecture manuals, they're freely available for the most relevant major architectures (and for x86, both Intel and AMD have their own version). They're monstrous doorstoppers (several volumes with many thousand pages), but include in excruciating detail the description of every single instruction, with the advantage that they'll work even if your Internet connection is offline.


> but include in excruciating detail the description of every single instruction

well, at least the official ones with the officially supported operands. Sandsifter has a lot to say about how many undocumented instructions exist on just the "exposed" x86 part of the CPU, nevermind the ARM and other sub ring-0 stuff.


Yes, but in the context of this thread: undocumented instructions can change from generation to generation of a processor architecture, while documented instructions are much more stable; and because of that, compilers will not generate undocumented instructions. When reading compiler output, the official documentation of the instructions (plus documentation of the calling conventions) should be enough.




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

Search: