6 ms·
Asm-declaration – Embed assembly language code within a C++ program (2017)
- mbel 7y agoOld, 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 7y agoHacker 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.
- entelechy0 7y agoyeah, people don't realize that not everyone knows everything already
- asveikau 7y agoIt'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 7y agoI'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=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.
- asveikau 7y agoIn 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.
- haberman 7y agoI 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_ 7y agoI believe it was discussed in Cologne, 'Enabling Constexpr Intrinsics......' - http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1668r0.html 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.
- paulirwin 7y agoEven 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.
- stevekemp 7y agoTrial 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.
- bregma 7y agoOdd 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?
- mud_dauber 7y agoI 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.
- clouddrover 7y agoLots of languages have inline assembly. Pascal does: - Free Pascal: https://www.freepascal.org/docs-html/prog/progch3.html#progse9.html https://www.freepascal.org/docs-html/prog/progch3.html#progs... - Delphi: http://docwiki.embarcadero.com/RADStudio/Rio/en/Inline_Assembly_Code_Index 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://thestarman.pcministry.com/asm/debug/debug2.htm https://www.robvanderwoude.com/debug.php https://www.robvanderwoude.com/debug.php
- tyingq 7y agoPerl's Inline::ASM is pretty neat. https://metacpan.org/pod/distribution/Inline-ASM/ASM.pod https://metacpan.org/pod/distribution/Inline-ASM/ASM.pod
- mhh__ 7y agoD has a very nice inline assembler (assuming you're on a supporter platform, if not then you get to use the standard gnu style)
- lawlessone 7y agodid this in college over 10 years ago..
- bla3 7y agoMaybe what's new here is the suggestion of using a raw string literal for the assembly source? That does look neat (if minor).
- ndesaulniers 7y agoI 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.
- siempreme 7y agoIn 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.
- snarfy 7y agoIt gives me nostalgia seeing mode 13 graphics. What's next, mode x? Triangle drawing routines? :)
- nurettin 7y agonext, we implement bresenham's algorithms, then we embed antialiasing.
- pan69 7y agoI'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...
- jsd1982 7y agoCool! Got a public repo of code? What toolchain are you using?
- pan69 7y agoSorry, no public repo, yet. Once I have something coherent working I will definitely publish it. I'm using Watcom C v11. I actually bought this somewhere in the 90's. However, the Watcom tool chain is still being developed as Open Watcom. So, great for 32bit DOS: https://github.com/open-watcom https://github.com/open-watcom
- raviolo 7y agoThe real question remains unanswered: why is this on the front page?
- nurettin 7y agoThere 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 7y agoIt 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.
- dvt 7y agoYears 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 7y agoTangentially 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-a-wide-char-string-in-c-gcc/49063073#49063073 https://stackoverflow.com/questions/48593734/calling-goto-on... https://codegolf.stackexchange.com/questions/2203/tips-for-golfing-in-c/104999#104999 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.