News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

This is just too cool

Started by donkey, December 13, 2010, 11:36:52 PM

Previous topic - Next topic

donkey

I was playing around with GoAsm today and found this little accidental feature. If you call something using invoke or call or really in any way reference a label either data or code that is forward referenced in the your project you can use conditional compilation with it. This means that you can create source code libraries and only assemble those parts that are referenced in your code, the rest will be ignored. For example:

CODE SECTION
#IFDEF SomeProc

SomeProc FRAME Param
    xor eax,eax
    ret
ENDF

#ENDIF


The proc above will not be assembled because it has not been previously referenced in the code so SomeProc is undefined. But...

CODE SECTION

invoke SomeProc, 0

#IFDEF SomeProc

SomeProc FRAME Param
    xor eax,eax
    ret
ENDF

#ENDIF


Now the proc is assembled ! I tried it with some pretty large routines and just adding a single invoke imported a bunch of code and grew my program by around 2KB, removing the invoke reduced it by the same amount. Looking at the output in a debugger verifies that the code is only assembled if it is referenced. The caveat is ofcourse that the #IFDEF/#ENDIF blocks must be the last thing the assembler sees or at least be after the first reference so if you keep a source library in a separate file just include it at the end of your main file. Also you have to take care to make sure that your source library doesn't violate the forward referencing rule. This is great for making an inline code library like they have in C/C++, I'm using it in my current project so that I have a common.asm file that contains a lot of string and other common functions and they are only brought into the program if I use them, like a static lib but with actual source code so there is less bloat.

At any rate I was thinking that if anybody was interested in donating some code I will consider making an inline common.asm file for inclusion in the headers project. Just post the code here in the following format:

#IFDEF lszCopy
/*
Copies a zero terminated string
Parameters:
Dest = Pointer to destination buffer
Source = Pointer to source string
Returns the address of the destination buffer
*/

lszCopy FRAME pDest,pSource
uses edi,esi

mov edi,[pDest]
mov esi,[pSource]

test esi,7
jz >L1
:
mov al,[esi]
mov [edi],al
or al,al
jz >D1
inc esi
inc edi
test esi,7
jne <

ALIGN 16
L1:
mov eax,[esi]
; Test for 0 byte
mov edx,eax
lea ecx,[edx-01010101h]
not edx
and ecx,edx
and ecx,80808080h
jnz >
; No 0 byte so copy the dword
mov [edi],eax
add esi,4
add edi,4
jmp <L1
:
; Remainder
bsf ecx,ecx
shr ecx,3
inc ecx
rep movsb

lea eax,[edi-1]
sub eax,[pDest]
D1:

RET
ENDF
#ENDIF


Edgar
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

donkey

Thinking further on the idea, we could have switched compilation of the source libraries as well. For example #DEFINE USEMMX, USESSE, USESSE2, USEMOVCC etc that can select the inline code based on the features you require for the target processor or Windows version via WINVER. This could be a pretty good addition to the headers project. I am not much for farming other people's code but anything that you contribute will be credited to you in the file and perhaps a line in the output from GoAsm, this is the actual output from my project:

QuoteGoAsm /c /x86 "TestInline.asm"

GoAsm.Exe Version 0.56.8 - Copyright Jeremy Gordon 2001/9 - JG@JGnet.co.uk
inline lszCase written by Edgar Hansen (Donkey)
Output file: TestInline.obj

If enough people ask for this kind of mention I will include it in the file. Also I decided to name the file inline.asm not common.asm

Edgar
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable