Archived

This topic is now archived and is closed to further replies.

Some basic assembly help

This topic is 4945 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

OK I made a simple function using VC++ 6.0''s inline assembler and well I don''t understand how the code could possibly go faster than what I''m looking at right now. It appears to be twice as slow as x = i + i (I''m using the function call Add(i, i) 1,000,000 times:
inline int ADD(int x, int y)
{
	__asm
	{
		mov eax, x
		add eax, y
		mov x, eax
	}
	return x;
}
Is there a way to streamline this? I''m literally an hour old to assembly.

Share this post


Link to post
Share on other sites
When you try to modularize an operation this small, you are bound to lose speed no matter what you do. When the compiler operates on your C code, it is able to look at a few operations at a time; the result is that almost every time an "add" comes up, the needed values are *already* in registers. So it just generates the add instruction using the registers that those variables happen to be in. It probably won''t bother moving the result to a different register either.

Share this post


Link to post
Share on other sites
Remember in most cases the compiler is probably a better optimiser than you and also that programming stuff in ASM does not automatically give a speed increase. ASM can be a useful skill but only employ it for optimisation when you really need to and if your profiling tells you it doesn''t make it faster, forget about it.

Share this post


Link to post
Share on other sites
Why would you remake fundamental operators that the processor already has for you? Try making a 3-vector dot product function and compare it with the C++ version, and then see what happens.

edit: ie, a 3-vector dot product with SSE.

[edited by - temp_ie_cant_thinkof_name on May 31, 2004 6:32:55 PM]

Share this post


Link to post
Share on other sites
I would like to point out that I was NOT trying to make a function that was faster than + but was merely determining the RELATIVE SPEED which I thought was so disproportionate that I thought I would ask the question here. And apparently I don''t add the addresses of x and y because my function returns the correct results.

Share this post


Link to post
Share on other sites
Furthermore, using the inline assembler prevents the compiler from optimizing the code around the assembly language block.

Furthermore, if the optimizer notices that the results of the non-assembly code is unused, it might skip that code. However, it is forced to include your function.

The only way to tell what is happening is to look at the optimized assembly language that the compiler generates and compare it to yours.



John Bolton
Page 44 Studios
Current project: NHL Faceoff 2005 PS2

Share this post


Link to post
Share on other sites
To be clear, JohnBolton (assuming you are familiar with VC++) that can be accomplished by putting a breakpoint around the + operator and compiling/running then going to view->debug windows->Disassembler or something?

Share this post


Link to post
Share on other sites
First of all, like everyone else has said already, there is no point in making an assembly function to do something so simple. But more importantly (may be useful to you in the future), the return values of functions in C/C++ are stored in the eax register (or ax in 16 bit programs). So instead of moving eax into x then returning x, you would just cut the function off after the add. The result would be returned in eax.

Share this post


Link to post
Share on other sites