
Asm-declaration – Embed assembly language code within a C++ program (2017) - ____Sash---701_
https://en.cppreference.com/w/cpp/language/asm
======
siempreme
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.

~~~
pjmlp
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
            }
        }

~~~
tropo
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.

~~~
pjmlp
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.

~~~
tropo
It's not mute or even moot. Assembly intrinsics are not enough.

I've dealt with this, getting software to run in Visual Studio. The intrinsics
are simply not available. You end up running code through gcc to produce
assembly, then hacking up the assembly (way too much to write by hand) into a
separate *.asm file for Visual Studio.

Vector stuff, if it isn't very new, is covered by intrinsics. Well, it is
badly covered, with terrible failures to keep things in registers.

Once you get into exotic OS-level stuff, the intrinsics simply don't exist.
The most important one is the ability to put an arbitrary byte sequence into
the instruction stream. For example, suppose you wanted to add a Spectre fix
to your JIT on day 1. You needed to fix a security problem, so waiting for a
new release of Visual Studio isn't an acceptable option. You really truly need
the ability to put weird byte sequences into the code. Visual Studio doesn't
provide an intrinsic for that.

~~~
pjmlp
For such corner cases using MASM on day 1 until the new VC++ release isn't
much of an issue.

~~~
tropo
MASM is just an assembler, with no awareness of the C or C++ code. MASM is
unable to place arbitrary bytes into the middle of functions that are written
in C or C++.

To fix a problem like Spectre, and generally to solve unusual problems that
the compiler vendor isn't dealing with, the full capability of an inline
assembler is required.

~~~
pjmlp
Sure it can, I really don't see any difference between that and a naked call
with LTO, beyond the convenience of saving about a minute not having to write
a prototype for the function being called and adding MASM into the build.

------
clouddrover
Lots of languages have inline assembly. Pascal does:

\- Free Pascal: [https://www.freepascal.org/docs-
html/prog/progch3.html#progs...](https://www.freepascal.org/docs-
html/prog/progch3.html#progse9.html)

\- Delphi:
[http://docwiki.embarcadero.com/RADStudio/Rio/en/Inline_Assem...](http://docwiki.embarcadero.com/RADStudio/Rio/en/Inline_Assembly_Code_Index)

You can even do assembly in batch files via Debug if you really want to:

[https://thestarman.pcministry.com/asm/debug/debug2.htm](https://thestarman.pcministry.com/asm/debug/debug2.htm)

[https://www.robvanderwoude.com/debug.php](https://www.robvanderwoude.com/debug.php)

~~~
tyingq
Perl's Inline::ASM is pretty neat.
[https://metacpan.org/pod/distribution/Inline-
ASM/ASM.pod](https://metacpan.org/pod/distribution/Inline-ASM/ASM.pod)

------
bregma
Odd bit of news reporting on something that was part of a standard published
in 1997. What's next, breaking news on a standard 7-bit code used by Americans
for information interchange?

~~~
mikeash
This is a link to a reference page, not a news report.

~~~
bregma
> This is a link to a reference page, not a news report.

I don't know about you, but to me the name "Hacker News" implies what I read
here is a news report, not a reference work of any sort.

------
haberman
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.

~~~
____Sash---701_
I believe it was discussed in Cologne, 'Enabling Constexpr Intrinsics......'
\- [http://www.open-
std.org/jtc1/sc22/wg21/docs/papers/2019/p166...](http://www.open-
std.org/jtc1/sc22/wg21/docs/papers/2019/p1668r0.html)

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

~~~
paulirwin
Even as a C++ amateur I didn't find it as a meaty or intimidating proposal,
rather it was refreshingly brief. If you understand that constexpr functions
can be evaluated at either run time or compile time, it makes sense that asm
isn't allowed in a compile-time context (it could get really trippy if it
was!). This allows for flow analysis to permit asm when evaluating at runtime
within a constexpr function, as long as there's a non-asm constexpr
alternative path available at compile time. If I'm misunderstanding or missing
some subtleties, as a novice I'd appreciate any corrections.

------
dvt
Years ago, I started work on a library that does this in Java:
[https://github.com/dvx/jssembly](https://github.com/dvx/jssembly)

The syntax looks like this:

    
    
        jsm.define("test", new Block(x64) {{
            __asm(
                "nop",  // no-op
                "ret"   // return
            );
        }}).invoke();

------
basementcat
Tangentially related but in gcc and clang you can jump to or call machine
language embedded within string literals.

[https://stackoverflow.com/questions/48593734/calling-goto-
on...](https://stackoverflow.com/questions/48593734/calling-goto-on-a-wide-
char-string-in-c-gcc/49063073#49063073)

[https://codegolf.stackexchange.com/questions/2203/tips-
for-g...](https://codegolf.stackexchange.com/questions/2203/tips-for-golfing-
in-c/104999#104999)

This is _not_ recommended for anything in production but is an amusing parlor
trick.

Edit: Recent toolchains may require the -zexecstack flag to avoid a segfault.

------
mbel
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" :)

~~~
mikeash
Hacker News Guidelines

What to Submit

On-Topic: Anything that good hackers would find interesting. That includes
more than hacking and startups. If you had to reduce it to a sentence, the
answer might be: anything that gratifies one's intellectual curiosity.

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

~~~
nurettin
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.

~~~
AdmiralAsshat
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.

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

~~~
ndesaulniers
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.

------
mud_dauber
I was a Forth dev a long time ago (on a planet far away...). Switching to &
from assembly seemed as natural as breathing. Oh for the days of 16bit cores
and minuscule register banks.

------
lawlessone
did this in college over 10 years ago..

