Live data from Hacker News

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

en.cppreference.com

41–50 of 53 posts

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

#41
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://codegolf.stackexchange.com/questions/2203/tips-for-g...

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.

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

#42
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…

That has nothing to do with their actual point; Turbo C could perfectly well support:

  void init() {
    asm("ax"(0x13)) { int 0x10 }
    }
and as TFA suggests, plenty of implementations use the string-syntax without any register/clobber extensions.

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

#43
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 comp…

In the case of the kernel it's not just a performance thing. There are a lot of things that are totally irrelevant to the high level notion of the C execution model that are therefore not exposed, and certainly not in the C standard.

Things like: Hm, I need to swap my stack register and page table with this other process.

Or writing interrupt handlers.

Atomic operations and memory barriers used to be one of those things, but compiler extensions and new language standards have been catching up on some of that... Though to be honest a kernel will want enough control that it will likely still go outside the standard or extra compiler support for these anyway.

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

#44
post #25

Earlier quoted context omitted.

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…

> Porting to rust is easier if it doesn't require a person to be an expert at two different kinds of inline assembly syntax. Sadly, this doesn't save you from that, in fact, it can be argued that the string syntax is what makes you need to learn a whole second set of syntax. This is due to clobbers. > There is also the issue, I'm sorry, of the rust preprocessor. It will be written and it will be used. I don't forsee…

> nobody has invented one yet.

no need to invent one when cpp exists and works. I've seen people use it in Java, and it is sometimes used in unix config files (e.g. .Xresources).

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

#45

Earlier quoted context omitted.

> Porting to rust is easier if it doesn't require a person to be an expert at two different kinds of inline assembly syntax. Sadly, this doesn't save you from that, in fact, it can be argued that the string syntax is what makes you need to learn a whole second set of syntax. This is due to clobbers. > There is also the issue, I'm sorry, of the rust preprocessor. It will be written and it will be used. I don't forsee…

> nobody has invented one yet. no need to invent one when cpp exists and works. I've seen people use it in Java, and it is sometimes used in unix config files (e.g. .Xresources).

Sure; I'm not aware of anybody using cpp with Rust either.

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

#46

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.

Assembler? Inline LLVM intermediate representation might be extremely useful instead, for many applications, see:

https://idea.popcount.org/2013-07-24-ir-is-better-than-assem...

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

#47
post #12

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.

It gives me nostalgia seeing mode 13 graphics. What's next, mode x? Triangle drawing routines? :)

I've actually picked this up as a hobby since a year ago or so.

I grew up in the late 80's early 90's doing exactly this type of thing as a kid. Having worked as a professional dev for the past few decades I started to notice that I've been taking my work home for all that time, and even though I like my work, I felt I needed a hobby as I'm getting older.

So, I'm currently making a shooting game for the 286/16 in modex VGA with SoundBlaster digitised sound and Adlib for music. All programmed from scratch...

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

#48
post #39
post #35

Earlier quoted context omitted.

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…

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

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.

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

#49

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.

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

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

#50
post #47
post #12

Earlier quoted context omitted.

It gives me nostalgia seeing mode 13 graphics. What's next, mode x? Triangle drawing routines? :)

I've actually picked this up as a hobby since a year ago or so. I grew up in the late 80's early 90's doing exactly this type of thing as a kid. Having worked as a professional dev for the past few decades I started to notice that I've been taking my work home for all that time, and even though I like my work, I felt I needed a hobby as I'm getting older. So, I'm currently making a shooting game for the 286/16 in mod…

Cool! Got a public repo of code? What toolchain are you using?
Post reply on HN