Page 1 of 1

Procedure, Shared vars, inline asm headaches!

Posted: Sat Oct 01, 2005 4:31 am
by Rescator
Related to: viewtopic.php?p=105095

Code: Select all

Procedure test(a)
!NEG [v_a]
ProcedureReturn a
EndProcedure

Debug test(1)
This fails as the variable is not global.

Code: Select all

Procedure test(a)
Shared a
!NEG [v_a]
ProcedureReturn a
EndProcedure

Debug test(1)
This works, but Shared effectively resets the procedure argument?

Anyone know how to use non global vars inside a procedure with inline asm?
It would make coding so much easier if inline asm didn't need the vars to be global.

EDIT: Yes I know about fetching the arguments from the stack etc. What I'm wondering is, would it be possible to not to that, it may be slower but a bit more practical.
EDIT: I also thought procedure arguments allready was global?

Posted: Sat Oct 01, 2005 8:18 am
by Psychophanta
Rescator; inside a procedure you can't manage it's local variables or arguments for ASM as if it was a normal global variable. Instead you must refer to ESP pointed addresses (ESP = Extended Stack Pointer).
That's because functions store variables in the address pointed by ESP register.

On the other hand, Protected, Static and Shared commands have problems if used with arguments defined in the Procedure. So then, until it is fixed (perhaps the argument variables is defined automatically as Protected), you must not use it like that. Look at viewtopic.php?t=16830 for more details.

Posted: Tue Oct 04, 2005 8:09 pm
by Trond
On the other hand, we have different fingers.

Code: Select all

Procedure test(a) 
  !v_a equ ESP
  !NEG dword [v_a]
  ProcedureReturn a
EndProcedure 
Debug test(1)

Posted: Tue Oct 04, 2005 9:00 pm
by Psychophanta
Trond wrote:On the other hand, we have different fingers.
I see, and in the main hand you have the main finger, don't :?: :lol:

Posted: Wed Oct 05, 2005 6:11 am
by Rescator
Ah thank you so much Trond, so that was the directive to create local asm variables.
Psychophanta totaly missed my actual question and copy pasted his response to the closely related bug/issue instead :lol:

Trond, any idea how to mix PB and ASM variables inside a procedure?
Thanks to you know I'm able to do simple asm procedures and make local (? or am I making global?) asm variables.

But how would one handle the following procedure layout?

start of procedure
PB code with local vars
ASM routine/loop with local or shared vars with PB code.
PB code with local vars
end of procedure

Would the procedure arguments have to be used to carry variable data to/from the PB and ASM code?
Or is there a easier way?

And what about cases where the procedure has no arguments at all?
How do one pass values between the PB and ASM code then?

Posted: Wed Oct 05, 2005 8:01 am
by Trond
Rescator wrote:Ah thank you so much Trond, so that was the directive to create local asm variables.
Psychophanta totaly missed my actual question and copy pasted his response to the closely related bug/issue instead :lol:

Trond, any idea how to mix PB and ASM variables inside a procedure?
Thanks to you know I'm able to do simple asm procedures and make local (? or am I making global?) asm variables.
They are neither global nor local. The definition (!v_a equ edx) will override any existing definition of v_a. The definition is global. Using it in another procedure later will redefine it again. The variable, however, should be cleaned up at procedure return and is local. Here's an example of the scope:

Code: Select all

Global a ;defines v_a
a = 4
Debug a ;outputs 4
Procedure first()
  Debug a
EndProcedure
first() ;outputs 4
Procedure second(a.l)
  !v_a equ esp
  a = 8
EndProcedure
Debug a ;output is the undefined contents pointed to by esp
first() ;output is the undefined contents pointed to by esp
So be careful.
Rescator wrote:start of procedure
PB code with local vars
ASM routine/loop with local or shared vars with PB code.
PB code with local vars
end of procedure
I'm not sure if I understand what you mean, but something like this?

Code: Select all

Procedure Banana(a.l, b.l)
  ;PB code with local variables
    a = a*2
    b = a+b
  ;ASM routine with local variables
    !lv_a equ esp ;lv prefix = Local Variable
    !lv_b equ esp+4
    !neg dword [lv_a]
    !neg dword [lb_b]
  ;PB code with local variables
    a = a*2
    b = a+b
EndProcedure

Posted: Fri Oct 07, 2005 4:04 am
by Rescator
Almost, at least I'm learning new stuff abut PB, ASM and procedures :)

The following is what I ment, obviously this does not work!

Code: Select all

Procedure Banana(a.l, b.l)
  ;PB code with local variables
    c = a*2
    d = a+b
  ;ASM routine with local variables
    !lv_c equ esp ;lv prefix = Local Variable
    !lv_d equ esp+4
    !NEG dword [lv_c]
    !NEG dword [lv_d]
  ;PB code with local variables
  e=c+d
  ProcedureReturn e
EndProcedure

Debug banana(1,2)
Do you understand what I mean?
On several occations I've tried to mix ASM and PB code inside procedures this way,
but never gotten it to work obviously, my skills in ASM is somewhat limited :)

Posted: Fri Oct 07, 2005 9:22 am
by Trond
The reason it does not work is because the newly declared variables c and d has moved the stack pointer. This should work:

Code: Select all

Procedure Banana(a.l, b.l) 
  ;PB code with local variables 
    c = a*2 ;c=2
    d = a+b ;d=2+2
  ;ASM routine with local variables 
    !lv_c equ esp+8 ;lv prefix = Local Variable 
    !lv_d equ esp+12 
    !NEG dword [lv_c] 
    !NEG dword [lv_d]
  ;PB code with local variables 
  e=c+d 
  ProcedureReturn e 
EndProcedure 

Debug banana(1,2)

Posted: Mon Oct 10, 2005 12:21 pm
by Rescator
Aaah! :idea: brilliant, I get it now! Thanks a lot Trond, I'm proly gonna update a few of my procedures (with has loops) now with ASM code. :)