problem with MSVC

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • raashid bhatt

    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:CONSO LE /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
  • Malcolm McLean

    #2
    Re: problem with MSVC


    "raashid bhatt" <raashidbhatt@g mail.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:CONSO LE /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.




    Comment

    • Antoninus Twink

      #3
      Re: problem with MSVC

      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...)

      Comment

      • Ben Bacarisse

        #4
        Re: problem with MSVC

        raashid bhatt <raashidbhatt@g mail.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:CONSO LE /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.

        Comment

        • santosh

          #5
          Re: problem with MSVC

          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:CONSO LE /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;
          }

          Comment

          • Serve Lau

            #6
            Re: problem with MSVC


            "Antoninus Twink" <nospam@nospam. invalidschreef in bericht
            news:slrngagp19 .v28.nospam@nos pam.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


            Comment

            • Antoninus Twink

              #7
              Re: problem with MSVC

              On 18 Aug 2008 at 16:47, Serve Lau wrote:
              "Antoninus Twink" <nospam@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.

              Comment

              • santosh

                #8
                Re: problem with MSVC

                Antoninus Twink wrote:
                On 18 Aug 2008 at 16:47, Serve Lau wrote:
                >"Antoninus Twink" <nospam@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.

                Comment

                • Keith Thompson

                  #9
                  Re: problem with MSVC

                  santosh <santosh.k83@gm ail.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_Keit h) kst-u@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"

                  Comment

                  Working...