intergrating asm in c++

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Cristian Tota

    intergrating asm in c++

    If I am using inline asm in functions, like this:

    void func(){
    __asm{
    // asm code
    };
    };

    is it guaranteed that this does not mess up the registries? (As opposed to
    using inline asm in middle of C++ code.)

    Cristian


  • Mike Wahler

    #2
    Re: intergrating asm in c++


    "Cristian Tota" <cristian.tota@ vion-software.ro> wrote in message
    news:bpok6l$5jm $2@ally.taide.n et...[color=blue]
    > If I am using inline asm in functions, like this:
    >
    > void func(){
    > __asm{
    > // asm code
    > };
    > };
    >
    > is it guaranteed that this does not mess up the registries? (As opposed to
    > using inline asm in middle of C++ code.)[/color]

    C++ has no notion of 'registries', so your query
    doesn't make much sense.

    -Mike


    Comment

    • lilburne

      #3
      Re: intergrating asm in c++

      Mike Wahler wrote:[color=blue]
      > "Cristian Tota" <cristian.tota@ vion-software.ro> wrote in message
      > news:bpok6l$5jm $2@ally.taide.n et...
      >[color=green]
      >>If I am using inline asm in functions, like this:
      >>
      >>void func(){
      >> __asm{
      >> // asm code
      >> };
      >>};
      >>
      >>is it guaranteed that this does not mess up the registries? (As opposed to
      >>using inline asm in middle of C++ code.)[/color][/color]

      I'd be wary about using asm in any code, are you absolutely
      sure its needed.
      [color=blue]
      >
      > C++ has no notion of 'registries', so your query
      > doesn't make much sense.
      >[/color]

      The OP is talking about registers, not registries (not all
      posters from .ro have English as a first language).

      Comment

      • Cristian Tota

        #4
        Re: intergrating asm in c++

        Sorry, I ment 'registers' (my first language is Romanian).

        <<I'd be wary about using asm in any code, are you absolutely sure its
        needed.>>

        Could there be problems even if the asm code is isolated in functions?

        I have to do a lot of bit operations, like decoding a 32 bit variable into
        values represented by groups of bits, (for example the value of
        bits[12:15]), swapping the bytes, etc. These would be done very nicely using
        the asm instructions.

        Cristian





        Comment

        • Moonlit

          #5
          Re: intergrating asm in c++

          Hi,

          Check your compiler docs. I believe you can change most registers. However
          (for x86 based machines) sp should probably be the same at the end of your
          routine since it is used for building the stack frame. From the top of my
          head I also believe you have to restore si and di but I am not completely
          sure about that.

          Regards, Ron AF Greve

          "Cristian Tota" <cristian.tota@ vion-software.ro> wrote in message
          news:bpoor1$fm5 $1@ally.taide.n et...[color=blue]
          > Sorry, I ment 'registers' (my first language is Romanian).
          >
          > <<I'd be wary about using asm in any code, are you absolutely sure its
          > needed.>>
          >
          > Could there be problems even if the asm code is isolated in functions?
          >
          > I have to do a lot of bit operations, like decoding a 32 bit variable into
          > values represented by groups of bits, (for example the value of
          > bits[12:15]), swapping the bytes, etc. These would be done very nicely[/color]
          using[color=blue]
          > the asm instructions.
          >
          > Cristian
          >
          >
          >
          >
          >[/color]


          Comment

          • Mike Wahler

            #6
            Re: intergrating asm in c++


            "Cristian Tota" <cristian.tota@ vion-software.ro> wrote in message
            news:bpoor1$fm5 $1@ally.taide.n et...[color=blue]
            > Sorry, I ment 'registers' (my first language is Romanian).[/color]

            Then yes you will need to ensure that your usage of registers
            does not interfere with that of your compiler. Your documentation
            should have information about this.

            -Mike


            Comment

            • lilburne

              #7
              Re: intergrating asm in c++

              Cristian Tota wrote:
              [color=blue]
              > Sorry, I ment 'registers' (my first language is Romanian).
              >
              > <<I'd be wary about using asm in any code, are you absolutely sure its
              > needed.>>
              >
              > Could there be problems even if the asm code is isolated in functions?
              >
              > I have to do a lot of bit operations, like decoding a 32 bit variable into
              > values represented by groups of bits, (for example the value of
              > bits[12:15]), swapping the bytes, etc. These would be done very nicely using
              > the asm instructions.
              >[/color]

              They can also be done portably and just as easily with
              expressions that use the operators >>, <<, |, &, and ~. The
              compiler usually creates pretty efficient code for these
              sort of expressions too. I check that first before resorting
              to asm.


              Comment

              • wogston

                #8
                Re: intergrating asm in c++

                > I have to do a lot of bit operations, like decoding a 32 bit variable into[color=blue]
                > values represented by groups of bits, (for example the value of
                > bits[12:15]), swapping the bytes, etc. These would be done very nicely[/color]
                using[color=blue]
                > the asm instructions.[/color]

                Be sure to write longer snips or assembly than 1-10 lines, and write it
                well, otherwise it might have negative effect on the performance. Visual C++
                6 doesn't optimize "over" assembly blocks, so your "optimizati on" might make
                your code run slower, not faster. Be sure you are doing the right thing
                whipping assembly mnemonics out in the open.

                For your example, check what compiler does first for:

                int v = (a >> 11) & 0x0f;

                ... or whatever it is that you wanted to do in your example! Can you do it
                faster in assembly, and more importantly, can you fuse it together with rest
                of the generated code as tirelessly and correctly as machine can? That kind
                of boring details are what the machines are best at, human mind is wasted on
                trivial details like that, concentrate on the big picture first, that's
                where the God's Creation is at it's best. You have a powerful, abstractly
                thinking mind: the computer doesn't, it is your slave, use it!

                Also look into realtime code generators and JIT-like compiler technology.
                Those are the near-future for high-performance low-level bitfucking software
                engineering. Shaders in GPU's and shader compilers by nVidia and Microsoft
                show the way the technologies like JIT from Sun have made popular in the
                past decade or so.

                Human operators writing assembly is backwards development. Just my opinion,
                not the ultimate truth. :)


                Comment

                • Giuseppe Scrivano

                  #9
                  Re: intergrating asm in c++

                  Cristian be sure that at the end you restore the value of the registers that
                  you use in your asm code. You can change the value of registers with your
                  asm code.


                  "wogston" <someone@micros oft.com> ha scritto nel messaggio
                  news:bppof2$4tn $1@phys-news1.kolumbus. fi...[color=blue][color=green]
                  > > I have to do a lot of bit operations, like decoding a 32 bit variable[/color][/color]
                  into[color=blue][color=green]
                  > > values represented by groups of bits, (for example the value of
                  > > bits[12:15]), swapping the bytes, etc. These would be done very nicely[/color]
                  > using[color=green]
                  > > the asm instructions.[/color]
                  >
                  > Be sure to write longer snips or assembly than 1-10 lines, and write it
                  > well, otherwise it might have negative effect on the performance. Visual[/color]
                  C++[color=blue]
                  > 6 doesn't optimize "over" assembly blocks, so your "optimizati on" might[/color]
                  make[color=blue]
                  > your code run slower, not faster. Be sure you are doing the right thing
                  > whipping assembly mnemonics out in the open.
                  >
                  > For your example, check what compiler does first for:
                  >
                  > int v = (a >> 11) & 0x0f;
                  >
                  > .. or whatever it is that you wanted to do in your example! Can you do it
                  > faster in assembly, and more importantly, can you fuse it together with[/color]
                  rest[color=blue]
                  > of the generated code as tirelessly and correctly as machine can? That[/color]
                  kind[color=blue]
                  > of boring details are what the machines are best at, human mind is wasted[/color]
                  on[color=blue]
                  > trivial details like that, concentrate on the big picture first, that's
                  > where the God's Creation is at it's best. You have a powerful, abstractly
                  > thinking mind: the computer doesn't, it is your slave, use it!
                  >
                  > Also look into realtime code generators and JIT-like compiler technology.
                  > Those are the near-future for high-performance low-level bitfucking[/color]
                  software[color=blue]
                  > engineering. Shaders in GPU's and shader compilers by nVidia and Microsoft
                  > show the way the technologies like JIT from Sun have made popular in the
                  > past decade or so.
                  >
                  > Human operators writing assembly is backwards development. Just my[/color]
                  opinion,[color=blue]
                  > not the ultimate truth. :)
                  >
                  >[/color]


                  Comment

                  • wogston

                    #10
                    Re: intergrating asm in c++

                    > Cristian be sure that at the end you restore the value of the registers
                    that[color=blue]
                    > you use in your asm code. You can change the value of registers with your
                    > asm code.[/color]

                    I recall that VC++ (on some versions) need user only to keep state of EBP
                    consistent (and not touch ESP, since the local variables are relative to
                    ESP). I could remember wrong, but that's besides the point, inline asm
                    blocks distrupt the optimizations the compiler can do, and usually the
                    resulting code is worse-performance, especially if the "optimizati ons" user
                    is doing are few-liners.

                    I'd go to assembly only if there are SIMD possibilities beyond the scope of
                    comp.lang.c++, and in those cases I propably use realtime code generation
                    and if I really want to type the mnemonics by my own hand into textfile, I
                    prefer .asm filename extension and NASM for compiling the binary for later
                    linking. SoftWire is a good search keyword in sourceforge/google for getting
                    started.


                    Comment

                    • Bob Hairgrove

                      #11
                      Re: intergrating asm in c++

                      On Sun, 23 Nov 2003 01:53:10 +0200, "Cristian Tota"
                      <cristian.tota@ vion-software.ro> wrote:
                      [color=blue]
                      >Sorry, I ment 'registers' (my first language is Romanian).
                      >
                      ><<I'd be wary about using asm in any code, are you absolutely sure its
                      >needed.>>
                      >
                      >Could there be problems even if the asm code is isolated in functions?
                      >
                      >I have to do a lot of bit operations, like decoding a 32 bit variable into
                      >values represented by groups of bits, (for example the value of
                      >bits[12:15]), swapping the bytes, etc. These would be done very nicely using
                      >the asm instructions.
                      >
                      >Cristian[/color]

                      Wrap your __asm code like this:

                      __asm {
                      pushfd
                      pushad
                      ... your code ...
                      popad
                      popfd
                      }


                      --
                      Bob Hairgrove
                      NoSpamPlease@Ho me.com

                      Comment

                      • Bob Hairgrove

                        #12
                        Re: intergrating asm in c++

                        On Sun, 23 Nov 2003 12:05:26 GMT, wouldnt_you_lik e@to_know.com (Bob
                        Hairgrove) wrote:
                        [color=blue]
                        >Wrap your __asm code like this:
                        >
                        >__asm {
                        > pushfd
                        > pushad
                        > ... your code ...
                        > popad
                        > popfd
                        >}
                        >[/color]

                        I forgot that you also need to CLD if you changed it.


                        --
                        Bob Hairgrove
                        NoSpamPlease@Ho me.com

                        Comment

                        • wogston

                          #13
                          Re: intergrating asm in c++

                          > >Wrap your __asm code like this:[color=blue][color=green]
                          > >
                          > >__asm {
                          > > pushfd
                          > > pushad
                          > > ... your code ...
                          > > popad
                          > > popfd
                          > >}
                          > >[/color][/color]

                          Quote #1, MSDN:

                          When using __asm to write assembly language in C/C++ functions, you don't
                          need to preserve the EAX, EBX, ECX, EDX, ESI, or EDI registers.

                          Quote #2, MSDN:

                          You should preserve other registers you use (such as DS, SS, SP, BP, and
                          flags registers) for the scope of the __asm block. You should preserve the
                          ESP and EBP registers unless you have some reason to change them (to switch
                          stacks, for example).



                          While this is specific to Microsoft Visual C++ implementation of the
                          language, I don't think this is topical to this newsgroup. I could be wrong.


                          Comment

                          • Bob Hairgrove

                            #14
                            Re: intergrating asm in c++

                            On Sun, 23 Nov 2003 16:32:30 +0200, "wogston" <someone@micros oft.com>
                            wrote:
                            [color=blue][color=green][color=darkred]
                            >> >Wrap your __asm code like this:
                            >> >
                            >> >__asm {
                            >> > pushfd
                            >> > pushad
                            >> > ... your code ...
                            >> > popad
                            >> > popfd
                            >> >}
                            >> >[/color][/color]
                            >
                            >Quote #1, MSDN:
                            >
                            >When using __asm to write assembly language in C/C++ functions, you don't
                            >need to preserve the EAX, EBX, ECX, EDX, ESI, or EDI registers.[/color]

                            ...."unless you are using __fastcall calling convention."

                            Also,

                            "...by using EBX, ESI or EDI in inline assembly code, you force the
                            compiler to save and restore those registers in the function prologue
                            and epilogue..."

                            There is no guarantee that other compilers will do this.
                            [color=blue]
                            >Quote #2, MSDN:
                            >[snip]
                            >
                            >While this is specific to Microsoft Visual C++ implementation of the
                            >language, I don't think this is topical to this newsgroup. I could be wrong.[/color]

                            It's definitely OT.


                            --
                            Bob Hairgrove
                            NoSpamPlease@Ho me.com

                            Comment

                            Working...