Thread: Asm + C++

  1. #1
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972

    Asm + C++

    I was considering learning asm (or at least a little) and looking through posts regarding assembly, i found that people would rather just use c++ entirely than to bother using asm/mixing c++ and asm.

    At the same time, however, I've read posts that (at least) games are made with a combination of asm/c++. I still want to learn a little about asm, since it seems it could help to understand how the computer works a bit better, but I couldn't find any threads regarding specifically using asm with c++.

    I see that "asm {... } " in your source is supposed to work, but I get a compiler error like "parse error before '{' " if i put it in (using dev cpp)

    ...If someone could at least direct me to a tutorial that deals with asm and c++ i would appreciate it.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    1) There is no standard way to mix ASM and C++. C++ is platform independant; ASM is platform and compiler dependant.

    2) Most games nowadays will not use any ASM at all. The fact that most graphics work has been moved off the CPU onto dedicated graphics hardware, and the existence of DirectX and similar hardware abstration layers have pretty much removed the need.

    I use assembly languages in general quite a lot, but it's probably been at least 5 years since I've done anything with the x86 in an assembly language, not counting embedded 8088 microcontrollers.

    If you (like me) use microcontrollers in embedded systems, which tend to be very hardware specific, it's nice to use assembly. If you're writing for a modern operating system (even WinCE) it's nicer to use C++, and forget about assembly. There's almost no benefit.

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Some compilers use '__asm' as opposed to 'asm' (I'm not sure why).

    Cat's right. Just by its very nature, C++ with embedded assembly becomes platform dependent. Also, there is rarely (for general applications) need to use assembly. You can get some performance gain by hand optimizing things in assembly, but compilers do a good job optimizing by themselves, so there is rarely a significant gain.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    15
    ASM is fun, I like the way statements are formed, the way you have to twist your logic around... *cough* anyways, it's worth while, if only for the enjoyment of knowing another language.
    /*When all else fails, Immortality can be achieved through Massive Failure*/

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    85
    hey,

    why dont you give the following two sites a try! as well, search google; im sure you can find some great stuff out there!

    http://anastasija.virtualave.net/doc...r/asm01001.htm

    http://webster.cs.ucr.edu/

    cheers!
    Ben
    Medical Robotics: "Pursuing perfection in healthcare through innovations in robotics and information technologies for medicine and surgery."

  6. #6
    julie lexx... btq's Avatar
    Join Date
    Jun 2002
    Posts
    161
    I haven't really seen a tutorial on asm and c++ but I think
    you'll be best of learning pure asm first, workin with registres etc.
    You might wanna consider downloading masm32 an perhaps
    and editor like RadASM, this way you'll get a good feel on many
    things. You'll be surprised how much you'll learn about the
    computers architecture by doing this! (and of course debugin your apps ollydbg! ).
    But then again it's much simpler using inline asm in VC++ or somehting else(using variables,
    debugging with sybols etc.). Be carefull though if you're writing inline asm
    for speed issues as an __asm-block within a c++-function will
    block the compiler from optimising some stuff.

    and as for speed you can have some noticable improvments
    writing in asm. And also it's probably a must when optimizing for
    MMX,3DNow!,SSE etc.

    all and all knowing asm can't hurt you, only improve your knowledge and programming skills!

    /btq

    oh and a link aswell: win32asm.cjb.net
    you'll find links to tutorials,MASM,RadASM and OllyDbg(I think) (and a great win32asm tut and lotsof usefull stuff )
    Last edited by btq; 06-25-2003 at 02:23 PM.
    ...viewlexx - julie lexx

  7. #7
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Assembly Language Instruction Set Reference

    Assuming you're using a Pentium... you can download the Instruction Set Reference from Intel!

    The hard copy doesn't seem to be avaliable right now (it's usually free!) Apparently they're between revisions.

  8. #8
    julie lexx... btq's Avatar
    Join Date
    Jun 2002
    Posts
    161
    Assuming you're using a Pentium... you can download the Instruction Set Reference from Intel!
    ..or go to http://betov.free.fr/SpAsm.html and download x86eas.hlp
    which is pretty much the same but alot faster to use imo

    /btq
    ...viewlexx - julie lexx

  9. #9
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Embedding ASM in C++ isn't hard. Its been a long time since I used it, but I think in general, you could access the function arguments simply by there name, and then do an explicit C++ return at the end of your function when the ASM block ended (I believe returning was equivalent to moving onto the eax register, but I'm not positive). At any rate, just tell your compiler to generate the assembly, and you'll be able to tell.

    (An) assembly language is a great fun and quite useful to learn. I'm not certain that you'll find yourself applying it in C++ though. It does make you think more about how your code is executed, and how to make things better/more efficient though, and that in itself can be worth it.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  10. #10
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Alright, thanks guys, I download MASM so I'll look at the links and hopefully find a decent tutorial. I guess I'll learn that and then decide if I want to learn more about C++/Asm from there.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  11. #11
    Registered User MicroFiend's Avatar
    Join Date
    Nov 2002
    Posts
    80
    If u want to learn true assembly dont download masm32 it sux it doesnt stick to origional assembly and u can use non assembly compare functions instead of using the origional 'jump' commands... yuk... u should goto source forge and download nasm its a lot nicer and u can set it to 16bit or 32bit apps make bootstrap loaders and all sorts (plus non platform specific).. btw i suggest not using the dos interrupts (unless ur doing a large program at a high rate) i found it alot more beneficial using bios interupts.. but just a thought

  12. #12
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    2) Most games nowadays will not use any ASM at all. The fact that most graphics work has been moved off the CPU onto dedicated graphics hardware, and the existence of DirectX and similar hardware abstration layers have pretty much removed the need.
    Not true. For proof look up some game programming jobs at www.monsterjobs.com. There are several game programmer positions that require you to know or be familiar with assembly, including a position to work on the new Madden 2004 NFL footbal game.

    Also, if you have good exposure to assembly then when new things come along like vertex shaders and per-pixel shaders which use a form of their own assembly, it will not be nearly as hard to learn and use. It will also help you when you start coding your own scripting language - complete with variables and structures. Understanding assembly language will give you a very very good understanding of what is going on underneath all that C code, or any code for that matter. Given this it will also aid you in writing faster code because you will begin to understand exactly what the compiler is going to turn your C code into. Granted, compiler optimizations are at their prime today but as you can see from all these posts - even with today's screaming machines you can still bring them to their knees with poorly written, poorly thought out, and poorly designed code.

    And when Windows tells you that your program has failed and gives you one of those memory dumps, you can pinpoint exactly where your code failed, what instruction it failed on, and what condition the registers were in at the time it failed. Very handy.

    So even if you do not use assembly language programming in any of your code from here to eternity, it's still a great skill to have.

    For a very good starting book on assembly, check out Randall Hyde's art of assembly language programming or AOA. Look it up on www.google.com, sorry I don't have the link on this system. Don't get the HLA version, get the orignal version of the book.
    Last edited by VirtualAce; 06-26-2003 at 08:36 AM.

  13. #13
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    As Zach L. sorta said, the value returned is the one in the eax register.

    Code:
    mov eax, 7
    ret
    That returns 7. Good luck on learning ASM
    Away.

  14. #14
    Registered User
    Join Date
    Jun 2003
    Posts
    85
    Bubba,

    Randall Hydes website is http://webster.cs.ucr.edu/
    and you are absolutely correct! it is a good reference site!

    JaWiB, TASM is also just as good as NASM. Turbo Assembler comes with comes with a nice TASM Reference that I personally find very helpful.

    cheers,
    Ben
    Medical Robotics: "Pursuing perfection in healthcare through innovations in robotics and information technologies for medicine and surgery."

  15. #15
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by Ben_Robotics
    Bubba,

    Randall Hydes website is http://webster.cs.ucr.edu/
    and you are absolutely correct! it is a good reference site!

    JaWiB, TASM is also just as good as NASM. Turbo Assembler comes with comes with a nice TASM Reference that I personally find very helpful.

    cheers,
    Ben
    The only problem with TASM is it's lack of support these days - Borland dont seem interested in it. There was talk of people trying to lobby Borland to release the code of TASM and put it out as open source....sounds like a good idea to me but I doubt Borland would allow it...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc asm code syntax
    By Uberapa in forum C Programming
    Replies: 4
    Last Post: 06-15-2007, 01:16 AM
  2. Understanding ASM
    By kuphryn in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 07-12-2002, 07:39 PM
  3. Inline asm
    By wavering in forum C Programming
    Replies: 2
    Last Post: 01-29-2002, 02:42 AM
  4. asm mov
    By manu in forum C++ Programming
    Replies: 1
    Last Post: 12-15-2001, 12:59 PM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM