logoalt Hacker News

bigfishrunningtoday at 12:24 PM2 repliesview on HN

I would argue that inline assembly, while not in the standard, is really just a convenience feature -- it is part of the standard to declare an extern reference to a function in the symbol table and jump to it, it just requires a separate ASM object to link alongside your C object. Inline assembly doesn't allow anything you can't do without it.


Replies

jonhohletoday at 2:25 PM

What does an external function have to do with the C supporting or not supporting ASM? The symbol is just a symbol from another object. That could be written in any language that follows the ABI (not that ASM has any enforcement of ABi to begin with). The symbol resolves to an address and nothing more.

Saying inline ASM is no different than a function call is like saying standard control structures are no different from function calls. I suppose from a Smalltalk perspective that could be true, but is that the mental model most programmers use?

I work on a system from the 90s with custom instructions. GNU-as was patched to understand the instructions. They’re used through macros that ultimately expand to inline ASM. Without this, you’d need function inlining, which may or may not be possible with a linked object (it certainly wasn’t standard in the 90s). So now a single instruction turns into stack management, a jump, more stack management and a return. At that point any benefit to a specialized instruction may be erased, or in the case I’m dealing with talking to external hardware becomes unreasonably expensive.

convolvatrontoday at 1:07 PM

that's kind of not true. inline asm lets me refer to the register that the compiler placed a value in.

lets say I really want to use popcnt in my inner loop. with inline assembly I can just shove it in there. external linkage forces a function call overhead that can't be inlined, which obviates any benefit I might have had from using the specialized instruction.

show 1 reply