flat assembler
Message board for the users of flat assembler.
Index
> Macroinstructions > Inlineable Procedures |
Author |
|
Eoin 19 Sep 2005, 18:41
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 ).
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 |
|||
19 Sep 2005, 18:41 |
|
vid 20 Sep 2005, 07:54
why use assembly then?
|
|||
20 Sep 2005, 07:54 |
|
f0dder 20 Sep 2005, 08:11
Eoín, it's cute and all... but why not just use a C++ compiler instead?
|
|||
20 Sep 2005, 08:11 |
|
f0dder 20 Sep 2005, 13:54
It's a cute idea anyway .
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. |
|||
20 Sep 2005, 13:54 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.