473,396 Members | 1,970 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

problem with MSVC

i am compiling my c program with MSVC its just a simple program

int main()
{
int a;
++a; // INC assembly instruction
return 0; // RET instruction
}

then i compile it

cl /O1 /c sample.c
link /SUBSYSTEM:CONSOLE /ENTRY:main /NODEFAULTLIB sample.obj

i compiled the program with the removal of libc but when i disassemble
the program i dont get the INC instruction

i only get this

XOR EAX,EAX
RET

Rather than

INC ADDRESS_OF_VAR
RET
Aug 17 '08 #1
8 1357

"raashid bhatt" <ra**********@gmail.comwrote in message
>i am compiling my c program with MSVC its just a simple program

int main()
{
int a;
++a; // INC assembly instruction
return 0; // RET instruction
}

then i compile it

cl /O1 /c sample.c
link /SUBSYSTEM:CONSOLE /ENTRY:main /NODEFAULTLIB sample.obj

i compiled the program with the removal of libc but when i disassemble
the program i dont get the INC instruction

i only get this

XOR EAX,EAX
RET

Rather than

INC ADDRESS_OF_VAR
RET
The compiler is optimising away the inc instruction.
You need to take a on the command line (use atoi), increment it, and print
the result.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Aug 17 '08 #2
On 17 Aug 2008 at 17:23, Malcolm McLean wrote:
> int a;
++a; // INC assembly instruction

i compiled the program with the removal of libc but when i disassemble
the program i dont get the INC instruction
The compiler is optimising away the inc instruction.
You need to take a on the command line (use atoi), increment it, and print
the result.
Or make a volatile.

(If you're compiling for an Intel chip, don't expect to see an INC
instruction generated - it will be an ADD instead for amusing historical
reasons...)

Aug 17 '08 #3
raashid bhatt <ra**********@gmail.comwrites:
i am compiling my c program with MSVC its just a simple program

int main()
{
int a;
++a; // INC assembly instruction
return 0; // RET instruction
}

then i compile it

cl /O1 /c sample.c
link /SUBSYSTEM:CONSOLE /ENTRY:main /NODEFAULTLIB sample.obj

i compiled the program with the removal of libc but when i disassemble
the program i dont get the INC instruction

i only get this

XOR EAX,EAX
RET
That is correct. Your program has no effect other than to return zero
to the host environment.

There is another issue. By incrementing an indeterminate value pretty
much anything can happen (though there is some debate about exactly
how bad this is).
Rather than

INC ADDRESS_OF_VAR
RET
The compiler is allowed to remove the increment if it can be sure
there is no need to for it, as in your example.

--
Ben.
Aug 17 '08 #4
raashid bhatt wrote:
i am compiling my c program with MSVC its just a simple program

int main()
{
int a;
++a; // INC assembly instruction
return 0; // RET instruction
}

then i compile it

cl /O1 /c sample.c
link /SUBSYSTEM:CONSOLE /ENTRY:main /NODEFAULTLIB sample.obj

i compiled the program with the removal of libc but when i disassemble
the program i dont get the INC instruction

i only get this

XOR EAX,EAX
RET

Rather than

INC ADDRESS_OF_VAR
RET
The compiler detects that the increment of 'i' has no effect whatsoever
and optimises it away. C compilers have become so good these days that
expecting them to produce the same sort of output that an assembler
beginner might produce is unrealistic. If you want to learn assembler
then do so with a proper assembler and necessary pedagogic material.
Examining the output of modern C compilers will quickly get *very*
confusing, even with optimisations disabled. Compiler assembler output
is not meant for beginners.

Anyway, to get the compiler to actually increment 'i' qualify it as
volatile. This will suppress all optimisations on it.

volatile int i;

int main(void) {
i++;
return 0;
}

Aug 17 '08 #5

"Antoninus Twink" <no****@nospam.invalidschreef in bericht
news:sl*******************@nospam.invalid...
Or make a volatile.

(If you're compiling for an Intel chip, don't expect to see an INC
instruction generated - it will be an ADD instead for amusing historical
reasons...)
amuse me
Aug 18 '08 #6
On 18 Aug 2008 at 16:47, Serve Lau wrote:
"Antoninus Twink" <no****@nospam.invalidschreef:
>(If you're compiling for an Intel chip, don't expect to see an INC
instruction generated - it will be an ADD instead for amusing historical
reasons...)

amuse me
OK, so it's not all that amusing, but here's an extract from
<http://www.intel.com/design/processor/manuals/248966.pdf>:

Assembly/Compiler Coding Rule 32. INC and DEC instructions should be
replaced with ADD or SUB instructions, because ADD and SUB overwrite all
flags, whereas INC and DEC do not, therefore creating false dependencies
on earlier instructions that set the flags.

Aug 18 '08 #7
Antoninus Twink wrote:
On 18 Aug 2008 at 16:47, Serve Lau wrote:
>"Antoninus Twink" <no****@nospam.invalidschreef:
>>(If you're compiling for an Intel chip, don't expect to see an INC
instruction generated - it will be an ADD instead for amusing
historical reasons...)

amuse me

OK, so it's not all that amusing, but here's an extract from
<http://www.intel.com/design/processor/manuals/248966.pdf>:

Assembly/Compiler Coding Rule 32. INC and DEC instructions should be
replaced with ADD or SUB instructions, because ADD and SUB overwrite
all flags, whereas INC and DEC do not, therefore creating false
dependencies on earlier instructions that set the flags.
If you had used the word might for the word will in your post up-thread,
then this discussion would not have been necessary.

Aug 19 '08 #8
santosh <sa*********@gmail.comwrites:
Antoninus Twink wrote:
[snip]
>
If you had used the word might for the word will in your post up-thread,
then this discussion would not have been necessary.
It wasn't necessary anyway.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 19 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bren | last post by:
Hi, Pardon my stupid, but I'm having a problem building Boost.Python as per the instructions at http://www.boost.org/libs/python/doc/building.html I got boost-build-2.0-m6.zip from...
0
by: Leor Zolman | last post by:
The Intel C++ version of STLFilt (my STL Error Message Decryptor utility) has now been updated to support Intel C++ 8 (currently in Beta) along with version 7. All three MSVC libraries are...
7
by: Christian Engström | last post by:
When i compile the program listed below with gcc version 3.3.1 (MinGW on Windows XP) I get the following result: Calling 'func(d)': 'base' copy constructor Calling 'func(*d_handle)': 'base'...
1
by: icedac | last post by:
I have some questions about template and c++ itself. ///////////////////////////////////////////////////////////////////////// q1) see follow c++ code, and compile. it's works only IntelC++8.1...
3
by: martinbriefcase | last post by:
Compiling a program using ACE + MSVC++8, got lots of errors and some are listed as below: Error 2 error C2894: templates cannot be declared to have 'C' linkage c:\program files\microsoft visual...
23
by: Babak | last post by:
Hi Everyone, I've written a standard C code for a simple finite element analysis in MSVC++ . When I save the file as a cpp file, it compiles and runs perfectly, but when I save it as a c file,...
2
by: BruceWho | last post by:
I downloaded boost1.35.0 and built it with following command: bjam --toolset=msvc-7.1 --variant=release --threading=multi -- link=shared --with-system stage and it failed to compile, error...
3
by: imaloner | last post by:
I am posting two threads because I have two different problems, but both have the same background information. Common Background Information: I am trying to rebuild code for a working,...
4
by: imaloner | last post by:
I am posting two threads because I have two different problems, but both have the same background information. Common Background Information: I am trying to rebuild code for a working,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.