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

I don't have a good book to suggest, but one tip you may find helpful:

A typical function has two kinds of assembly code:

(1) The ABI-required logic for functions and function calls, and

(2) Everything else, which can be more or less whatever you want. As long as you don't stomp on the details required by the ABI.



1) is the bit I seem to have trouble finding good resources for. It seems every "intro to assembly" tutorial spends all this time talking about instructions, and never seems to get to something as basic as "how do I create and call a function in assembly?".

Any suggestions there?


I've only had to deal with this on x86-64 Linux, and I worked from this reference document: https://refspecs.linuxbase.org/elf/x86_64-abi-0.99.pdf (section 3.2) [0]

I imagine similar documents exist for each OS-architecture pairing, since the ABI is a convention that (ideally) everyone will agree on.

If you need a definitive answer for some particular OS-architecture pairing, you might ask within a development community that also needs to support that ABI. E.g., gcc, LLVM, lldb, gdb.

[0] IIRC: Even after reading that doc I was unclear on a few details. I got clarification by writing some minimal examples and looking at the assembly that gcc produces. Note that you may need to disable inlining and optimization, or else you might draw the wrong conclusions from the generated assembly.


Thanks for the reply.

Actually, I ended up finding the answer to my question (“why doesn’t every assembly tutorial cover calling conventions on like, page 2?”), and it was because I was coming at this topic from a C perspective.

https://stackoverflow.com/a/17309038

“ If you're writing in assembly language, you can do whatever you want. It's only when you want to interact with some external code (maybe a library, maybe a system call) that you need to obey the calling convention.”


Personally, I found that approach to learning assembly somewhat difficult.

If you're first learning assembly in some little simulator, or on bare metal, I can see the point in deferring any discussion about the ABI until later.

My first assembly project needed to be invoked as a function call from a C/C++ program. So I'd have really benefited from at least a brief explanation about the boilerplate asm you need to be a callable function, how to make function calls / syscalls to print some output, etc.




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

Search: