Howdy, Stranger!

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

Categories

c++/asm - mixing assembler registers with c++ variables

star_phoenixstar_phoenix Member Posts: 92
I've been having problems trying to mix C++ variables with assembler registers. Below is an example of what I'm trying to do. At the moment, I don't care what the result is, I just want to know how to swap values from C++ to asm and vice-versa. In case it matters, I'm using MSVC++ 6.0.

[code]void getmouse(int *button,int *x,int *y)
{
__asm {
mov ax,3;
int 0x33;
*button=bx;
*x=cx;
*y=dx;
}
}[/code]

I've looked at a whole bunch of asm tutorials and stuff like that, but still don't know what to do.
«1

Comments

  • PatLDDDPatLDDD Member Posts: 30
    : I've been having problems trying to mix C++ variables with assembler registers. Below is an example of what I'm trying to do. At the moment, I don't care what the result is, I just want to know how to swap values from C++ to asm and vice-versa. In case it matters, I'm using MSVC++ 6.0.
    :
    : [code]void getmouse(int *button,int *x,int *y)
    : {
    : __asm {
    : mov ax,3;
    : int 0x33;
    : *button=bx;
    : *x=cx;
    : *y=dx;
    : }
    : }[/code]
    :
    : I've looked at a whole bunch of asm tutorials and stuff like that, but still don't know what to do.
    :

    Hello,

    The simplist way to do is to create temp var like.
    [code]
    void getmouse(int *button,int *x,int *y)
    {
    int iX,iY,iButton;
    __asm {
    mov ax,3;
    int 0x33;
    mov iButton,bx
    mov iX,cx
    mov iY,dx
    }
    *button = iButton;
    *x = iX;
    *y = iY;
    }
    [/code]

    But if I recall __asm is a VC++ keyword and VC dont let you call interrupt.

    Pat
  • tsagldtsagld Member Posts: 621
    : : I've been having problems trying to mix C++ variables with assembler registers. Below is an example of what I'm trying to do. At the moment, I don't care what the result is, I just want to know how to swap values from C++ to asm and vice-versa. In case it matters, I'm using MSVC++ 6.0.
    : :
    : : [code]void getmouse(int *button,int *x,int *y)
    : : {
    : : __asm {
    : : mov ax,3;
    : : int 0x33;
    : : *button=bx;
    : : *x=cx;
    : : *y=dx;
    : : }
    : : }[/code]
    : :
    : : I've looked at a whole bunch of asm tutorials and stuff like that, but still don't know what to do.
    : :
    :
    : Hello,
    :
    : The simplist way to do is to create temp var like.
    : [code]
    : void getmouse(int *button,int *x,int *y)
    : {
    : int iX,iY,iButton;
    : __asm {
    : mov ax,3;
    : int 0x33;
    : mov iButton,bx
    : mov iX,cx
    : mov iY,dx
    : }
    : *button = iButton;
    : *x = iX;
    : *y = iY;
    : }
    : [/code]
    :
    : But if I recall __asm is a VC++ keyword and VC dont let you call interrupt.
    :
    : Pat
    :
    Or this:

    [code]
    void getmouse(int *button,int *x,int *y)
    {
    int iX,iY,iButton;
    __asm {
    mov ax,3;
    int 0x33;
    mov si, button
    mov [si], bx
    mov si, x
    mov [si], cx
    mov si, x
    mov [si],dx
    }
    }
    [/code]

    But be aware: ax, dx etc are 16-bit registers. An int is 32 bits with MSVC++ 6.0. You may want to use movzx instead of mov, or a mov dword ptr.


    Greets,
    Eric Goldstein
    www.gvh-maatwerk.nl

  • blitzblitz Member Posts: 620
    [b][red]This message was edited by blitz at 2002-10-24 13:34:36[/red][/b][hr]
    : I've been having problems trying to mix C++ variables with assembler registers. Below is an example of what I'm trying to do. At the moment, I don't care what the result is, I just want to know how to swap values from C++ to asm and vice-versa. In case it matters, I'm using MSVC++ 6.0.
    :
    : [code]void getmouse(int *button,int *x,int *y)
    : {
    : __asm {
    : mov ax,3;
    : int 0x33;
    : *button=bx;
    : *x=cx;
    : *y=dx;
    : }
    : }[/code]
    :
    : I've looked at a whole bunch of asm tutorials and stuff like that, but still don't know what to do.
    :

    The code you posted will work under 16-bit DOS, not on the Win32 platform (targeted by VC++ 6.0). On Win32 you may use the API function GetCursorPos() to retrieve the mouse position...

    Anyway, here's an example that may be helpful:
    [code]
    void asm_sum(int x, int y, int *result)
    {
    __asm {
    mov eax, [x]
    mov edx, [result]
    add eax, [y]
    mov [edx], eax
    }
    }
    [/code]

    PS: You should preserve the values of EBX, ESI and EDI as they are usually used by the compiler's optimizer.


  • star_phoenixstar_phoenix Member Posts: 92
    : The code you posted will work under 16-bit DOS, not on the Win32 platform (targeted by VC++ 6.0). On Win32 you may use the API function GetCursorPos() to retrieve the mouse position...
    :
    : Anyway, here's an example that may be helpful:
    : [code]
    : void asm_sum(int x, int y, int *result)
    : {
    : __asm {
    : mov eax, [x]
    : mov edx, [result]
    : add eax, [y]
    : mov [edx], eax
    : }
    : }
    : [/code]
    :
    : PS: You should preserve the values of EBX, ESI and EDI as they are usually used by the compiler's optimizer.
    :
    :
    :

    I forgot to mention that I [italic]am[/italic] working with 16-bit DOS programs. I have yet to enter the world of 32-bit Windows. I am able to use Borland 5.01 - would that be a better option?

    Oh, I also tried compiling the code you posted with Borland, but Borland didn't know what 'eax' was.
  • star_phoenixstar_phoenix Member Posts: 92
    [b][red]This message was edited by star_phoenix at 2002-10-24 18:6:37[/red][/b][hr]
    :
    : Hello,
    :
    : The simplist way to do is to create temp var like.
    : [code]
    : void getmouse(int *button,int *x,int *y)
    : {
    : int iX,iY,iButton;
    : __asm {
    : mov ax,3;
    : int 0x33;
    : mov iButton,bx
    : mov iX,cx
    : mov iY,dx
    : }
    : *button = iButton;
    : *x = iX;
    : *y = iY;
    : }
    : [/code]
    :
    : But if I recall __asm is a VC++ keyword and VC dont let you call interrupt.
    :
    : Pat
    :

    I tried using your code in Borland 5.01. For the most part it worked, but I was only getting the y-value of the mouse coordinates and the button-presses. Do you happen to have any idea how I would get the x-coordinate?

    Now that I know how to get values from registers, I want to see if I can get this function to work.

    (Should I actually go to the asm messageboard for this question?)
  • star_phoenixstar_phoenix Member Posts: 92
    Never mind, I solved the x-coordinate problem by using references in the function, rather then pointers. I don't know why, but *b and *c ended up having the same value when I used pointers.

    This was my final code:

    [code]#include
    #include

    void getmouse(int &button, int &x, int &y);

    int main()
    {
    int a, b, c;
    while(!kbhit())
    {
    getmouse(a, b, c);
    cout << a << endl;
    cout << b << endl;
    cout << c << endl;
    clrscr();
    }
    getch();
    return 0;
    }

    void getmouse(int &button, int &x, int &y)
    {
    int iX,iY,iButton;
    asm {
    mov ax,3;
    int 0x33;
    mov iButton,bx
    mov iX,cx
    mov iY,dx
    }
    button = iButton;
    x = iX;
    y = iY;
    }[/code]

    Thanks for all of your help!
  • PatLDDDPatLDDD Member Posts: 30
    : : The code you posted will work under 16-bit DOS, not on the Win32 platform (targeted by VC++ 6.0). On Win32 you may use the API function GetCursorPos() to retrieve the mouse position...
    : :
    : : Anyway, here's an example that may be helpful:
    : : [code]
    : : void asm_sum(int x, int y, int *result)
    : : {
    : : __asm {
    : : mov eax, [x]
    : : mov edx, [result]
    : : add eax, [y]
    : : mov [edx], eax
    : : }
    : : }
    : : [/code]
    : :
    : : PS: You should preserve the values of EBX, ESI and EDI as they are usually used by the compiler's optimizer.
    : :
    : :
    : :
    :
    : I forgot to mention that I [italic]am[/italic] working with 16-bit DOS programs. I have yet to enter the world of 32-bit Windows. I am able to use Borland 5.01 - would that be a better option?
    :
    : Oh, I also tried compiling the code you posted with Borland, but Borland didn't know what 'eax' was.
    :

    Hello, 16 bit applications dont know 32 bit register.

    Pat
  • tsagldtsagld Member Posts: 621
    How do you create 16-bit DOS apps with MSVC++ 6.0 ?????

    : : The code you posted will work under 16-bit DOS, not on the Win32 platform (targeted by VC++ 6.0). On Win32 you may use the API function GetCursorPos() to retrieve the mouse position...
    : :
    : : Anyway, here's an example that may be helpful:
    : : [code]
    : : void asm_sum(int x, int y, int *result)
    : : {
    : : __asm {
    : : mov eax, [x]
    : : mov edx, [result]
    : : add eax, [y]
    : : mov [edx], eax
    : : }
    : : }
    : : [/code]
    : :
    : : PS: You should preserve the values of EBX, ESI and EDI as they are usually used by the compiler's optimizer.
    : :
    : :
    : :
    :
    : I forgot to mention that I [italic]am[/italic] working with 16-bit DOS programs. I have yet to enter the world of 32-bit Windows. I am able to use Borland 5.01 - would that be a better option?
    :
    : Oh, I also tried compiling the code you posted with Borland, but Borland didn't know what 'eax' was.
    :


    Greets,
    Eric Goldstein
    www.gvh-maatwerk.nl

  • blitzblitz Member Posts: 620
    :
    : I forgot to mention that I [italic]am[/italic] working with 16-bit DOS programs. I have yet to enter the world of 32-bit Windows. I am able to use Borland 5.01 - would that be a better option?
    :

    Yes, BCC 5.01 is able to produce [b]both[/b] 16-bit and 32-bit programs - that way you can continue to develop your 16-bit programs (to learn the assembly languange) and also "to enter the world of 32-bit Windows" :-)

    :
    : Oh, I also tried compiling the code you posted with Borland, but Borland didn't know what 'eax' was.
    :

    That's because you didn't select the '386 instruction set' from the 'Options' menu (Borland C++ 3.x)...

    PS: Here's an old function of mine that does what you asked for initially:
    [code]
    void mouse_data(pINT x, pINT y, pINT but) {
    asm {
    mov ax,3
    int 0x33
    les di,but
    mov es:[di],bx
    les di,x
    mov es:[di],cx
    les di,y
    mov es:[di],dx
    }}
    [/code]
    where pINT is defined as:
    [code]
    typedef int far *pINT;
    [/code]
    to make sure that it'll work on all memory models
  • star_phoenixstar_phoenix Member Posts: 92
    : How do you create 16-bit DOS apps with MSVC++ 6.0 ?????
    :

    Now that I think about it, I guess you can't... I've resorted to using Borland 5.1 for my 16-bit programming.
«1
Sign In or Register to comment.