asm dropped into VC++ code

Hello,
I've been playing around with assembler, trying to get the basic feel for it, just for the fun of it. I'm trying just to make a function that adds two numbers and returns the result. I think I have the function correct so far, but I don't know how to return a value. So far:
[code]#include
using namespace std;

void addnums(short int i, short int j)
{
short int a;

__asm {
mov ax, i
mov bx, j
add ax, bx
mov a, ax
}

cout << a << '
';
}

int main()
{
__asm {
push 1
push 3
call addnums
add esp, 8
}

return 0;
}[/code]
Is there anything wrong so far?

Also, in this code, will i be 3, and j be 1, because of how paramaters are pushed?

Another thing, without the
[code]add esp, 8[/code]
line, the program runtime errors, and I'm not sure why this is necessary. I know that esp is the stack pointer (extended?), but why must it be after the function call?

Thanks.

-Tony

Comments

  • : Hello,
    : I've been playing around with assembler, trying to get the basic feel for it, just for the fun of it. I'm trying just to make a function that adds two numbers and returns the result. I think I have the function correct so far, but I don't know how to return a value. So far:
    : [code]#include
    : using namespace std;
    :
    : void addnums(short int i, short int j)
    [red] should be int addnums(int i, int j)[/red]
    : {
    : short int a;
    :
    : __asm {
    : mov ax, i
    : mov bx, j
    : add ax, bx
    : mov a, ax
    : }
    :
    : cout << a << '
    ';
    [red]use return a
    not cout<<a<<'
    ' <---use this in function main;[/red]
    : }
    :
    : int main()
    : {
    : __asm { [red]<----not needed just call addnum[/red]
    : push 1
    : push 3
    : call addnums
    : add esp, 8
    : }
    [red] example call for your function would be 1st declare a variable
    int x;
    then have it equal the result from the function
    x=addnums(1,4);
    then print out
    cout <<x<<'
    '[/red];
    :
    : return 0;
    : }[/code]
    : Is there anything wrong so far?
    :
    : Also, in this code, will i be 3, and j be 1, because of how paramaters are pushed?
    :
    : Another thing, without the
    : [code]add esp, 8[/code]
    : line, the program runtime errors, and I'm not sure why this is necessary. I know that esp is the stack pointer (extended?), but why must it be after the function call?
    :
    : Thanks.
    :
    : -Tony
    :

    [green] I'm not to familiar with c++ so what I just said might not work quite right but is a example on how to do it.[/green]


  • : : Hello,
    : : I've been playing around with assembler, trying to get the basic feel for it, just for the fun of it. I'm trying just to make a function that adds two numbers and returns the result. I think I have the function correct so far, but I don't know how to return a value. So far:
    : : [code]#include
    : : using namespace std;
    : :
    : : void addnums(short int i, short int j)
    : [red] should be int addnums(int i, int j)[/red]
    : : {
    : : short int a;
    : :
    : : __asm {
    : : mov [red][b]e[/b][/red]ax, i
    : : mov [red][b]e[/b][/red]bx, j
    : : add [red][b]e[/b][/red]ax, [red][b]e[/b][/red]bx
    : : mov a, [red][b]e[/b][/red]ax
    [red][b]If you drop 'short' from the parameters,
    you should use the 32-bit REGs now...[/b][/red]

    [red][b]Also, shorter would have been:
    : : mov eax, i
    : : add eax, j
    : : mov a, eax
    [/b][/red]
    : : }
    : :
    : : cout << a << '
    ';
    : [red]use return a
    : not cout<<a<<'
    ' <---use this in function main;[/red]
    : : }
    : :
    : : int main()
    : : {
    : : __asm { [red]<----not needed just call addnum[/red]
    : : push 1
    : : push 3
    : : call addnums
    : : add esp, 8
    : : }
    : [red] example call for your function would be 1st declare a variable
    : int x;
    : then have it equal the result from the function
    : x=addnums(1,4);
    : then print out
    : cout <<x<<'
    '[/red];
    : :
    : : return 0;
    : : }[/code]
    : : Is there anything wrong so far?
    : :
    : : Also, in this code, will i be 3, and j be 1, because of how paramaters are pushed?
    : :
    : : Another thing, without the
    : : [code]add esp, 8[/code]
    : : line, the program runtime errors, and I'm not sure why this is necessary. I know that esp is the stack pointer (extended?), but why must it be after the function call?
    : :
    : : Thanks.
    : :
    : : -Tony
    : :
    :
    : [green] I'm not to familiar with c++ so what I just said might not work quite right but is a example on how to do it.[/green]
    :
    :
    :

  • : : : Hello,
    : : : I've been playing around with assembler, trying to get the basic feel for it, just for the fun of it. I'm trying just to make a function that adds two numbers and returns the result. I think I have the function correct so far, but I don't know how to return a value. So far:
    : : : [code]#include
    : : : using namespace std;
    : : :
    : : : void addnums(short int i, short int j)
    : : [red] should be int addnums(int i, int j)[/red]
    : : : {
    : : : short int a;
    : : :
    : : : __asm {
    : : : mov [red][b]e[/b][/red]ax, i
    : : : mov [red][b]e[/b][/red]bx, j
    : : : add [red][b]e[/b][/red]ax, [red][b]e[/b][/red]bx
    : : : mov a, [red][b]e[/b][/red]ax
    : [red][b]If you drop 'short' from the parameters,
    : you should use the 32-bit REGs now...[/b][/red]
    [green] I thought it defaulted to short if you didn't specify the size? Or do you need to specify the
    size in this case? Just curious...[/green]
    : [red][b]Also, shorter would have been:
    : : : mov eax, i
    : : : add eax, j
    : : : mov a, eax
    : [/b][/red]
    : : : }
    : : :
    : : : cout << a << '
    ';
    : : [red]use return a
    : : not cout<<a<<'
    ' <---use this in function main;[/red]
    : : : }
    : : :
    : : : int main()
    : : : {
    : : : __asm { [red]<----not needed just call addnum[/red]
    : : : push 1
    : : : push 3
    : : : call addnums
    : : : add esp, 8
    : : : }
    : : [red] example call for your function would be 1st declare a variable
    : : int x;
    : : then have it equal the result from the function
    : : x=addnums(1,4);
    : : then print out
    : : cout <<x<<'
    '[/red];
    : : :
    : : : return 0;
    : : : }[/code]
    : : : Is there anything wrong so far?
    : : :
    : : : Also, in this code, will i be 3, and j be 1, because of how paramaters are pushed?
    : : :
    : : : Another thing, without the
    : : : [code]add esp, 8[/code]
    : : : line, the program runtime errors, and I'm not sure why this is necessary. I know that esp is the stack pointer (extended?), but why must it be after the function call?
    : : :
    : : : Thanks.
    : : :
    : : : -Tony
    : : :
    : :
    : : [green] I'm not to familiar with c++ so what I just said might not work quite right but is a example on how to do it.[/green]
    : :
    : :
    : :
    :
    :

  • [blue]In VC++ (from version 4.0) [b]short int[/b] means 16-bit integer and just [b]int[/b] means 32-bit integer. So, if you try to MOV 32 bit into AX - you get an error.[/blue]
  • Thanks for the help. I don't think I was very clear in my question. My question is: how to change the following function into pure asm, not using any C/C++ statements (except variable declarations if needed).
    ie
    [code]int addnums(int i, int j)
    {
    [green]//no C/C++[/green]
    __asm {
    mov eax, i
    mov ebx, j
    add eax, ebx
    ret
    [green]// ??? how to return a value?
    //push onto stack? put in a certain register?[/green]
    }
    [green]//no C/C++[/green]
    }[/code]
    and functions the same as
    [code]int addnums(int i, int j) {
    return i + j;
    }[/code]

    and then also, how to call it in asm code
    [code]int main()
    {
    int a;

    [green]//a = addnums(1, 3); not in C/C++[/green]
    __asm {
    push 1
    push 3
    call addnums
    add esp, 8
    [green]//mov a, ???
    //how to retrieve value of function?[/green]
    }

    return 0;
    }[/code]

    Thanks again.

    -Tony

  • : Thanks for the help. I don't think I was very clear in my question. My question is: how to change the following function into pure asm, not using any C/C++ statements (except variable declarations if needed).
    : ie
    : [code]int addnums(int i, int j)
    : {
    : [green]//no C/C++[/green]
    : __asm {
    : mov eax, i
    : mov ebx, j
    : add eax, ebx
    : ret
    : [green]// ??? how to return a value?
    : //push onto stack? put in a certain register?[/green]
    : }
    : [green]//no C/C++[/green]
    : }[/code]
    : and functions the same as
    : [code]int addnums(int i, int j) {
    : return i + j;
    : }[/code]
    :
    : and then also, how to call it in asm code
    : [code]int main()
    : {
    : int a;
    :
    : [green]//a = addnums(1, 3); not in C/C++[/green]
    : __asm {
    : push 1
    : push 3
    : call addnums
    : add esp, 8
    : [green]//mov a, ???
    : //how to retrieve value of function?[/green]
    : }
    :
    : return 0;
    : }[/code]
    :
    : Thanks again.
    :
    : -Tony
    :
    :
    [blue]I never have done that, just because it will not speed up the code more... Usually, inline ASM is for internal use in functions, not for organizing the function calls and parameters passing...

    If you trying to make code faster - these tricks will not be of much help. What you need to do at the very first is to organize your data structures - align them, use multiple heap objects, cache your information, etc. And then, you have to look into the functions which get called most times and try to improve the function body with inline ASM. And finally you have to measure the real-time the functions executing - use [b]QueryPerformanceCounter()[/b] from Win32 API for this purpose.[/blue]
  • : : Thanks for the help. I don't think I was very clear in my question. My question is: how to change the following function into pure asm, not using any C/C++ statements (except variable declarations if needed).
    : : ie
    : : [code]int addnums(int i, int j)
    : : {
    : : [green]//no C/C++[/green]
    : : __asm {
    : : mov eax, i
    : : mov ebx, j
    : : add eax, ebx
    : : ret
    : : [green]// ??? how to return a value?
    : : //push onto stack? put in a certain register?[/green]
    : : }
    : : [green]//no C/C++[/green]
    : : }[/code]
    : : and functions the same as
    : : [code]int addnums(int i, int j) {
    : : return i + j;
    : : }[/code]
    : :
    : : and then also, how to call it in asm code
    : : [code]int main()
    : : {
    : : int a;
    : :
    : : [green]//a = addnums(1, 3); not in C/C++[/green]
    : : __asm {
    : : push 1
    : : push 3
    : : call addnums
    : : add esp, 8
    : : [green]//mov a, ???
    : : //how to retrieve value of function?[/green]
    : : }
    : :
    : : return 0;
    : : }[/code]
    : :
    : : Thanks again.
    : :
    : : -Tony
    : :
    : :
    : [blue]I never have done that, just because it will not speed up the code more... Usually, inline ASM is for internal use in functions, not for organizing the function calls and parameters passing...
    :
    : If you trying to make code faster - these tricks will not be of much help. What you need to do at the very first is to organize your data structures - align them, use multiple heap objects, cache your information, etc. And then, you have to look into the functions which get called most times and try to improve the function body with inline ASM. And finally you have to measure the real-time the functions executing - use [b]QueryPerformanceCounter()[/b] from Win32 API for this purpose.[/blue]
    :

    Ah, i see. Well, I actually was not doing this for a speed improvement, just for some knowledge of functions in asm. But, i now see that it isn't too important in these days of higher languages ;-). Thanks.

    -Tony

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories