Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8249757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:39:15+00:00 2026-06-07T23:39:15+00:00

I just read the whole documentation of Templates in the D programming language but

  • 0

I just read the whole documentation of Templates in the D programming language but cant seem to find a way for my very simple task, for functions I need 3 assembler instructions to be inserted at the beginning of each function, I would like to automate this through a macro so I dont have to manually write it each time.

__gshared void jump()
{
    asm{db START_KEY;}

    //bla bla bla

    asm{mov EBX, ip;add ip,4;jmp dword ptr [EBX];db END_KEY;}
}

something like this should get replaced with

__gshared void jump()
{
    mixin starttemplate();

    //bla bla bla

    mixin endtemplate();
}

In C I would have done something like this

#define STARTASM() asm{.......}

but if I try something like this

template endtemplate()
{
    asm{mov EBX, ip;add ip,4;jmp dword ptr [EBX];db END_KEY;}
}

it will throw an error saying that I have to declare a variable at a template (which I dont want to, since performance is absolutely needed here).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T23:39:17+00:00Added an answer on June 7, 2026 at 11:39 pm

    How about saving the asm command as a string, and using a mixin?

    immutable string ASM_START=q{
        asm{db START_KEY;}
    }
    immutable string ASM_END=q{
        asm{mov EBX, ip;add ip,4;jmp dword ptr [EBX];db END_KEY;}
    }
    
    __gshared void jump()
    {
        mixin(ASM_START);
        //bla bla bla
        mixin(ASM_END);
    }
    

    Another option is to use the mixing to create the entire function, and pass it’s signature and content(=body) as arguments:

    string functionWithAsm(string signature,string content)(){
        return Format!(q{
            %s
            {
                asm{db START_KEY;}
                %s
                asm{mov EBX, ip;add ip,4;jmp dword ptr [EBX];db END_KEY;}
            }
            },signature,content);
    }
    
    mixin(functionWithAsm!("__gshared void jump()",q{
                /*some actual code*/
                })());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've read the official documentation and I understand what class references are but I
I read the documentation on visit_each , but can't really see what exactly it
I just read an article on www.songho.ca which indicates that a projection matrix is
I just read on this answer (answer has since been removed) and I'm sure
I just read in Code Complete that you should not use exceptions for flow
I just read a blog which says ...methods that couldn’t be inlined previously because
I just read this great answer from BalusC about how to upload files with
I just read this requirement on a job listing: Aware of the pitfalls of
I just read this: http://haacked.com/archive/2008/11/20/anatomy-of-a-subtle-json-vulnerability.aspx I was under the impression that overwriting Object or
I just read about the breadth-first search algorithm in the Introduction to Algorithms book

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.