Inline Assembly: Defining and Calling a Procedure 
Author Message
 Inline Assembly: Defining and Calling a Procedure

Greetings.  I am trying to define and call an assembly
procedure via inline-assembly (VC++/Win32), however,
I'm not having much luck (my compiler keeps locking up).
Here's what I'm trying to do:

// begin code fragment
// some C++ code goes here
_asm
{
CALL TESTPROC  // 'undefined label' error here

Quote:
} // end asm

// more C++ code goes here

// assembly procedure/subroutine
void test(void)
{
_asm
{
TESTPROC:
PUSH EAX
XOR  EAX, EAX  ; perform some silly operation
POP  EAX

Quote:
} // end asm
} // end test()

// end code fragment

The above code produces an 'undefined label' error.

Okay, after you stop laughing, please provide some
insight into what I'm doing wrong.  Thanks.



Sat, 31 May 2003 00:42:11 GMT  
 Inline Assembly: Defining and Calling a Procedure


Quote:
> Greetings.  I am trying to define and call an assembly
> procedure via inline-assembly (VC++/Win32), however,
> I'm not having much luck (my compiler keeps locking up).
> Here's what I'm trying to do:

[code snipped]

Quote:
> The above code produces an 'undefined label' error.

> Okay, after you stop laughing, please provide some
> insight into what I'm doing wrong.  Thanks.

   Other's problems are not funny, to my tastes... C labels work only
on function scope. MSVC's inline assembler treats all labels as C
labels (i.e. possible "goto" targets). You can only call an assembly
procedure from the same *C* function here. E.g.:

void asm_procs()
{

Quote:
> _asm
> {
> CALL TESTPROC  // 'undefined label' error here
> } // end asm
> // more C++ code goes here
> _asm
> {
> TESTPROC:
> PUSH EAX
> XOR  EAX, EAX  ; perform some silly operation
> POP  EAX
> } // end asm
} // end asm_procs()

/VD

Programming links at: -- http://phantom.urbis.net.il/bphantom --
Tripple DOS home at: --
http://phantom.urbis.net.il/bphantom/tridos.html --
Executing Inspector home at: -- http://phantom.urbis.net.il/inspector
--

-- I hate you and you hate me
And everybody smiles ---
[R.J. DIO]

Sent via Deja.com http://www.deja.com/
Before you buy.


Newsgroups: comp.lang.asm.x86
Subject: Re: Inline Assembly: Defining and Calling a Procedure
Organization: Deja.com - Before you buy.
Lines: 55

X-Article-Creation-Date: Mon Dec 11 23:45:23 2000 GMT
X-Http-User-Agent: Mozilla/4.51 [en] (Win95; I)
X-Http-Proxy: 1.0 x55.deja.com:80 (Squid/1.1.22) for client 213.8.4.149
X-MyDeja-Info: XMYDJUIDblack_phantom


Quote:
> Greetings.  I am trying to define and call an assembly
> procedure via inline-assembly (VC++/Win32), however,
> I'm not having much luck (my compiler keeps locking up).
> Here's what I'm trying to do:

[code snipped]

Quote:
> The above code produces an 'undefined label' error.

> Okay, after you stop laughing, please provide some
> insight into what I'm doing wrong.  Thanks.

   Other's problems are not funny, to my tastes... C labels work only
on function scope. MSVC's inline assembler treats all labels as C
labels (i.e. possible "goto" targets). You can only call an assembly
procedure from the same *C* function here. E.g.:

void asm_procs()
{

Quote:
> _asm
> {
> CALL TESTPROC  // 'undefined label' error here
> } // end asm
> // more C++ code goes here
> _asm
> {
> TESTPROC:
> PUSH EAX
> XOR  EAX, EAX  ; perform some silly operation
> POP  EAX
> } // end asm
} // end asm_procs()

/VD

Programming links at: -- http://phantom.urbis.net.il/bphantom --
Tripple DOS home at: --
http://phantom.urbis.net.il/bphantom/tridos.html --
Executing Inspector home at: -- http://phantom.urbis.net.il/inspector
--

-- I hate you and you hate me
And everybody smiles ---
[R.J. DIO]

Sent via Deja.com http://www.deja.com/
Before you buy.



Sat, 31 May 2003 08:23:07 GMT  
 Inline Assembly: Defining and Calling a Procedure

Quote:

