top | item 20533895

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

45 points| ____Sash---701_ | 6 years ago |en.cppreference.com | reply

51 comments

order
[+] siempreme|6 years ago|reply
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|6 years ago|reply
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
        }
    }
[+] snarfy|6 years ago|reply
It gives me nostalgia seeing mode 13 graphics. What's next, mode x? Triangle drawing routines? :)
[+] dfeojm-zlib|6 years ago|reply
Ah yes. And Borland Pascal. Mode X VGA hackery in unreal mode. Later, when I worked for a software store in high school, I acquired Borland C++ 3.1, the physically-largest and heaviest (27 lbs / 12.2 kg) retail software package that I know of. It was a small software shop, they gave us crazy discounts, vendors gave us NFRs and they let us borrow anything on the shelf (trusty-dusty shrinkwrap machine). The profiler, debugger, and assembler were also good as there were protected-mode variants that could sometimes keep the machine from crashing.
[+] gumby|6 years ago|reply
> In a string literal?!?!

It's a thing literal as it is literally dumped straight into the generated .s file by the compiler, as most C compilers don't do the actual assembly or generation of object file.

Of course these days it's not literally dumped in unmodified -- various compilers do substitutions for you. But that's the legacy.

[+] clouddrover|6 years ago|reply
Lots of languages have inline assembly. Pascal does:

- Free Pascal: https://www.freepascal.org/docs-html/prog/progch3.html#progs...

- Delphi: http://docwiki.embarcadero.com/RADStudio/Rio/en/Inline_Assem...

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

https://thestarman.pcministry.com/asm/debug/debug2.htm

https://www.robvanderwoude.com/debug.php

[+] mhh__|6 years ago|reply
D has a very nice inline assembler (assuming you're on a supporter platform, if not then you get to use the standard gnu style)
[+] bregma|6 years ago|reply
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|6 years ago|reply
This is a link to a reference page, not a news report.
[+] haberman|6 years ago|reply
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.

[+] stevekemp|6 years ago|reply
Trial and error is valuable, but if you go to github and search for "movq", "ld a,(hl)", or similar string you can often find examples of code that is presumably working.

I'm reminding myself of Z80 assembly at the moment, building a simple computer and I've done a bit of that.

[+] dvt|6 years ago|reply
Years ago, I started work on a library that does this in Java: https://github.com/dvx/jssembly

The syntax looks like this:

    jsm.define("test", new Block(x64) {{
        __asm(
            "nop",  // no-op
            "ret"   // return
        );
    }}).invoke();
[+] mbel|6 years ago|reply
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|6 years ago|reply
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.

[+] asveikau|6 years ago|reply
It's not technically a feature of the language, as it is not standardized and is compiler-specific.

But thing you are talking about indeed recurs here. The way I typically guess is that new generations of programmers are coming up all the time and may be unfamiliar with some old stuff. Or, maybe even some experienced people who never happened to touch c or c++ [there are more of those as time goes on].

[+] AdmiralAsshat|6 years ago|reply
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.

[+] raviolo|6 years ago|reply
The real question remains unanswered: why is this on the front page?
[+] nurettin|6 years ago|reply
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.
[+] bla3|6 years ago|reply
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|6 years ago|reply
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|6 years ago|reply
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|6 years ago|reply
did this in college over 10 years ago..