Closed Thread
Results 1 to 9 of 9

Thread: Problems Using Assembler In C++

  1. #1
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Problems Using Assembler In C++

    I've been programming in assembler for about a year now, and am trying to incorporate it into my C++ code. Visual C++ refuses to cooperate, however. Here's the problematic code:

    [HIGHLIGHT="C++"]
    /*
    Variable declarations omitted to save space
    FileHandle and ErrorCode are both unsigned words
    */
    unsigned __int16 open(char *FilePath)
    {
    __asm
    {
    //opening the file...save ds, get string pointer to path
    push ds
    add sp,2
    pop edx
    pop ds
    sub sp,8
    //Call DOS interrupt 21h, subfunction 4Dh - open file.
    mov ax,0x4d02
    xor cx,cx
    int 21h
    //if the file can't be opened, then set error
    //and set the file handle to NULL
    jc error_trap //ERROR
    mov FileHandle,ax //ERROR
    mov ErrorCode,0 //ERROR
    jmp restore_ds //ERROR
    error_trap:
    mov FileHandle,0 //ERROR
    mov ErrorCode,ax //ERROR
    restore_ds:
    pop ds
    };
    return ErrorCode;
    }
    [/HIGHLIGHT]

    The compiler gives me the following error for the marked lines:

    error C2415: improper operand type

    Can anyone help me please?
    Last edited by dargueta; 10-07-2007 at 02:53 PM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    v0id is offline Retired
    Join Date
    Apr 2007
    Posts
    2,937
    Blog Entries
    3
    Rep Power
    42
    I've heard many people having problems with inlining Assembly in C++-code. Personally, I don't prefer to do it in this way. I prefer to an external Assembly-file, and then link to it, when compiling.

    <offtopic>
    Is there some reason for why you're using 8086-specific code mixed with x86? I'm just curious about this, and want to know, why some people still uses the DOS interrupts, like 0x10, 0x21, ...
    </offtopic>

  4. #3
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59
    It's easier that way, I guess. I want to create fast and tight code. Using WinAPI is annoying because my compiler chokes when I link to a library, DLL, or anything like that; besides, I can tailor the code to suit my own purposes and ignore unnecessary things such as stack frames for functions that don't have arguments, etc. Can you think of a better method of coding stuff like this?

    I've never tried linking an ASM file--how exactly would I do that? I know I have to use MASM directives and stuff like that, which I really don't like. I tend to be a simplistic traditionalist, if that makes any sense.

  5. #4
    v0id is offline Retired
    Join Date
    Apr 2007
    Posts
    2,937
    Blog Entries
    3
    Rep Power
    42
    I've wrote an article on the subject on my website, some months ago. It shows how it works (which you probably knows. You seems experienced) and how you're linking with an external assembly-file (it's this part you might want to look into.)

  6. #5
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59
    Thanks a lot; that really helped. I do have one question, however. I haven't really worked with functions that return values in assembler; if the return value is larger than 32 bits (say, a 16:32 pointer for example) where would it go? Pushing it onto the stack would cause a stack overflow error because esp < ebp.

    Also, by any chance, do you know how to call interrupts in C without crashing? I've written functions in assembler for graphics operations, but they all crash with an access violation error when I call an interrupt. The only ones I've used so far are 10h (graphics), 16h (keyboard), and 21h (I/O).
    Last edited by dargueta; 10-08-2007 at 09:57 AM.

  7. #6
    v0id is offline Retired
    Join Date
    Apr 2007
    Posts
    2,937
    Blog Entries
    3
    Rep Power
    42
    I don't use Assembly inlined in C-code, and I've never had, so I can't really help you, sorry.

    The external linking of Assembly will probably work-out, but you'll have to do a little extra work (when linking, as you saw in my article) F.ex. you could write some sub-procedures in your Assembly-file, and then use them externally. IMO, it makes the code prettier looking as well. I like to keep the different language alone, and not to use some features in one language to use another language.

  8. #7
    davHunter is offline Newbie
    Join Date
    Oct 2007
    Posts
    8
    Rep Power
    0
    About calling interrupts in C, it's not possible (at least in Windows) since the OS runs in protected mode, and the user programs don't have privilege to make those calls. To do it, you would need to compile the project as DOS project (but there are only a few compiler that will do that, and its not worth it because you lose all the benefits from the protected mode).
    The other option is to do it as a driver project, where I think you can use the interruptions.
    Anyway, in these days it's probably you won't want to do that, because using old DOS-interrupts is really slow, and every card has its own quick-operation shortcuts.
    For graphics the best way to go is with DirectX or OpenGL (or any other software like these), since those companies have searched, buyed, and implement all those shortcuts (accelerated hardware)...

    So unless you have many men and time, or you are only learning the basics, you're best shot is to use DirectX (only for Windows) or OpenGL.

    PD: If you want to see the difference between Accelerated Hardware and DOS interrutpts, just look for a comparison between DirectX HAL and REF (for example, in the Direct3D SDK Samples)
    Last edited by davHunter; 10-14-2007 at 07:29 PM.

  9. #8
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59
    What libs and dlls would I need to use DX?

  10. #9
    davHunter is offline Newbie
    Join Date
    Oct 2007
    Posts
    8
    Rep Power
    0
    You must download the DirectX SDK from Microsoft website.
    Install it, and then add the paths to Visual Studio (or any other IDE / Compiler you use)...

    That's for using it in C (development).
    When playing the game, you need the runtime version of DX (almost every decent PC has DX).
    Last edited by davHunter; 10-15-2007 at 06:51 PM.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. TASM assembler help
    By M88 in forum Assembly
    Replies: 6
    Last Post: 06-25-2010, 07:38 PM
  2. Two Pass Assembler
    By rivci in forum General Programming
    Replies: 2
    Last Post: 04-05-2010, 09:58 PM
  3. Assembler
    By fondi in forum C and C++
    Replies: 2
    Last Post: 03-31-2010, 09:26 AM
  4. Assembler guy
    By Slider in forum Introductions
    Replies: 6
    Last Post: 08-27-2009, 09:37 AM
  5. 8086 Assembler HELP
    By BamaBUB1983 in forum General Programming
    Replies: 3
    Last Post: 12-12-2007, 07:11 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts