Live data from Hacker News

Asm-declaration – Embed assembly language code within a C++ program (2017)

en.cppreference.com

21–30 of 53 posts

Re: Asm-declaration – Embed assembly language code within a C++ program (2017)

#22
post #14

Earlier quoted context omitted.

For some cultural reason C and C++ compilers for UNIX do it the hard way. PC compilers always followed that path. The same in Turbo C would be: void init() { asm { mov ax, 0x13 int 0x10 } }

Also Rust uses strings for asm. It can't be bad.

It’s a fairly major point of contention, and we may not keep it. Note that it’s not a stable feature yet. As pointed out below, it’s this way because it’s basically a convenience into LLVM.

Re: Asm-declaration – Embed assembly language code within a C++ program (2017)

#23
post #3

I don't see anything on that page referencing C++20. Am I missing something? On a related note, I always find the official docs for GCC inline assembly are insufficient for figuring out what I am trying to do. I nearly always have to resort to dumb trial and error. I was just recently planning to write some docs of my own on the subject. Not tutorial docs, but reference docs.

I believe it was discussed in Cologne, 'Enabling Constexpr Intrinsics......' - http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p166...

Quite a meaty document, should provide you with help for material on the subject.

Re: Asm-declaration – Embed assembly language code within a C++ program (2017)

#24
post #8

Maybe what's new here is the suggestion of using a raw string literal for the assembly source? That does look neat (if minor).

I wonder if that has anything to do with not having to then use escaped newlines and tabs. Without newlines, multiple instructions may fail to parse in the assembler, tabs are just for readability when printing asm rather than assembling.

Re: Asm-declaration – Embed assembly language code within a C++ program (2017)

#25

Earlier quoted context omitted.

Also Rust uses strings for asm. It can't be bad.

It’s a fairly major point of contention, and we may not keep it. Note that it’s not a stable feature yet. As pointed out below, it’s this way because it’s basically a convenience into LLVM.

Keep it.

Porting to rust is easier if it doesn't require a person to be an expert at two different kinds of inline assembly syntax. Being able to grab a chunk of inline assembly from a C project is very useful.

Whatever you do, don't embed knowledge of the assembly language into the compiler. That way lies madness. Assembly is often used for new CPU features that are not yet supported by the compilers that people are using. Constantly putting out minor compiler updates for every CPU revision would be miserable, and the users won't want to force those upgrades anyway.

There is also the issue, I'm sorry, of the rust preprocessor. It will be written and it will be used. It may even be popular and ultimately written into an ISO standard. The irregularity of switching suddenly to a radically different CPU-specific syntax for assembly code would make the preprocessor situation much more nasty and gross.

Re: Asm-declaration – Embed assembly language code within a C++ program (2017)

#26
post #2

Old, well-known feature of one of the most popular languages. I must be missing something, because I have no idea how this became a hacker "news" :)

I'm scratching my head, too. I could've sworn the Linux kernel itself had a few chunks of hand-written assembly for better performance in key spots.

EDIT: I see several, just from doing a simple search against 'movq'

https://github.com/torvalds/linux/search?q=movq&unscoped_q=m...

I am not a kernel programmer, though, so I can't say whether it's being used in any capacity on modern, x64 systems or whether it's a compatibility mode for low-powered embedded architectures. Maybe someone more knowledgeable can chime in.

Re: Asm-declaration – Embed assembly language code within a C++ program (2017)

#27
post #14

In a string literal?!?! We did better almost 40 years ago..., this is assembly in turbo pascal: procedure init; assembler; asm mov ax,13h int 10h end; You can even have only the asm block in a regular pascal function and make use of regular arguments in the asm block.

For some cultural reason C and C++ compilers for UNIX do it the hard way. PC compilers always followed that path. The same in Turbo C would be: void init() { asm { mov ax, 0x13 int 0x10 } }

The style used by Turbo C is strictly inferior. There is no way for the author of the assembly code to cooperate with the compiler's register allocator or instruction scheduler.

You wouldn't even need both of those lines with gcc. You could ask the compiler to put 0x13 into ax, and the compiler might schedule that instruction far earlier or even take advantage of the value already being in the register by luck.

Turbo C makes a simplistic assumption about what registers might have been trashed. With gcc, the compiler knows because you told it. Turbo C must save things to memory before the assembly and then reload registers afterward, which adds enough slowness that the assembly might not even be worthwhile.

Re: Asm-declaration – Embed assembly language code within a C++ program (2017)

#28
post #15

The real question remains unanswered: why is this on the front page?

There is no QC check, it works on votes. I think anything could end up here. Which casts quite a bit of shadow over what has been here.

It does not work entirely on votes. The mods have quite a bit of weight in deciding what gets pushed to the front page.

For example, there's an article about gorillas on the front page right now that has exactly 7 points. HN is nowhere near that anemic that 7 votes would be enough to push to the front page on its own.

To boot, I once submitted something that went unnoticed. An HN mod then emailed me and said it looked cool, asking me to resubmit it again so that it could get more prominent featuring.

Re: Asm-declaration – Embed assembly language code within a C++ program (2017)

#29
post #25

Earlier quoted context omitted.

It’s a fairly major point of contention, and we may not keep it. Note that it’s not a stable feature yet. As pointed out below, it’s this way because it’s basically a convenience into LLVM.

Keep it. Porting to rust is easier if it doesn't require a person to be an expert at two different kinds of inline assembly syntax. Being able to grab a chunk of inline assembly from a C project is very useful. Whatever you do, don't embed knowledge of the assembly language into the compiler. That way lies madness. Assembly is often used for new CPU features that are not yet supported by the compilers that people are…

> Being able to grab a chunk of inline assembly from a C project is very useful.

Wasn't Rust's current inline assembly syntax subtly different from the gcc-compatible one you'd find in most C projects? IIRC, it uses the syntax from the LLVM IR to specify inputs and outputs, instead of the syntax from GCC inline assembly.

Re: Asm-declaration – Embed assembly language code within a C++ program (2017)

#30
post #27
post #14

Earlier quoted context omitted.

For some cultural reason C and C++ compilers for UNIX do it the hard way. PC compilers always followed that path. The same in Turbo C would be: void init() { asm { mov ax, 0x13 int 0x10 } }

The style used by Turbo C is strictly inferior. There is no way for the author of the assembly code to cooperate with the compiler's register allocator or instruction scheduler. You wouldn't even need both of those lines with gcc. You could ask the compiler to put 0x13 into ax, and the compiler might schedule that instruction far earlier or even take advantage of the value already being in the register by luck. Turbo…

Turbo C was a compiler done in 1990 for MS-DOS, naturally it was just an example.

Your remarks are a mute point in modern Windows compilers with Assembly intrinsics, the evolution of those inline assembly instructions.

Post reply on HN