Move Register Value To Structure Pointer Argument

Bare metal programming in PureBasic, for experienced users
User avatar
grabiller
Enthusiast
Enthusiast
Posts: 309
Joined: Wed Jun 01, 2011 9:38 am
Location: France - 89220 Rogny-Les-Septs-Ecluses
Contact:

Move Register Value To Structure Pointer Argument

Post by grabiller »

Hello,

I'm trying to set the values of eax/edx to a structure pointer recieved as a procedure argument but it does not work directly in my case. As I'm very newbie in assembler, I must be doing something wrong.

I have a code like this:

Code: Select all

Procedure GetPosition( *out.sf::Vector2i, *window.sf::Window )
  sfWindow_getPosition( *window )
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
    !mov [p.p_out],eax
    !mov [p.p_out+4],edx
  CompilerElse
    ; TODO
  CompilerEndIf
EndProcedure
and it does not work.

sf::Vector2i is a simple structure defined in a Module:

Code: Select all

Structure Vector2i
  x.l
  y.l
EndStructure
I have a variant working, by using a local variable:

Code: Select all

Procedure GetPosition( *out.sf::Vector2i, *window.sf::Window )
  Protected vout.sf::Vector2i
  sfWindow_getPosition( *window )
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
    !mov [p.v_vout],eax
    !mov [p.v_vout+4],edx
    *out\x = vout\x
    *out\y = vout\y
  CompilerElse
    ; TODO
  CompilerEndIf
EndProcedure
This code above works correctly.

What should I do to set the value directly to the structure pointer argument, without the need to use a local variable ?
guy rabiller | radfac founder / ceo | raafal.org
User avatar
grabiller
Enthusiast
Enthusiast
Posts: 309
Joined: Wed Jun 01, 2011 9:38 am
Location: France - 89220 Rogny-Les-Septs-Ecluses
Contact:

Re: Move Register Value To Structure Pointer Argument

Post by grabiller »

I have it working for x64 (but still with a local variable).

Feel free to tell me if I'm doing something wrong:

Code: Select all

Procedure GetPosition( *out.sf::Vector2i, *window.sf::Window )
  sfWindow_getPosition( *window )
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
    Protected vout.sf::Vector2i
    !mov [p.v_vout],eax
    !mov [p.v_vout+4],edx
    *out\x = vout\x
    *out\y = vout\y
  CompilerElse
    Protected vout.q
    !mov [p.v_vout],rax
    *out\x = vout & $FFFF
    *out\y = vout >> 32
  CompilerEndIf
EndProcedure
It works on Windows, but can I assume it will work on Linux & MacOSX ?
guy rabiller | radfac founder / ceo | raafal.org
User avatar
grabiller
Enthusiast
Enthusiast
Posts: 309
Joined: Wed Jun 01, 2011 9:38 am
Location: France - 89220 Rogny-Les-Septs-Ecluses
Contact:

Re: Move Register Value To Structure Pointer Argument

Post by grabiller »

Nevermind, I got it to set the structure pointer (almost) directly (on x86):

Code: Select all

Procedure GetPosition( *out.sf::Vector2i, *window.sf::Window )
  sfWindow_getPosition( *window )
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
      !mov ecx,[p.p_out]
      !mov [ecx],eax
      !mov [ecx+4],edx
  CompilerElse
    Protected vout.q
    !mov [p.v_vout],rax
    *out\x = vout & $FFFF
    *out\y = vout >> 32
  CompilerEndIf
EndProcedure
guy rabiller | radfac founder / ceo | raafal.org
User avatar
grabiller
Enthusiast
Enthusiast
Posts: 309
Joined: Wed Jun 01, 2011 9:38 am
Location: France - 89220 Rogny-Les-Septs-Ecluses
Contact:

Re: Move Register Value To Structure Pointer Argument

Post by grabiller »

I also got it for x64:

Code: Select all

Procedure GetPosition( *out.sf::Vector2i, *window.sf::Window )
  sfWindow_getPosition( *window )
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
      !mov ecx,[p.p_out]
      !mov [ecx],eax
      !mov [ecx+4],edx
  CompilerElse
      !mov rdx,[p.p_out]
      !mov [rdx],rax
  CompilerEndIf
EndProcedure
Sorry for noise :?

Anyway, this may help other ASM newbies like me at some point.

Cheers,
Guy.
guy rabiller | radfac founder / ceo | raafal.org
User avatar
Tenaja
Addict
Addict
Posts: 1949
Joined: Tue Nov 09, 2010 10:15 pm

Re: Move Register Value To Structure Pointer Argument

Post by Tenaja »

I saved this in case I ever need to reference it; thanks for sharing.

If you are trying to do it in asm, I'd guess it is for speed...if so, why not make it a macro?
User avatar
grabiller
Enthusiast
Enthusiast
Posts: 309
Joined: Wed Jun 01, 2011 9:38 am
Location: France - 89220 Rogny-Les-Septs-Ecluses
Contact:

Re: Move Register Value To Structure Pointer Argument

Post by grabiller »

I'm using ASM because there is no other way to do it :wink:

And yes, I should use a Macro, would be cleaner :D
guy rabiller | radfac founder / ceo | raafal.org
Post Reply