{"id":1523,"date":"2026-05-07T10:25:03","date_gmt":"2026-05-07T07:25:03","guid":{"rendered":"https:\/\/biyer.com.tr\/?p=1523"},"modified":"2026-05-07T10:25:03","modified_gmt":"2026-05-07T07:25:03","slug":"implementing-c-style-async-await-in-raw-x86-64-assembly-lessons-from-building-the-fluxsharp-compiler","status":"publish","type":"post","link":"https:\/\/biyer.com.tr\/?p=1523","title":{"rendered":"Implementing C#-style Async\/Await in raw x86-64 Assembly: Lessons from building the FluxSharp compiler"},"content":{"rendered":"<p><!-- SC_OFF --><\/p>\n<div class=\"md\">\n<p>Hi everyone,<\/p>\n<p>I\u2019ve spent the last few months building <strong>FluxSharp<\/strong>, a systems programming language that brings C# syntax (Classes, Inheritance, Async) to a bare-metal environment. The compiler is written in Rust using the <code>Pest<\/code> PEG parser, but instead of targeting LLVM or a VM, it generates raw <strong>x86-64 Assembly (NASM)<\/strong>.<\/p>\n<p>I wanted to share some technical insights and challenges I faced, specifically regarding the async\/await implementation without a heavy runtime.<\/p>\n<p><strong>1. The &quot;No-Runtime&quot; Async Challenge<\/strong> Unlike the .NET CLR, FluxSharp doesn&#039;t have a managed thread pool. To make <code>async\/await<\/code> work, I had to implement a custom <strong>State Machine<\/strong> at the assembly level.<\/p>\n<ul>\n<li>When a function is marked <code>async<\/code>, the compiler transforms its AST into a series of states.<\/li>\n<li>The &quot;Promise&quot; system is actually a struct in memory that holds a function pointer (the continuation) and a context pointer.<\/li>\n<li><strong>The Event Loop:<\/strong> I wrote a minimal event loop in ASM that polls these promises. When an I\/O operation (or a timer) completes, the loop restores the registers and jumps to the continuation address.<\/li>\n<\/ul>\n<p><strong>2. Direct x86-64 Code Generation<\/strong> Since I&#039;m not using LLVM, I have to handle register allocation and stack frames manually.<\/p>\n<ul>\n<li><strong>Calling Convention:<\/strong> I&#039;m following a modified version of the System V AMD64 ABI.<\/li>\n<li><strong>Safety:<\/strong> To implement null-safety and bounds checking, the compiler injects <code>CMP<\/code> and <code>JE\/JNE<\/code> instructions before every array access or object dereference. If it fails, it triggers a syscall to exit with a specific error code.<\/li>\n<\/ul>\n<p><strong>3. Parsing with Pest (Rust)<\/strong> Using PEG (Parsing Expression Grammars) via the <code>Pest<\/code> crate was a game changer for the C#-like syntax. However, mapping the concrete syntax tree (CST) to a lean AST for ASM generation required a recursive descent pass that collapses nested expressions to avoid stack overflows during compilation of complex logic.<\/p>\n<p><strong>Technical Stack:<\/strong><\/p>\n<ul>\n<li><strong>Frontend:<\/strong> Rust + Pest (Grammar)<\/li>\n<li><strong>Backend:<\/strong> Custom x86-64 ASM Generator<\/li>\n<li><strong>Assembler\/Linker:<\/strong> NASM &amp; LD<\/li>\n<li><strong>Security:<\/strong> Built-in overflow protection and bounds checking at the instruction level.<\/li>\n<\/ul>\n<p>I&#039;m particularly interested in hearing from anyone who has worked on <strong>Assembly-level coroutines<\/strong>. My current approach saves the minimal set of registers (RBX, RSP, RBP, R12-R15), but I&#039;m curious if there are more efficient ways to handle the &quot;State Machine&quot; transition in raw ASM without bloating the binary size.<\/p>\n<p>The project is open-source (MIT), and you can see the compiler source and the generated ASM output in the repo:<a href=\"https:\/\/github.com\/Yvan4001\/FluxSharp\">https:\/\/github.com\/Yvan4001\/FluxSharp<\/a><\/p>\n<p>Full documentation on the architecture is here:<a href=\"https:\/\/flux-sharp.sivagames.eu\/docs\/\">https:\/\/flux-sharp.sivagames.eu\/docs\/<\/a><\/p>\n<p>Looking forward to your technical feedback!<\/p>\n<\/p><\/div>\n<p><!-- SC_ON --> &#032; submitted by &#032; <a href=\"https:\/\/www.reddit.com\/user\/Drenfa\"> \/u\/Drenfa <\/a> <br \/> <span><a href=\"https:\/\/flux-sharp.sivagames.eu\/\">[link]<\/a><\/span> &#032; <span><a href=\"https:\/\/www.reddit.com\/r\/programming\/comments\/1t62u51\/implementing_cstyle_asyncawait_in_raw_x8664\/\">[comments]<\/a><\/span><\/p>\n<p><a href=\"https:\/\/www.reddit.com\/r\/programming\/comments\/1t62u51\/implementing_cstyle_asyncawait_in_raw_x8664\/\" target=\"_blank\">Orijinal Kayna\u011fa Git<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi everyone, I\u2019ve spent the last few months building FluxSharp, a systems programming language that brings C# syntax (Classes, Inheritance, Async) to a bare-metal environment. The compiler is written in Rust using the Pest PEG parser, but instead of targeting LLVM or a VM, it generates raw x86-64 Assembly (NASM). I wanted to share some [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1523","post","type-post","status-publish","format-standard","hentry","category-genel"],"_links":{"self":[{"href":"https:\/\/biyer.com.tr\/index.php?rest_route=\/wp\/v2\/posts\/1523","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/biyer.com.tr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/biyer.com.tr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/biyer.com.tr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/biyer.com.tr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1523"}],"version-history":[{"count":1,"href":"https:\/\/biyer.com.tr\/index.php?rest_route=\/wp\/v2\/posts\/1523\/revisions"}],"predecessor-version":[{"id":1524,"href":"https:\/\/biyer.com.tr\/index.php?rest_route=\/wp\/v2\/posts\/1523\/revisions\/1524"}],"wp:attachment":[{"href":"https:\/\/biyer.com.tr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1523"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/biyer.com.tr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1523"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/biyer.com.tr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1523"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}