> Greetings.  I am trying to define and call an assembly
> procedure via inline-assembly (VC++/Win32), however,
> I'm not having much luck (my compiler keeps locking up).
> Here's what I'm trying to do:

> // begin code fragment
> // some C++ code goes here
> _asm
> {
> CALL TESTPROC  // 'undefined label' error here
> } // end asm
> // more C++ code goes here

> // assembly procedure/subroutine
> void test(void)
> {
> _asm
> {
> TESTPROC:
> PUSH EAX
> XOR  EAX, EAX  ; perform some silly operation
> POP  EAX
> } // end asm
> } // end test()
> // end code fragment

> The above code produces an 'undefined label' error.

You are thinking like you're using assembler, and in previos posts you

Quote:
> FYI:  I'm using Windows98 and VisualC++ 6.0

so if you're using C++, think C++.
You don't call procedures like that, call them like you would do in C.
Use inline asm only to optimize parts inside a procedure, there is no
point writing code this way in a VC++, thats what (M/N/T)ASM is for.

Anyways, you could wrote 'call test', but that whats the point? You have
compiler to do that for you. Code should look like this:

// begin code fragment
// some C++ code goes here
_asm
{
mov        eax,34
//some asm

Quote:
}

test();
_asm{
mov        ecx,89
//some asm
Quote:
} // end asm

// more C++ code goes here

// assembly procedure/subroutine
void test(void)
{
_asm
{
TESTPROC:
PUSH EAX
XOR  EAX, EAX  ; perform some silly operation
POP  EAX

Quote:
} // end asm
} // end test()

// end code fragment

take care
Otto



Sat, 31 May 2003 08:23:10 GMT  
 Inline Assembly: Defining and Calling a Procedure

Greetings, Otto and Stargazer.  Thank you for taking the time
to reply to my post.

Quote:
> You are thinking like you're using assembler...

Exactly.  I'm used to programming in 100% assembly (DOS/MASM5);
I've never done any mixed-language programming before.

Quote:
> so if you're using C++, think C++.  You don't call procedures like that,
> call them like you would do in C.

Okay.  So, if I understand correctly, it's impossible to use 'CALL
ASMPROC' via inline assembly -- instead, you have to use 'asmproc();',
right?

So, in order to call asmproc() via inline assembly, you have to
do so using 'inline C++' -- that is, you have to call a 'C++' function
within your inline assembly block, right?  (I believe I read somewhere
that it is possible to call a C++ function from within an asm block.)

Here's what I've gleaned from your replies (please feel free to add
corrections/suggestions):

// some C++ code goes here
_asm
{
// some asm code goes here

Quote:
}

asmproc(); // C++ substitute for 'CALL ASMPROC'
_asm
{
// continue with asm code

Quote:
}

Question:  Will VC++ fiddle with the registers before and/or after
the asmproc() function/procedure is called/invoked?

Thanks in advance.



Mon, 02 Jun 2003 02:54:58 GMT  
 Inline Assembly: Defining and Calling a Procedure


Quote:

>Okay.  So, if I understand correctly, it's impossible to use 'CALL
>ASMPROC' via inline assembly -- instead, you have to use 'asmproc();',
>right?

You can use CALL via inline assembly.

//some C here
_asm
{
     //asm stuff

    call asmproc

    //more stuff

Quote:
}

..........

void asmproc(void)
{
     printf("Hello\n");

Quote:
}

..........

---------------------
Kyle Martin



Mon, 02 Jun 2003 04:44:10 GMT  
 Inline Assembly: Defining and Calling a Procedure

Quote:
>Okay.  So, if I understand correctly, it's impossible to use 'CALL
>ASMPROC' via inline assembly -- instead, you have to use 'asmproc();',
>right?

It is possible, but be carefull C++ might expect something on the stack. I
strongly suggest that you don't call them from inside asm block, instead:
-close asm block
-call c function
-open asm block and countinue with asm

Using CALL instead of actually calling a function (like from ordinary C),
makes no sense, since you can't do that any better than a compiler can. (and
it keeps track of the arguments on the stack, and you have to do it manually).

Quote:
>So, in order to call asmproc() via inline assembly, you have to
>do so using 'inline C++' -- that is, you have to call a 'C++' function
>within your inline assembly block, right?  (I believe I read somewhere
>that it is possible to call a C++ function from within an asm block.)

