#### Archived

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

# Gritty C++ Questions

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

## Recommended Posts

First off, does anyone know how to output assembler to a source file from MSVC, using my game's C++ source? It seems as though it would be obvious, but I don't see a way of doing it.. Also, if there are such tasks, is there a way to output individual functions? I'm not an assembler, but I can easily see how many clock cycles certain tasks use up, and can optimize. Now a really low level (perhaps stupid) question. Anyone that understands how C++ is converted to ASM could probably answer these. Two codes fragments that do the same thing:
if(x1 == x2)
{
if(x3 == x4)
{
if(x5 == x6)
DoSomething();
else if(x6 < 0)
DoSomething();
}
}

if(x1==x2 && x3==x4 && (x5==x6 || x6 < 0))
DoSomething();  
At first, I would have thought they would be the same. But what about "||" (OR) operators? Does the second version there check each "AND" instead of skipping the rest when the first one was false? Like I said, this is probably a stupid question. And one other one. This: "if(x < 6)" Versus: "if(x <= 5)" Doesn't it seem as though saying less than or equal to will be performing 2 checks? Instead of just "less than the number plus 1"? Another version could be like "if(x > MIN_X-1)" instead of "if(x >= MIN_X)". Ha, I'm sorry if anyone finds this repulsive. -Jiia [edited by - Jiia on August 28, 2002 7:46:12 PM]

##### Share on other sites
The first two you gave are exactly the same. A compiler will stop after the first comparison fails.

Processors have opcodes for both in the second example.

##### Share on other sites
Hi there, no I don''t find this repulsive, these are good questions.

First off I don''t know how to get the assembly output from MSVC, sorry never tried.

Now about the if question, the output from the compiler will almost cetainly be different for each code segment. The first code segment:

if(x1 == x2) {
if(x3 == x4) {
if(x5 == x6)
DoSomething();
if(x6 < 0)
DoSomething();
}
}

would be converted to a series of jumps based on the comparison, whereas the seconds code segment:

if(x1==x2 && x3==x4 && (x5==x6 || x6 < 0))
DoSomething();

will use actual AND and OR statements. Now most optimizing compilers will ''shortcut'' the AND statments by skiping the rest if the first (or subsequent) operands are false, which would be the case here (the OR operator wouldn''t have any affect here). On the other hand the intial code segment wouldn''t recieve this optimzation. Its concievable that a compiler could optimize both code segments to the same output in the future, but as of now none will do that.

As far as the difference between:
if(x > MIN_X-1)
and
if(x >= MIN_X)
they should compile to the same code output (with optimzation enabled), whether or not MSVC actually does that is unknown to me. For the most part the small syntatic differences between say ++x and x++, or using x-- in a loop instead ox x++ (rewriting the loop to count backwards) are no longer important as most compilers will optimze these differences out. Another thing to keep in mind is that with modern PC CPU''s (P4, and Athlon), more time is often spent fetching variables and data than doing operations, so rather than just looking at assembly output, profile the code and run both versions within the game engine. This is the only true way to test the differences as things like cache hit/miss''s and memory transfers will affect performance as well.

##### Share on other sites
quote:
Original post by smanches
The first two you gave are exactly the same.

I think not really: if x1==x2, and then x3==x4 and finally x5==x6==-1 then the first piece of code will execute DoNothing () two times, but the second piece will execute DoNothing () only once.

theNestruo

Syntax error in 2410
Ok

##### Share on other sites
theNestruo - ha ha, you''re right. Oops. I''ll edit that in a second.

Thanks for the help. You''re totally right about the speed difference only being reliable when you see the time it took go by. But when you deal with operators, it would have to be performed for an hour to finally see the tiny bit of difference in speed. That''s why I mentioned it was a little stupid. But these were things I''ve always pondered. Thanks again,
Jiia

##### Share on other sites
Yea, your right about executing it twice. I didn't look close enough. As far as nesting IF statements compared to ANDs in a single comparison, MSVC compiles the same code. As long as there is no other code to execute within each IF.

The only way I know of to get disassembly is to run your program in debug then open the disassembly window pane.

[edited by - smanches on August 28, 2002 7:56:50 PM]

##### Share on other sites
You can get the assembly listing in VC++ 6.0 by going to the Project Options, selecting the produce list file box and selecting the listing file type (in this case, assembly file, with or without the corresponding source) from the dropdown.