flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > template

Author
Thread Post new topic Reply to topic
l4m2



Joined: 15 Jan 2015
Posts: 648
l4m2
In C++ we write
Code:
template<class T>
void swap(T& a, T& b) {
  T t = a;
  a = b;
  b = t;
}    
So can I do this in fasm? (I guess it should be relevant to macro so I put it here)
Update : Not only the function swap uses template but it is a simple - enough one
Post 05 Jan 2016, 03:56
View user's profile Send private message Reply with quote
cod3b453



Joined: 25 Aug 2004
Posts: 619
cod3b453
Yes but T may be a problem. The obvious one is:
Code:
macro swap lhs,rhs
{
    xchg lhs,rhs
}    
but if T is more than a basic register/pointer you have to introduce information about T - this can also be done at preprocessor level but is more involved as you'd be creating preprocessor time objects. I'll leave type hierarchies and annotations for now but as an example of what I mean here's two partial specialisation equivalents:
Code:
macro swap lhs,rhs,T
{
if lhs in <...> and rhs in <...> then ; regs/mem
    xchg lhs,rhs
; could add more cases/checks for other specialisations here
else ; assume structs
    mov ecx,sizeof.T
    sub esp,ecx

    mov esi,lhs
    mov edi,esp
    rep movsb

    mov esi,rhs
    mov edi,lhs
    rep movsb

    mov esi,esp
    mov edi,rhs
    movsb

    add esp,ecx
end if
}    
Hope that helps Question
Post 06 Jan 2016, 19:12
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar.

Powered by rwasa.