Hello ... I'm Jenison.

I would like to extend my knowledge into the assembly world because I want to get performance out of my apps.

However, I don't know where to start.

I've been reading 'Art of Assembly' and have tried to write some code however ... I have some questions.

1. It appears that MSVC is limited in the assembly it can do? I could not find ANY examples on the web using MSVC and assembly.
2. Do I have to use this MASM? and does MASM only accept assembly code?
3. There are times where I would like to write portions of code in normal C++ however, there are times where I need a function to be as fast as it can be ... and I want to write that in assembly. Are there compilers out there that can handle this? (there must be I'm just an idiot and dont know about them)

I started in MSVC with a simple function

int i =0;

it spitt out this (gotten from the disassembler window)
mov dword ptr ,0;
mov eax,dword ptr
add eax,1;
mov dword ptr ,eax

WHY cant it be this? or can it ...
mov dword ptr ,0;
add ,1;

Ok...enough questions ... for now ;)
Posted on 2002-11-23 20:54:19 by JenisonWRX
1. Use inline assembly.
int ImAnAlien = 0;

mov ImAnAlien, 1
2. Yes, MASM only accepts assembly code
3. MS-VC can handle inline assembly
WHY cant it be this? or can it ...
mov dword ptr ,0;
add ,1;
I can see the ; at the end of each line, this means you have been too long in the world of C/C++ don't worry we've been there. ;)

To answer the question: Why not mov dword ptr , 1

You have to understand compilers aren't perfect. ;)
Posted on 2002-11-23 21:27:58 by stryker
The variables you declare between brackets are local
to the function (as much as possible), this explains "" (as you have a stack frame.)

And for the why add eax,1 ? This stuff is mainly due to optimisations... You may want to read some article about that subject...
Posted on 2002-11-23 21:45:47 by Axial
Actually if you had optimizations turned on, MSVC would recognize that the function doesn't do anything and compile accordingly.


xor eax, eax

It ignores your code because it doesn't really do anything. But - if you give main() some functionality like:

[size=12]void main( void )

int i = 0;
printf( "%i\n", i );

...and turn optimizations on, you'll see that it doesn't do too badly:


xor eax, eax
@@: push eax
push offset string "%i\n"
call printf
add esp, 8
inc eax
jmp @b[/size]

...notice there's no ret. C knows that the loop is endless. ;)
Posted on 2002-11-23 21:49:29 by iblis
You guys are pretty close ...

but why the wasted step of putting it into EAX?

why did the comipler make it 3 lines of assembly instead of just one ...

add , 1
Posted on 2002-11-23 21:57:30 by JenisonWRX
I don't work for microsoft... my reason why 3 lines of code?

1. EAX may be used in future references. Who knows the value of EAX might be used further down the code. Loading from mem->reg and reg->mem would be costly. So why not used a register as a copy of the value in ... you can use this reg on further usage/calculation/manipulation of the variable i.
2. Rules of the compiler.
3. Bad compiler :)
4. Ask MS VC Compiler Team why they did it... it could have been the rules of the compiler or something....
Posted on 2002-11-23 22:15:50 by stryker
which is why we write in assembly I guess ... ;)

ok ... now for another question ...

profiling? what is a good way to determine the amt of performace I gained? Or a simple way of cumputing how long a function takes. Is there a way to count CPU cycles? or do I have to just run it a bunch of times so I can get it in miliseconds.

sorry for being a n00b ...
Posted on 2002-11-23 22:38:09 by JenisonWRX
Try searching for profiling, RDTSC(x86 instruction), QueryPerformanceCounter(Win32 API), timeGetTime(Win32 API), GetTickCount(Win32 API)....

1. Intel VTune (Commercial)
2. AMD CodeAnalyst (FREE)

The reason why MS-VC did that is how it processed the code. This is why we're only assuming answers here since we don't know how MS-VC compiler works internally... But the reason posted thus far, just pointed some of the reasons behind it. Not really all of it.... I can think a couple more reasons but it doesn't matter, we are here to learn assembly aren't we. ;) We can always beat the compiler. ;)
Posted on 2002-11-23 22:53:28 by stryker

...why did the comipler make it 3 lines of assembly instead of just one ...

Because you probably have optimizations disabled. When optimizations are turned off, MSVC expands the asm so that it's easier to follow. Also this way it can more easily display the C source along with the asm listing.

You need to enable optimizations and look again.
Posted on 2002-11-23 23:50:11 by iblis
The compiler starts by generating (intermediate) code that contains only the most basic instructions.

This minimizes the number of different instructions created, making a lean and fast parser. It also allows maximum flexibility for the optimizer to rearrange and combine code.

With optimizations completely turned off, you get a direct translation of this simplistic code.
Posted on 2002-11-24 02:01:28 by tenkey
As iblis showed, you probably compiled in debug mode. In debug mode, all optimizations are off to make the debugger's job easier. If you would have compiled in release mode the only thing that would be generated is 'ret' (not even the xor eax, eax because the function returns nothing). Since you never use the i variable anywhere (well, i.e. removing it has no effect on the program), it is optimized away and not used at all.
If you want the compiler to use your i variable, you'll have to do something with it like returning it:

int main()
int i =0;
return i;

This results in:

mov eax, 1

Posted on 2002-11-24 04:34:26 by Thomas