hmmm...consider this example for sum of X(n), where 'X(n)' is 'n+n', and n is
in a range of 0 to 10:

void main()
{
int n, sum=0;

for (n=0;n<=10;n++){
 sum+=addtwo(n);

Quote:
}
} //end main

int square(int arg)
{
return (arg+arg);
Quote:
}

--------------------------------

Now, with inline asm I would do:

void main()
{
int n, sum=0,h;

_asm{
 mov ecx,0
 mov n,ecx
loop:
 }
h=addtwo(n);
_asm{
 mov ebx,sum
 add ebx,h
 mov sum,ebx
 inc ecx
 cmp ecx,10
 jle loop

Quote:
}
} //end main

int addtwo(int arg)
{
int he;
_asm{
 mov edx,arg
 sal edx,1  //arg+arg
 mov he,edx

Quote:
}
return (he);
}
>Question:  Will VC++ fiddle with the registers before and/or after
>the asmproc() function/procedure is called/invoked?

Absolutely, result is returned in eax, so above example could also be written:

void main()
{
int n, sum=0,h;

_asm{
 mov ecx,0
 mov n,ecx
loop:
 }
h=addtwo(n);
_asm{
 mov ebx,sum
 add ebx,eax  //result is returned in eax
 mov sum,ebx
 inc ecx
 cmp ecx,10
 jle loop

Quote:
}
} //end main

Best way to figure out what is C doing when calling functions, is to place a
brekpoint, right before calling a function, then switch into dissassembly view
(Alt-8) and explore what happens.

Well...hope this helps
Otto



Mon, 02 Jun 2003 10:50:31 GMT  
 Inline Assembly: Defining and Calling a Procedure

Quote:

> Greetings.  I am trying to define and call an assembly
> procedure via inline-assembly (VC++/Win32), however,
> I'm not having much luck (my compiler keeps locking up).
> _asm
> {
> CALL TESTPROC  // 'undefined label' error here
> } // end asm
> // more C++ code goes here

> // assembly procedure/subroutine
> void test(void)
> {
> _asm
> {
> TESTPROC:
> PUSH EAX
> XOR  EAX, EAX  ; perform some silly operation
> POP  EAX
> The above code produces an 'undefined label' error.

> Okay, after you stop laughing, please provide some
> insight into what I'm doing wrong.  Thanks.

Hi again,
I'm not sure what you mean when you say your compiler is locking up.
If it generates the label error and then freezes to the point of needing
a
reboot, then I have no suggestions.

But if it generates the error message and then refuses to run the code,
it's
possible that the label being CALLed needs to be defined before the call
is made.
This means putting the TESTPROC code physically ahead of the CALL
instruction.
(In assembler,something like this is necessary when using the newer
"high level" calling
conventions.)
;----------------------------------------------------------
Is it possible that VC++ requires you to define an actual procedure ?

TESTPROC    PROC
...code
    ret
TESTPROC    ENDP
;------------------------------------------
Your message seems to imply that you don't get beyond the compiler error

message, but in case I'm reading it wrong, and the machine is locking up
while
trying to run the code, consider the following :

When a CALL is executed there must be some provision for a RETurn.
If you have a RET statement but didn't include it in the code
snippet then my next comments have no bearing on the matter.

But, if your machine is locking up while running the code, it could be
that it's
executing the CALL to TESTPROC but because there's no RETurn, the
instruction pointer is dropping to the next "thing" in the memory after
TESTPROC and executing this garbage as code.

In assembler, a CALL can be made to code which is truly a PROC
(procedure)
or it can be made to code which (like your snippet) is just a label. But
in either
case there must be a RETurn provided. Usually, a simple RET is enough.

Good luck,
Austin Kelly



Wed, 18 Jun 2003 13:03:26 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Accessing C #define in inline assembly

2. brk system call in inline assembly.

3. how to call a c++ constructor in vc++ inline assembly

4. Win32: Calling API Function via Inline Assembly

5. problem porting assembly codes from ms inline assembly to linux (gnu - AT&T syntax)

6. Calling a GNAT (or other Ada 95) Procedure from x86 Assembly

7. Calling assembly procedures

8. Defining procedures whose argument list is result of procedure

9. How to setup a procedure in a DLL that calls a procedure in another DLL

10. Call a procedure from a procedure

11. Calling a procedure with a variable procedure name

12. Procedure that only calls a procedure?

 

 
Powered by phpBB® Forum Software