Using __asm keyword & Get Segment/offset in VC 5.0

    I need get the segment/Offset in Visual C5.0, I Use __asm keyword but I have message errors (error C2415: improper operand type and error C2443: operand size conflict, When I use seg and offset keyword)

by sample I use:
int myOffset( int num1){
int *n;
     n= &num1;              
   __asm xor eax,eax
   __asm mov eax, offset n    <== I have MESSAGE ERROR
                          //return with result in eax

int mySeg( FARPROC num2){
   __asm xor eax eax
   __asm mov eax, seg &num2// <== I have MESSAGE ERROR ..??
                    //return with result in eax

What`s Up??
 How Can I get offset/segment of any function??
Please write a skeletor sample!!

Thanks Again for All!!

Who is Participating?
answer coming.
You can use the "mov reg,offset something" syntax only when the "something" is at a specifc address in memory.  Thus you can use it one a global variable, because that has a fixed address know at assembly/link time.  You cannot use it on a local variable, like n.  The address of the local variable, can change each time the function is called.  the address of the local varialbe must be "calculated" using the current stack frame.  You can use the LEA instruction to perform this calculation.  I beleive you can use the statement

 __asm LEA eax,n

and VC will generate the propper LEA instruction.  (It has to fill in the stack frame register and offset information, but I beleive it will do so for you.)
When it comes to
      __asm mov eax, seg &num2// <== I have MESSAGE ERROR ..??
you are running into the same problem and more.  Again, just like the offset, you can't load the segment of varaible that wasn't resolved at link time.  However, you shouldn't need to anyways.  First of all, the segment has to be the stack segment, so it is already loaded in SS.  Secondly, in win32, all the segments for a parituclar are the same, thus you should never need the segment.  They get loaded when the program starts and never get used after that.  Third, segments are 16 bit values, not 32.

One more thing, why clear EAX with a XOR before loading it?  That is unecessary..
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Now that I look at this again, I think there are more problems still.  What is it that you are trying to accomplish?  
gauss101998Author Commented:
Thanks Again Nietod!!!
Like I said, I think I still see problems after that.  You may want to discuss what your goal is.  
gauss101998Author Commented:
Thanks Nietod!
My Goald is:

       I need make two functions in a VxD, my problem is:
the firts function:
the VMMCall Get_PM_Vector service Returns the segment address in the CX register and the offset in the EDX register. The high word of the EDX register is always zero.
BUT I need return in my source code 32bits  including segment and
offset address, and I think join CX in the Segment of EDX .
(I word with VC 5.0m under WIN95) .

Any Idea Nietod?
Thanks Again!!

Essentially that is returning a 32 bit  far pointer.  You have a 16 bit address in CX and a 16 bit offset in DX.  This is the type of pointer used by DOS applications, or by device drivers that stilll need to communicate with DOS type hardware.  Note that for this you would ignopre the high 16 bits of EDX and just use DX.  

However, only your  VxD can use this pointer (Or maybe pass it to other VxDs or the OS--I don't know much about device drivers).  This pointer can't be passed back to an application.  (Well it can be passed back, but the application can't use it, allthough it could pass back to the VxD later on for the VxD to use).  The application runs in a flat memory model and cannot use pointers that have segments.

I still don't see where the functions above fit in.  Anyways, one problem I wonder about is i myOffset(), do  you want a pointer to n (that's what I showed you), or do you want a pointer to what n points to?
gauss101998Author Commented:
Thanks Nietod..

I want a poiter to what n points

One question:
  In turbo C I can use :
         void interrupt ( *oldtiempo)(__CPPARGS);
         setvect(INTR, oldtiempo);

old tiempo is a interrupt function... but In VC doesn´t exit  the type interrupt...
I need pass the routine interrupt function to my VxD ...
What Can I do?  The interrupt functios is FARPROC or what??


to get what n point to you would use MOV rather than LEA, like

LEA EAX,n ; Get a pointer to n (not what n points to.)
MOV EAX,n ; Get n, that is, get a pointer to what n points to.

I don't understand the rest of the comment.  But a procedure in a 32 bit program cannot be called by the DOS interrupts, like the timer interrupt.  
gauss101998Author Commented:
thanks Nietod!!!

     I use the MASM 6.11c , Visual C 5.0 and the DDK win95.  its ok??
(I have doubts, but I to make the test).

Thanks  sincerely....   !!!!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.