flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > Inlineable Procedures

Author
Thread Post new topic Reply to topic
Eoin



Joined: 16 Jun 2003
Posts: 68
Location: Ireland
Eoin
Well here are the fruits of my labours over the past two days, and the results of the many questions I asked in this thread (and answered myself mostly Very Happy ).

I’ve developed some inlineable procedure macros for my generic programming work but I though I’d present them here as a separate entity for people to use, should they wish. Now I do realise my recent ASM work seems very high level, but in fact you lose no more control than if you call any regular functions written by someone else.

Anyway a serious problem with calling macros is that any registers in the parameters could get overwritten by the macro code before they are read. These macros provide a way around that, where desired, by allowing you to use symbolic registers which are altered during each instance of the inline code depending on the parameters. Not a perfect solution, but not too bad a one either.

Here are the macros;
Code:
; -------- Register list management macros --------

; Remove a register from the free list.
macro RemoveReg [Reg] {
forward
        match a-=Reg-b, FreeList@iproc \{ FreeList@iproc equ a-b \}
}

; Pops any registers from the preserve list off of the stack.
macro RestoreRegs {
        match any, PushList@iproc \{ pop PushList@iproc \}
}

; Equates the provided list of symbols to available registers from the free list.
macro GetReg [symbol] {
forward
        match a-reg-b, FreeList@iproc \{ 
                symbol equ reg                  
                FreeList@iproc equ a-b
                
                match =reg, end \\{ 
                        display "Not enough free registers to generate code!",13,10
                \\}
                
                match c-=reg-d, PresList@iproc \\{ 
                        pushd reg
                        PushList@iproc equ reg PushList@iproc
                \\}
        \}
}

; Checks the parameters passed and removes any registers found from the free list.
macro CheckReg param,[Reg] {
forward
        match =Reg,param \{ RemoveReg Reg \}
        match a=Reg,param \{ RemoveReg Reg \}
        match =Reg b,param \{ RemoveReg Reg \}
        match a=Reg b,param \{ RemoveReg Reg \}
}

; -------- Inline procedure macros --------

macro iprocMacro name,[param] {
common
        macro name param {      
                FreeList@iproc equ <-eax-ecx-edx-ebx-esi-edi-end->
                PushList@iproc equ 
                PresList@iproc equ <-esi-edi-ebx->
if ~ param eq
        forward
                CheckReg param,eax,ecx,edx,ebx,esi,edi
        common
end if

}

macro iendpMacro {
        RestoreRegs
        
        restore FreeList@iproc,PresList@iproc,PushList@iproc
}

iproc fix iprocMacro
iendp fix iendpMacro }     

The macros only select registers from a predefined (and easily altered) list. The order of the registers in that list is the order in which they’ll be assigned, so the most general purpose registers should be first. A second list contains the names of registers whose values must be preserved (as per whatever calling convention you are using) these will automatically be pushed / popped as appropriate. Here is an example of using them along with an output of the code generated;
Code:
; The procedure is defined either in code ar data section
iproc TestTwoArgs,arg1,arg2
        GetReg rg1,rg2

        mov rg1,arg1    
        mov rg2,arg2
iendp

; It is called in a code section
TestTwoArgs ecx,[123456h]
TestTwoArgs [ecx+eax*4],edx    

Code:
00402042 89C8                   mov     eax,ecx
00402044 8B1556341200           mov     edx,[123456h]
0040204A 53                     push    ebx
0040204B 56                     push    esi
0040204C 8B1C81                 mov     ebx,[ecx+eax*4]
0040204F 89D6                   mov     esi,edx
00402051 5E                     pop     esi
00402052 5B                     pop     ebx    
Post 19 Sep 2005, 18:41
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
why use assembly then? Wink
Post 20 Sep 2005, 07:54
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3170
Location: Denmark
f0dder
Eoín, it's cute and all... but why not just use a C++ compiler instead? Smile
Post 20 Sep 2005, 08:11
View user's profile Send private message Visit poster's website Reply with quote
Eoin



Joined: 16 Jun 2003
Posts: 68
Location: Ireland
Eoin
Thing is, I do use C++ these days. But there is just something really nice about assembly. I think the overhead in C++ is what get to me. EXEs start off big, and seem to grow very quickly to much bigger sizes. I realise in the end that the code is probably better than what I write with ASM, but where you can't tell the difference in speed I only judge by size Smile .

This idea was just an idea I'd had ages ago, way back when I still used MASM which would put it about 4 years ago, and I suppose I just had to get it out of my system Very Happy . But I don't expect I'll ever finish the generic library thing cause chances are I wouldn't even use it, never mind anyone else.
Post 20 Sep 2005, 12:31
View user's profile Send private message Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3170
Location: Denmark
f0dder
It's a cute idea anyway Smile.

And yeah, C++ does start out with somewhat of an overhead. Especially iostreams because of all that internatinalization overhead-schmoverhead. I haven't experienced "irrational" growth beyond that, though... but I guess badly-written templates can kill you there.
Post 20 Sep 2005, 13:54
View user's profile Send private message Visit poster's website 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. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.