Page 1 of 1

Optimized inline functions within assembly

Posted: Fri Jan 16, 2009 6:46 am
by CPLH
Hello,
I've been programming in assembly (mainly nasm) for quite some time.. after some point I have realized that I have been overdoing some of the optimizations to a point where the code became more difficult to understand without comments.
Finally I have overlooked what I have done, and found that a lot of my optimizations could have been automated. However I don't know how to do it.
For example, optimized inline functions. If the following is the only code of a program:

Code: Select all

start:
	mov eax, 2
	call SuperFunction
	int 0x20

SuperFunction:
	push ebx
	mov ebx, 3
	add ebx, eax
	mov [0x700], ebx
	pop ebx
	ret
....I would like it to have SuperFunction as an inline function since the function isn't used anywhere else:

Code: Select all

start:
	mov eax, 2
	push ebx
	mov ebx, 3
	add ebx, eax
	mov [0x700], ebx
	pop ebx
	int 0x20
....also I would love for it to be optimized:

Code: Select all

start:
	mov eax, 2		; This is still needed because it may be used by the software interrupt.
				; (It would be nice to be able to tell the assembler whether it's needed it not.)
	mov dword [0x700], 5
	int 0x20
This would save a LOT of time of me trying to optimize everything... Unfortunately so far I haven't found anything that can do such a thing...

Code: Select all

nasm file.asm -O3
doesn't work that well.. it mainly optimizes "jmp"s...

Code: Select all

fasm file.asm
is well known for its multi-pass algorithm, but it also seems to only optimize "jmp"s.

Usually hear something like... "Oh, that's simple - just use a macro." ...well, I don't know about you, but I have no idea how to make a macro make the optimization I made, without making a whole bunch of extremely specialized macros.
Some of you may say that I should change the "SuperFunction" function to a macro. That would make it inlined, however that would force me to rewrite it into a function if I need to call it more than once. Additionally, by that time I could get confused, why is it a macro in the first place?
So, does anybody know of a way to get this thing to work? Some good assembler capable of optimizing well? I think it would save me, and a lot of other people some effort.

Plus, there is an additional problem:
Imagine you have a super duper assembler that is capable of doing the optimizations that I noted above.
Now you don't want to "%include" everything into your main file because that would make all public AND private functions accessible - This may cause you to have longer function names because you don't want to have conflicts between two same named functions... So instead, you link everything together.

Unfortunately, the linker usually doesn't seem optimize assembly code. I don't know if it can do inline functions either. Using the above example, if "SuperFunction" is assembled in a separate file, I am not sure how it would be possible for this stuff to be optimized much at all.
So the solution is to either get a better linker, or have the super duper assember have abilities to have private and public functions.
Does anybody know of any assemblers that can do such an optimization? Once again, I think it would help a LOT of people who write in assembly...

Thank you,
Veniamin


PS - I realize that for some of the optimizations, we go by some assumptions of what we will not be doing in assembly... well, I think that by now it is safe to say that we will not be programming self modifying code. That should cover all of the worries.. If we are programming self modifying code, then possibly we could flag to the assembler not to optimize some piece of code... however I believe that such instances would be rare.

Re: Optimized inline functions within assembly

Posted: Fri Jan 16, 2009 7:36 am
by yemista
ok, i think that if im understanding you correctly, all you need is a macro. a macro is just a text substitute, and you can use it as much as you want, you dont need to write any specialized functions or anything. When you declare a function as inline in c, the compiler generates the code for that function rather than a call to that function, so

inline int f() { //do some stuff }
f();
f();
f();

is translated to

//do some stuff
//do some stuff
//do some stuff

So when working in assembly, a macro pretty much does the same thing
If you write,

start:
mov eax, 2
SuperFunction
int 0x20

%macro SuperFunction 0
push ebx
mov ebx, 3
add ebx, eax
mov [0x700], ebx
pop ebx
%endmacro

it is translated to

start:
mov eax, 2
push ebx
mov ebx, 3
add ebx, eax
mov [0x700], ebx
pop ebx
int 0x20

Also, in the future, any time you type SuperFunction, the assembler will just expand the text to be what you defined the macro as.

Re: Optimized inline functions within assembly

Posted: Fri Jan 16, 2009 7:37 am
by CodeCat
I don't think you'll have much luck with optimising assembly other than optimising it yourself. A compiler can optimise C code because it can determine to an extent what the code does. The code has 'semantics', and optimisations are designed to retain the semantics of the code while making it more efficient. Assembly code has no semantics that can be determined by the assembler, because all the assembler sees is lines of instructions, it has no knowledge of functions, control flow structures or variables. So it can't determine the connection between those instructions. And without being able to tell what the code actually does, it can't optimise.