Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-07-2007, 05:34 PM
dargueta dargueta is offline
Guru
 
Join Date: Oct 2007
Age: 18
Posts: 787
Last Blog:
Programs Under the Hoo...
Rep Power: 13
dargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the rough
Default 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:

C++ Code:
  1. /*
  2. Variable declarations omitted to save space
  3. FileHandle and ErrorCode are both unsigned words
  4. */
  5. unsigned __int16    open(char *FilePath)
  6. {
  7.     __asm
  8.     {
  9.         //opening the file...save ds, get string pointer to path
  10.         push    ds
  11.         add  sp,2
  12.         pop  edx
  13.         pop  ds
  14.         sub  sp,8
  15.         //Call DOS interrupt 21h, subfunction 4Dh - open file.
  16.         mov  ax,0x4d02
  17.         xor  cx,cx
  18.         int  21h
  19.         //if the file can't be opened, then set error
  20.         //and set the file handle to NULL
  21.         jc    error_trap     //ERROR
  22.         mov  FileHandle,ax //ERROR
  23.         mov  ErrorCode,0   //ERROR
  24.         jmp  restore_ds    //ERROR
  25. error_trap:
  26.         mov  FileHandle,0  //ERROR
  27.         mov  ErrorCode,ax //ERROR
  28. restore_ds:
  29.         pop  ds
  30.     };
  31.     return ErrorCode;
  32. }

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 05:53 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 10-08-2007, 01:01 AM
v0id's Avatar   
v0id v0id is offline
Retired
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,650
Last Blog:
CherryPy(thon)
Rep Power: 29
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
Send a message via MSN to v0id
Default

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>
__________________
05-03-2007 - 11-13-2008
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 10-08-2007, 11:54 AM
dargueta dargueta is offline
Guru
 
Join Date: Oct 2007
Age: 18
Posts: 787
Last Blog:
Programs Under the Hoo...
Rep Power: 13
dargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the rough
Default

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 10-08-2007, 12:34 PM
v0id's Avatar   
v0id v0id is offline
Retired
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,650
Last Blog:
CherryPy(thon)
Rep Power: 29
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
Send a message via MSN to v0id
Default

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.)
__________________
05-03-2007 - 11-13-2008
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 10-08-2007, 12:49 PM
dargueta dargueta is offline
Guru
 
Join Date: Oct 2007
Age: 18
Posts: 787
Last Blog:
Programs Under the Hoo...
Rep Power: 13
dargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the rough
Default

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 12:57 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 10-08-2007, 01:00 PM
v0id's Avatar   
v0id v0id is offline
Retired
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,650
Last Blog:
CherryPy(thon)
Rep Power: 29
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
Send a message via MSN to v0id
Default

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.
__________________
05-03-2007 - 11-13-2008
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 10-14-2007, 10:25 PM
davHunter davHunter is offline
Newbie
 
Join Date: Oct 2007
Posts: 8
Rep Power: 0
davHunter is on a distinguished road
Default

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 10:29 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 10-15-2007, 09:26 PM
dargueta dargueta is offline
Guru
 
Join Date: Oct 2007
Age: 18
Posts: 787
Last Blog:
Programs Under the Hoo...
Rep Power: 13
dargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the rough
Default

What libs and dlls would I need to use DX?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 10-15-2007, 09:48 PM
davHunter davHunter is offline
Newbie
 
Join Date: Oct 2007
Posts: 8
Rep Power: 0
davHunter is on a distinguished road
Default

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 09:51 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems in GETMEM.INC with newer WinXP security updates. dima_q Pascal/Delphi 0 06-04-2007 10:17 AM
Problems in compiling D-ITG MietitoreDAnime C and C++ 2 02-15-2007 12:18 PM
Problems dirkfirst HTML Programming 8 10-12-2006 09:00 PM
Problems with Google? Void Marketing 4 07-23-2006 08:12 PM


All times are GMT -5. The time now is 03:36 PM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 100%

Ads