Jump to content
  • Advertisement
Sign in to follow this  
X-ed_out

Assembly & C++

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I have some knowledge of assembly and I want to write some small procedures in c++ but I want to write them in assembly, my problem is that I don't know how to integrate the two, accessing variable declared on the c++ part inside the assembly code, etc... I'm using Microsoft Visual studio 2005. I know I have to use something like this:
asm{

}
But I'm not sure...

Share this post


Link to post
Share on other sites
Advertisement
You will have to be an expert in assembly to write more optimized code than the VS2005. Are you sure that your knowledge of ASM is up to these standards? Inline ASM is considered to be a last resort in low-level C++. I think you'd be better off learning some basic C++ instead of using ASM, because you'll be productive much sooner then with pure machine code with practically no speed difference.
Anyway, I'd take a look at this: http://www.touro.edu/acm/Newsletters/Fall99/advpro.htm

Jeroen

Share this post


Link to post
Share on other sites
I want to write in asm not because I want to make something super optimized, but because I want to know how to do it... I believe that the more you know the better! :-P

and thanks for the link

Share this post


Link to post
Share on other sites
Quote:

Each mnemonic, therefore, represents the smallest possible task the computer can carry out in a single clock cycle. It seems that people are much better at putting these miniscule instructions together efficiently than compilers are

That's just outdated and wrong.

Share this post


Link to post
Share on other sites
The __asm keyword is not even supported on Vista 64 so you should read this.
It covers using inline assembly with C++ under Microsoft's latest OS.
It'll probably be all Greek to you unless you have prior asm experience.

Share this post


Link to post
Share on other sites
In VS2005 you can use the _asm keyword to put assembly code right in the middle of C++ code, like this:

int myfunction(int a)
{
int b = a + 7;
_asm {
mov ax,ax
nop
xor ebx,0
}
return b * 2;
}


Be aware that inline assembly is not part of the C or C++ language, but rather is a Microsoft-specific extension. Some other compilers, such as GCC, also have inline assembly, but do it differently because it isn't (and shouldn't be) standard, so inline assembly is not portable across compilers (and obviously won't be portable across different machine achitectures).

Share this post


Link to post
Share on other sites
If it's just for learning, inline ASM should be even less of a priority, because it is incredibly platform- AND toolchain-dependent. For example Visual Studio inline assembly won't compile with gcc, not even on gcc's windows port. And developing for 64-bit windows with Visual Studio you can't use inline assembly at all.

That said, the VS 2005 documentation contains a decent amount of information about using inline assembly. Some highlights:

* Don't use asm{} blocks; they will compile without errors or warnings, but they will not generate any code. Use __asm{} blocks instead.
* You can use identifiers inside __asm blocks just like in normal C++. So, for example, if you declared an int called LoopCounter, __asm move ecx, LoopCounter will work just fine.
* You can call any C function from inside an asm block, but you can only call certain C++ functions. Specifically, you can only call non-overloaded global functions; calling overloaded functions and member functions will cause compiler errors.

Share this post


Link to post
Share on other sites
you don't necessarily have to use inline asm, you can write a separate asm file, compile it into an object file, and link it w/ the other object files generated from your c++ files.

this can get tricky to learn at first, but it makes complete sense later.

using nasm syntax, you could write an asm file like (i'm rusty in asm so please forgive any syntax errors):

.data
mystring: db "Hello, World!",0
.text
; i belive this is the correct syntax for declaring externals?
import _printf
global myfunc

; this is 'naked' function, no prolog/epilog code
myfunc:
push mystring
call _printf
ret

using nasm to compile this to a microsoft compatible object file, than in your c++ code:

extern "C" {
void myfunc();
}

int main(int argc, char *argv[])
{
myfunc();
}


(myfunc might require some name mangling on the c++ side).

link all your object files together (and don't forget stdio.lib for the asm file), and all should work ok.

Share this post


Link to post
Share on other sites
Assembly is one of those topics where theory will be a lot more useful to you than practice (unless you are looking for a career in some *very* specialized programming). My strong recommendation is to take an appropriate university course in computer science.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!