Question about inline assembly with VC++ 6...

Started by
14 comments, last by Mike 24 years, 1 month ago
The code:

void AddNumbers( int a, int b, int &total )
{
    total = a+b;
}

void main()
{
    int a = 2;
    int b = 3;
    int total = 0;

    __asm
    {
        //some code here
    }
}
 
The Question: What is the proper way to call the AddNumbers function using inline assembly to push the local variables onto the stack and call the function?
Advertisement
You push the variables in reverse order, and the pointer uses the "offset" keyword.

Your code would appear:

__asm
{
mov eax, offset total
push eax
mov eax, b
push eax
mov eax, a
push eax
call AddNumbers
}

(I''m assuming that what you gave here was just an example to get your answer -- because to actually use Assembly to call an add function instead of coding the add itself in assembly is pretty much the same as hitting yourself in the head with a hammer because it feels so good when you stop )

-Chris

---<<>>---
Chris Rouillard
Software Engineer
crouilla@hotmail.com
---<<>>--- Chris Rouillard Software Engineercrouilla@hotmail.com
Yes, I''m aware of the ADD in assembly; and no, I would not normally write the add funtion in assembly (I''m pretty sure that VC++ generate good enough assembly for this feature).

I''m working on a scripting language and I''m planning on using inline assembly to call the native functions. Anyway, I tried your code (pretty much exactly what I had except I use LEA for the local variables) and it does not work. Here is the error code:

C:\Programming\C++\Scripting Language\test\dos_main.cpp(17) : error C2415: improper operand type

That is the same error that prompted the post. If anyone knows how to fix the problem let me know. I''ve tried looking through the MSDN to find something but what I found did not help.

Thanks for trying crouilla, please play again.
Hmmm... what''s on line 17?

-Chris
---<<>>--- Chris Rouillard Software Engineercrouilla@hotmail.com
I supose that would help wouldn''t it.

#include void AddNumbers( int &a ){	printf( "%d", a );}void main(){	int a = 2;	int b = 3;	int total = 0;        __asm        {                mov eax, offset total                push eax                   //line 17                mov eax, b                push eax                mov eax, a                push eax                call AddNumbers        }} 
Taken directly from MSDN:

An __asm block can call only global C++ functions that are not overloaded. If you call an overloaded global C++ function or a C++ member function, the compiler issues an error.

You can also call any functions declared with extern "C" linkage. This allows an __asm block within a C++ program to call the C library functions, because all the standard header files declare the library functions to have extern "C" linkage.

With this in mind I would say that you need to use "C" linkage.

---Ranok---
When you consider the number of classes and the way I use classes "c" linkage would be a problem.

I supose that I could declare the function using extern "c", but how does that explain the error?

Edited by - Mike on 3/3/00 11:37:38 PM
Why do you want to call functions in Assembly? I''ve looked at code generated by CL (Microsoft''s C++ compiler). Function calls are reduced to exactly the assembly code that you''re trying to write. Just call the function in C++.

As for the error, I don''t know why it was caused, but I do know you wrote AddNumbers accepting only one parameter, yet you''re pushing three parameters to the stack.

Also, you can push variables directly. You don''t need to move them to EAX and then push them.

~CGameProgrammer( );

~CGameProgrammer( );Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
quote: Original post by Mike
Here is the error code:

C:\Programming\C++\Scripting Language\test\dos_main.cpp(17) : error C2415: improper operand type

That is the same error that prompted the post.



Not trying to test anyone''s nerves here, but I don''t think VC++ is gonna like the OFFSET in the snippet you are showing.


Don''t know if this is what you are trying or not:

#include "stdio.h"void AddNumbers( int a, int b, int c ){        c = a + b;        printf( "%d\n", c );}void main(){        int num1 = 4;        int num2 = 8;        int testVal = 0;        __asm        {                push testVal;                push num2;                push num1;                call AddNumbers;                pop num1;                pop num2;                pop testVal;	}        printf( "%d\n", testVal );}  


Anyhow, good luck
--deadlinegrunt

~deadlinegrunt

CGameProgrammer:

The reason I am using assembly to call functions is that I am writing a scripting language that can call native C++ functions. Since the C++ functions can have any variety of a parameter list I cannot simple call the functions through C++ (even if I used a function pointer). Using assembly I will be able to use the same function to call any kind of C++ function. I''m not to good at explaining things, if you want to see what I mean just wait about a week.

The reason the function only had one parameter is because I reduced to one from the original to play with it my self. When crouilla posted his code I simply copied and pasted it into my program to see if I got the error message. The error message has nothing to do with the fact that there was only one paramter.

Thanks for the help

deadlinegrunt:

Thanks for the code. That is exactly what I needed.

This topic is closed to new replies.

Advertisement