News for hackers. Stuff that matters.
Re:(Debugging Story) Inline Assembly Dangers by pjmlp on Monday June 10, 2024 @01:56AM (#40630639)
And this is why inline Assembly done by GCC and clang sucks, and the inline Assembly done by most PC compilers, with first level support for opcodes, or instrisics is a great experience.
Re:(Debugging Story) Inline Assembly Dangers by SassyBird on Monday June 10, 2024 @02:41AM (#40630858)
It seems that MSVC supports inline assembly only on 32-bit x86. For amd64 and ARM they moved entirely to intrinsics.
Re:(Debugging Story) Inline Assembly Dangers by vnorilo on Monday June 10, 2024 @02:59AM (#40630958)
The more I've written code close to metal (mostly SIMD for signal processing), the more I've grown to prefer either intrinsics or separate translation unit for assembly.

If you want your code to intertwine with what the C compiler does, intrinsics are great.

If you don't, .s is great.

Re:(Debugging Story) Inline Assembly Dangers by pjmlp on Monday June 10, 2024 @03:49AM (#40631197)
Exactly, this is the right approach.
Re:(Debugging Story) Inline Assembly Dangers by immibis on Monday June 10, 2024 @10:04AM (#40633820)
And the only way to use new instructions is to make them slow or to wait for a compiler update.
Re:(Debugging Story) Inline Assembly Dangers by pjmlp on Monday June 10, 2024 @03:49AM (#40631201)
Yes, Assembly programming concatenanting strings, is horrible.

Intrisics give the best of both worlds, without dealing with string based content.

Actually ESPOL/NEWP from 1961 was one of the very first system programming languages with two key inovations, intrisics and unsafe code blocks.

Re:(Debugging Story) Inline Assembly Dangers by SassyBird on Monday June 10, 2024 @08:11AM (#40632721)
I’m not a fan of concatenating strings either. I was thinking about the MSVC/Pascal-style inline assembly here, which isn’t based on strings.

Correct me if I’m wrong, but I don’t think that you can guarantee a routine is constant-time by using intrinsics. You need asm for that. Asm that won’t be changed by the compiler. So you need to write an external asm file for that now, which is fair enough, but I just wouldn’t present intrinsics as all-around superior.

Re:(Debugging Story) Inline Assembly Dangers by pjmlp on Monday June 10, 2024 @09:42AM (#40633617)
Yes, if you want full control, only external Assemblers will do the trick.
Re:(Debugging Story) Inline Assembly Dangers by ack_complete on Monday June 10, 2024 @12:04PM (#40635033)
Yes, and the set of intrinsics supported by MSVC is really lacking. On x64, this left no way to force a conditional move to bypass a poorly predictable branch, nor was there a way to access carry-extended operations until recently. On ARM64, it doesn't have complete coverage of scalar intrinsics for operations like RBIT (reverse bit order).
Re:(Debugging Story) Inline Assembly Dangers by clausecker on Monday June 10, 2024 @05:39AM (#40631750)
It's great experience, but it's also slow as the compier must save the entire function state before your assembly block and cannot make any assumptions about what you do inside.

This is acceptable if you see the point of inline assembly in writing long code sections, but that's not what inine assembly is meant to solve. If you want to write long sections of assembly code, just put the whole thing into an assembly source file and link it into your binary.

gcc-style inline assembly on the other hand is designed to solve the problem of augmenting code generation with individual instructions the compiler doesn't know about. Correctly used, gcc-style asm statements typically only hold one or two instructions that are then integrated into a complex function with little to no overhead. And for this usage, the syntax and model provided by gcc is perfectly fine. It only fails if you misuse it for writing long chunks of assembly code, something for which you should have used an assembly file in the first place.

Re:(Debugging Story) Inline Assembly Dangers by pjmlp on Monday June 10, 2024 @07:27AM (#40632398)
> It's great experience, but it's also slow as the compier must save the entire function state before your assembly block and cannot make any assumptions about what you do inside.

That is why there are function attributes and #pragmas to control what the compiler is supposed to do.

Compilers that don't follow the brainded execution model of UNIX, with hard separation between compiler and Assembler phases, as separate processes comunicating via pipes, are a bit more knowledgeable of what those Assembly instructions are actually doing.

Not only that, they are able to provide proper error messages when the opcodes are badly used.

Re:(Debugging Story) Inline Assembly Dangers by cesarb on Monday June 10, 2024 @08:12AM (#40632730)
> [...] are a bit more knowledgeable of what those Assembly instructions are actually doing. Not only that, they are able to provide proper error messages when the opcodes are badly used.

That's only possible if the compiler knows all assembly instructions being used. But the whole point of GCC-style inline assembly is to be able to use assembly instructions the compiler doesn't know about (for instance, because they're new instructions which didn't exist when the compiler was released), with the same performance as the ones the compiler does know about. In many cases, even the assembler doesn't know about the new instruction, and you have to specify it using .byte directives or similar (though the Unix compiler model, with proper separation between the compiler and the assembler, also allows you to use a newer assembler without having to change your compiler).

Re:(Debugging Story) Inline Assembly Dangers by pjmlp on Monday June 10, 2024 @09:41AM (#40633600)
Again, this kind of arguments gets tiring, compilers get upgrades.
Re:(Debugging Story) Inline Assembly Dangers by leni536 on Monday June 10, 2024 @10:31AM (#40634062)
And GCC/clang do get intrinsics that are nicer to use than inline asm, often with more optimization available to them as well.

If the compiler does know about a specific instruction, then it's better to just provide the intrinsic than try to infer it from some inline asm string.

Re:(Debugging Story) Inline Assembly Dangers by AshamedCaptain on Monday June 10, 2024 @11:03AM (#40634398)
One of the benefits of gcc's powerful inline asm support is that it allows you to write new intrinsics. This is almost impossible in most other compilers, as they will do crazy things such as flushing out all registers before each asm block. Very useful for low level work.
Re:(Debugging Story) Inline Assembly Dangers by leni536 on Monday June 10, 2024 @11:45AM (#40634808)
> it allows you to write new intrinsics

I admit that it gets very close, however I think compiler provided intrinsics can be and are optimized more than ones that are implemented with inline asm, as the compiler knows about the semantics of the instruction for such an intrinsic, and not much about an inline asm one, apart from inputs/outputs/clobbers.

Having said that I agree that inline asm is very useful for instructions that the compiler does not know about.

Re:(Debugging Story) Inline Assembly Dangers by secondcoming on Monday June 10, 2024 @11:05AM (#40634427)
That's great if you're not locked down to a particular compiler version
Re:(Debugging Story) Inline Assembly Dangers by AshamedCaptain on Monday June 10, 2024 @09:30AM (#40633466)
> That is why there are function attributes and #pragmas to control what the compiler is supposed to do.

Which basically reinvent gcc's syntax to set input/output/modified registers, only worse. No thank you.

The compiler you are most likely thinking off still does not allow to set multiple output values from an inline asm block, for example. Something the gcc/clang syntax has no problems with. These details all make the "ffi"/interface between the C world and your inline asm slower than it could be.

Re:(Debugging Story) Inline Assembly Dangers by pjmlp on Monday June 10, 2024 @09:40AM (#40633594)
No thank you, to pasting strings.
Re:(Debugging Story) Inline Assembly Dangers by AshamedCaptain on Monday June 10, 2024 @10:04AM (#40633828)
But yes to pragmas with register lists ? Be serious.
Re:(Debugging Story) Inline Assembly Dangers by Retr0id on Monday June 10, 2024 @09:06AM (#40633195)
What is a PC compiler in this context? MSVC?
Re:(Debugging Story) Inline Assembly Dangers by pjmlp on Monday June 10, 2024 @09:47AM (#40633673)
Almost all MS-DOS, OS/2, Windows 16 bit, Windows 32 bit and Windows 64 bit compilers you can think of, that aren't GCC or clang forks with their UNIX architecture, and support inline Assembly.

For C, C++, BASIC, Pascal, Delphi, Modula-2, Oberon-2, Component Pascal, D,...

Traditionally PC refers to IBM/Microsoft's linage of computers and operating systems.