Inline assembly with c++

What cant you do with __asm blocks. I have trouble compiling my code and was wondering if i had to set Visual Studio 2005 up a special way to make everything work. anyone know what i'm talking about?

Comments

  • ISO C does not support inline assembler at all.

    ISO C++ only supports the following syntax:

    asm ("NOP");


    Anything else is not standard syntax and may not work when porting the code to a different compiler or system. Examples of inline assembler that isn't standard C++:

    __asm

    asm { "NOP" };

    asm { NOP };

    asm NOP;
  • [color=Blue]I use __asm all the time in VS 2005 - never had any issues. What exactly is the problem? Does it compile? Ot you have problem running the code?[/color]
  • : [color=Blue]I use __asm all the time in VS 2005 - never had any
    : issues. What exactly is the problem? Does it compile? Ot you have
    : problem running the code?[/color]

    i cant get everything to compile. I'm new to asm and i dont know exactly what i can do __asm. I'm more comfortable using it in c++ in VS 2005 then something like NASM. i'm trying to do some low level file io and seem to get opcode errors with things like labels/syboles.
  • : : [color=Blue]I use __asm all the time in VS 2005 - never had any
    : : issues. What exactly is the problem? Does it compile? Ot you have
    : : problem running the code?[/color]
    :
    : i cant get everything to compile. I'm new to asm and i dont know
    : exactly what i can do __asm. I'm more comfortable using it in c++ in
    : VS 2005 then something like NASM. i'm trying to do some low level
    : file io and seem to get opcode errors with things like
    : labels/syboles.
    :

    Post your error messages and which compiler, linker and assembler you're using.
  • : : : [color=Blue]I use __asm all the time in VS 2005 - never had any
    : : : issues. What exactly is the problem? Does it compile? Ot you have
    : : : problem running the code?[/color]
    : :
    : : i cant get everything to compile. I'm new to asm and i dont know
    : : exactly what i can do __asm. I'm more comfortable using it in c++ in
    : : VS 2005 then something like NASM. i'm trying to do some low level
    : : file io and seem to get opcode errors with things like
    : : labels/syboles.
    : :
    :
    : Post your error messages and which compiler, linker and assembler
    : you're using.


    [italic]... I'm more comfortable using it in c++ ...[/italic]
    [color=Blue]So, you are NOT using it in C++ - not sure if I can help, because I use it only in C++.[/color]
  • : : : : [color=Blue]I use __asm all the time in VS 2005 - never had any
    : : : : issues. What exactly is the problem? Does it compile? Ot you have
    : : : : problem running the code?[/color]
    : : :
    : : : i cant get everything to compile. I'm new to asm and i dont know
    : : : exactly what i can do __asm. I'm more comfortable using it in c++ in
    : : : VS 2005 then something like NASM. i'm trying to do some low level
    : : : file io and seem to get opcode errors with things like
    : : : labels/syboles.
    : : :
    : :
    : : Post your error messages and which compiler, linker and assembler
    : : you're using.
    :
    :
    : [italic]... I'm more comfortable using it in c++ ...[/italic]
    : [color=Blue]So, you are NOT using it in C++ - not sure if I can
    : help, because I use it only in C++.[/color]
    :


    No i am using it in c++. Thank you for the reply by the way. So i was trying some low level disk IO. and the assembly routine works in masm right. but all i get when i compile it in c++ in VS 2005 is an memory violation prompt. What am i doing wrong? I'm very cofident with c++, but i'm a todler to asm and i'm very drawn to it. Also; is there a right and wrong time to use asm in c++. Like i have been writing (or trying to) code for math operations when i need to. Is this ok? dont know why im doing it it just seems funner to switch between asm and c++.


    Happy Holidays


    Thanks again;

    phixjnj
  • I can only reply to the later part of the question:

    : Also; is
    : there a right and wrong time to use asm in c++. Like i have been
    : writing (or trying to) code for math operations when i need to. Is
    : this ok? dont know why im doing it it just seems funner to switch
    : between asm and c++.


    In a PC program, there likely is no situation where asm is to prefer.

    If you are using it for efficiency reasons, you should be aware of how the C++ compiler translates the same algorithm: disassemble it and see if it is inefficient, and if so, in what way. If you find that the C++ compiler does a particular task inefficiently, you should dig up the compiler options and see if all optimization is set correctly. In most of the cases, it is however the programmer that creates inefficient C++.

    Assembler has some very serious disadvantages, the major ones are that it is more error prone than high-level languages, and that it makes the whole code non-portable. If using inline assembler, it is concidered good programming to hide away all such code in files of their own, and thereby "encapsule" the assembler.

    If you are concidering writing assembler for efficiency reasons, you should ask yourselves these questions:

    - Am I writing a very demanding real-time application, such as commerical compiler, a desktop OS to compete with Windows/Linux, a commercial 3D-graphics game or a graphics engine? If not, I likely have no reason to use PC assembler for efficiency reasons.

    - If I must write very demanding real-time applications that are none of the ones mentioned above, why am I writing them on a PC and not on a computer which has real-time performance?

    - Is it likely that I am a better programmer than the programmers on MS/Borland who wrote the optimizer routines for the C++ compiler?
  • [color=Blue]When using inline Assembler for Windows you need to remember that any interaction with hardware is prohibited by Windows if you writing a User application and not a driver. Obviously, things like low-level Disk I/O or writing directly into memory of graphics card (or using interrupts) will not be possible.

    However, some pieces no doubt can be done in assembly. Like the complex FPU sequence can be done better by hand then by optimizer (if it is complex enough - optimizer may not 'see' through it). It is also possible to make local calls inside C/C++ function to avoid the save/restore of registers in standard Windows calling convention - it may save some instruction, especially running in a loop:

    [code]
    void MyClass:Foo ()
    {
    _asm call proc1
    _asm call proc2
    _asm call proc3

    return;

    proc1:
    _asm ...
    _asm ...
    _asm ...
    _asm ret

    proc2:
    _asm ...
    _asm ...
    _asm ...
    _asm ret

    proc3:
    _asm ...
    _asm ...
    _asm ...
    _asm ret
    }
    [/code]
    Usually, compiler optimizes very well - I use VC++ 2005 and it generates very good code, but if you need a long sequence - you may want to do it by hand.
    [/color]
  • : It is also possible to make local calls inside C/C++ function to avoid
    : the save/restore of registers in standard Windows calling convention
    : - it may save some instruction, especially running in a loop:

    You don't need assembler for that, inlining solves it. And since the language is C++, that is not a problem: inlining is supported by the ISO C++ standard.

    Edit: inlining as in inline functions, not inline assembler! :-)
  • : : It is also possible to make local calls inside C/C++ function to avoid
    : : the save/restore of registers in standard Windows calling convention
    : : - it may save some instruction, especially running in a loop:
    :
    : You don't need assembler for that, inlining solves it. And since the
    : language is C++, that is not a problem: inlining is supported by the
    : ISO C++ standard.
    :
    : Edit: inlining as in inline functions, not inline assembler! :-)
    [color=Blue]Inlining also increases the code size, which may not fit into CPU instruction cache, so reloading the cache may impact performance - only measuring the piece of code in question will tell.[/color]
  • : [color=Blue]Inlining also increases the code size, which may not fit
    : into CPU instruction cache, so reloading the cache may impact
    : performance - only measuring the piece of code in question will
    : tell.[/color]

    But what you have done in your example [italic]is[/italic] inlining... with the exception of the jump instructions that will add a few extra overhead clock cycles. Why can't you do like this instead?

    [code]inline static void proc1();
    inline static void proc2();
    inline static void proc3();

    void MyClass:Foo ()
    {
    proc1();
    proc2();
    proc3();
    }[/code]


    Also, the code size will only be increased if the function is called from several places in the program. If the function is only called from one place, the code size is [italic]decreased[/italic], because the calling convention-specific stacking of function parameters is removed.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories