Page 1 of 2

Copy PbConstant value in variable ASM [Resolved]

Posted: Fri May 25, 2012 9:01 am
by Kwai chang caine
Hello

If i have a constant

Code: Select all

#ConstantPb = 10
How can i read it in ASM for copy value in ASM Variable ???

Code: Select all

#ConstantPb = 10
! ConstantAsm = ????
Thanks

Re: Copy PbConstant value in variable ASM

Posted: Fri May 25, 2012 11:57 am
by bosker
Hi KCC

I don't know of any way to get a #Const value substituted into assembler.
However, if you can declare the PB constant in a macro you can get what I think you want with some macro magic. ;-)

Code: Select all

EnableExplicit

Macro ConstantPB      ; Use this instead of #ConstantPB = 9
9
EndMacro

Macro DeclareConstantASM(k)
! ConstantASM = k
EndMacro


Define.l i
OpenConsole()

For i = 1 To ConstantPB       ; Constant used in PB
    PrintN(Str(i))
Next

PrintN(Str(PeekA(?T1)))

DeclareConstantASM(ConstantPB)   ; Constant declared for asm

DataSection
T1:
!   db  ConstantASM
EndDataSection


Re: Copy PbConstant value in variable ASM

Posted: Fri May 25, 2012 12:32 pm
by Kwai chang caine
One thousand of thanks BOSKER 8)
A moment i was afraid i ask a question again impossible to do, or so ridiculous, that nobody can or want help me :oops:
Thanks to you i' have a little bit of hope :D
I don't know of any way to get a #Const value substituted into assembler.
In fact...it's simple...
huuuummm ..what that, all of my idea, is not really simple :oops:

The kind Freeze4Me give to me a splendid tips for create "Dynamical Datasection" 8)

Code: Select all

!KCC_DataSection = 10
!RepNum = 17

ShowMemoryViewer(?my_data, 500)

DataSection
  my_data:
  
  !repeat KCC_DataSection
    !repeat RepNum
      CompilerIf #PB_Compiler_Unicode
        !dw '*'
      CompilerElse
        !db '*'
      CompilerEndIf
    !end repeat
  !end repeat   
EndDataSection
So i'm verry happy with that, but i have another problem :(

The number 10 is not always the same.
I choose it a the beginning of the code like that:

Code: Select all

#SizeOfDataSection = 10
This PBconstant is using after in all my code.
But i don't know how i can pass this PBconstant into the ASM code for replace the "10" :(
Else i'm forced to use two ligne for declare my number 10 :(

Code: Select all

!KCC_DataSection = 10
#SizeOfDataSection = 10
One for PB and one for ASM, because i don't kno how i can copy PbConstant into ASM or ASMConstant into PbConstant :(

It's the reason why i ask how is possible to copy PbConstant in a Constant ASM :wink:

Re: Copy PbConstant value in variable ASM

Posted: Fri May 25, 2012 1:55 pm
by Kwai chang caine
Thanks to BOSKER, i move a little bit :D

I have my two constant Pb and ASM with the same value 8)
But if the top would if i have no datasection :oops:

Code: Select all

Macro DeclareConstantASM(k)
 #KccConstant = k
 ! ConstantASM = k
EndMacro

DeclareConstantASM(10)   ; Constant declared for asm

Debug "ConstanteASM = " + Str(PeekA(?T1))
Debug "ConstantePB = " + Str(#KccConstant)

DataSection
T1:
!   db  ConstantASM
EndDataSection

Re: Copy PbConstant value in variable ASM

Posted: Fri May 25, 2012 3:56 pm
by bosker
Hey KCC, I like your constant declaration macro better than my original. I can use that in my code, thank you.

Using your macro technique, you can do the stars thing like shown in the code below - I remember you wanted groups of 10 stars.
I added a demo procedure which gets the asm constant back into PB so you could (if you wanted) just declare it in asm.

Code: Select all

EnableExplicit

Macro DeclareConstant(k)
#ConstantPB = k
! ConstantASM = k
EndMacro

Procedure.l GetConstant()   ; just a demo - not needed to make it work
!   MOV EAX, ConstantASM
    ProcedureReturn
EndProcedure

DeclareConstant(9)       ; declare PB and ASM constants

Debug #ConstantPB
Debug GetConstant()     ; get the value of ConstantASM in PB
Debug PeekS(?T1)

DataSection
T1:
!   times ConstantASM db "**********"
Data.c  0       ; terminate the string
EndDataSection


Re: Copy PbConstant value in variable ASM

Posted: Fri May 25, 2012 5:00 pm
by Kwai chang caine
Hey KCC, I like your constant declaration macro better than my original. I can use that in my code, thank you.
Then here !!!!!!! When i have read this line, i'm so happy, then i believe it's the most nice day of my life :shock:
The ASM, it's for me, more again than a dream, already i dream to learn C, then imagine :oops:
So if a day i imagine helping even a little bit a AsmMan...it's really amazing for me :shock:

Image

You not imagine what this sentence make me proud....
In fact i understand often nothing what i do, but i search search and search again
It's what we call in french "the beginner chance" i don't know if that exist in english (La chance du debutant) :mrgreen:

And we have also another sentence by Michel audiard (French filmmaker), extract to a french movie "The uncles gunslingers" (Les tontons flingueurs) very very funny, who stick exactely at this situation :D

Image

Like i don't understand anything, but try for hours and hours, all what i think in my little head.
I remember this sentence who say "The idiots this dare all, this is why we even recognize them" :lol: :lol: (Les cons ça ose tout, c'est même à ça qu'on les reconnait)
And idiot..it's my second name :lol: :lol: :lol:
Image

Sure i search more a code like this

Code: Select all

#ConstantPB = 10

Macro DeclareConstant(k)
 !ConstantASM = k
EndMacro

DeclareConstant(#ConstantPB)
But it'simpossible to send the PbConstant directly :(
So i have reduce at the maximum, and for the moment i think there are no better way

Code: Select all

Macro DeclareConstant(k)
 #ConstantPB = k
 !ConstantASM = k
EndMacro

DeclareConstant(9)       ; declare PB and ASM constants
And super your procedure GetConstant() i go keep it, for use it later, in case i need read a ASMConstant

After this moment, i believe i go to sleep this night, with the mouth open, and the teeth on the air :mrgreen:
Image

I wish you a very very very good day and week end !!! 8)

Re: Copy PbConstant value in variable ASM

Posted: Fri May 25, 2012 5:39 pm
by bosker
KCC, thank you for your unique response - it made me smile - and that's a good thing. :-)

As a "Friday special bonus", I thought of a way to make the DeclareConstant general, so you could declare lots of constants for use
in PB and Asm. (if you need to do that sort of thing)

Code: Select all

Macro mHash
#
EndMacro

Macro DeclareConstant(Name, Value)
mHash#Name#PB = Value
! Name#ASM = Value
EndMacro

DeclareConstant(NewConst, 7)    ; declare #NewConstPB and NewConstASM with value 7
DeclareConstant(OldConst, 99)    ; declare #OldConstPB and OldConstASM with value 99
Have fun.

Re: Copy PbConstant value in variable ASM [Resolved]

Posted: Fri May 25, 2012 7:08 pm
by netmaestro
You're not using EnableASM, which makes it simple:

Code: Select all

#myconst = 10

Procedure Test(*Pointer, Variable)
  Protected this.l
  EnableASM
     MOV dword [p.v_this], #myconst
     MOV dword [p.p_Pointer], 20
     MOV dword [p.v_Variable], 30
  DisableASM
  Debug this
  Debug *Pointer
  Debug Variable
EndProcedure
  
  Test(0, 0)

Re: Copy PbConstant value in variable ASM [Resolved]

Posted: Fri May 25, 2012 8:35 pm
by bosker
Hi netmaestro

Thanks for this - I rarely use EnableASM and I didn't know that #Const or that the [p.p_var] form worked in that case.
I see the assembler statements can also be written as..

Code: Select all

  EnableASM
     MOV this, #myconst
     MOV *Pointer, 20            ; didn't know this worked either until I just tried it
     MOV Variable, 30
  DisableASM
.. which is also interesting. Thanks for the enlightenment - if I can learn something new each day I'm a happy chappy.

Incidentally NM, I'm very happy to see you here. I spotted you hanging out on another language site and was concerned
you had left. Your knowledge and contribution here would be greatly missed. 'nuff said.

Re: Copy PbConstant value in variable ASM [Resolved]

Posted: Sat May 26, 2012 8:18 am
by Kwai chang caine
@Bosker
KCC, thank you for your unique response - it made me smile - and that's a good thing.
I'm happy...in fact it's do for that. :lol:
Give a little bit of humor (And i have to much of this :oops: ), in exchange of knowledge (That i have not :oops:) , in this world not always funny :wink:

See here...i begin to learn a little bit :mrgreen:
Thanks to your nice procedure GetConstant (I say to you i use later... :wink:) i can see the resultss of your new code 8)

Code: Select all

Macro mHash
 #
EndMacro

Macro DeclareConstant(Name, Value)
 mHash#Name#PB = Value
 ! Name#ASM = Value
EndMacro

DeclareConstant(NewConst, 7)    ; declare #NewConstPB and NewConstASM with value 7
DeclareConstant(OldConst, 99)    ; declare #OldConstPB and OldConstASM with value 99

; Procedure just for see the result :-)
Procedure.l GetConstant(ConstantToGet)
 
 Select ConstantToGet
  Case #NewConstpb
   !MOV EAX, NewConstASM
  Case #OldConstpb
   !MOV EAX, OldConstASM
 EndSelect
 
 ProcedureReturn
 
EndProcedure

Debug "DeclareConstant(NewConst, 7)"
Debug "ConstantePB = " + Str(#NewConstpb)
Debug "ConstanteASM = " + Str(GetConstant(#NewConstpb))
Debug ""
Debug "DeclareConstant(OldConst, 99)"
Debug "ConstantePB = " + Str(#OldConstpb)
Debug "ConstanteASM = " + Str(GetConstant(#OldConstpb))
It's a pity, it's not really possible to passing variable of PB to ASM or reverse :(
It's very cool if i can write the parameter of a procedure and send to the ASM like we send a parameter to a PB line, but that not works obviously
Like this who not works :( :

Code: Select all

Procedure.l GetConstant(NewConstASM)
  !MOV EAX, NewConstASM
  ProcedureReturn
EndProcedure
So i'm forced to use this for have two different results of two ASMConstant with only one procedure :( :

Code: Select all

Procedure.l GetConstant(ConstantToGet)
 
 Select ConstantToGet
  Case #NewConstpb
   !MOV EAX, NewConstASM
  Case #OldConstpb
   !MOV EAX, OldConstASM
 EndSelect
 
 ProcedureReturn
 
EndProcedure
@Netmaestro
Thanks for your help. 8)
Your code is surely much powerfull for me :lol:
But i not understand how you do for give the same value to a PBConstant and a AsmConstant with the minimum of lines ????

Re: Copy PbConstant value in variable ASM [Resolved]

Posted: Sat May 26, 2012 9:40 am
by Fred
EnableASM is really the way to go if you want to mix PB code and ASM

Re: Copy PbConstant value in variable ASM [Resolved]

Posted: Sat May 26, 2012 9:11 pm
by Kwai chang caine
Hello FRED happy to talk to you 8)

In fact my "only" problem, is to donate the most simple possible, the same value at a PBConstant and a ASMConstant.
Or give to a PbConstant a value and copy this value in the AsmConstant in the same time
Or the reverse, give to a AsmConstant a value and copy this value in the PbConstant (This is not my favorite way)

The problem is when i give a value to a PbConstant

Code: Select all

#PbConstant = 10
The code Asm dont want my value :(

Code: Select all

!AsmConstant = #PbConstant
It's prohibited :(

Or the reverse

Code: Select all

!AsmConstant = 10
The Pb code dont want the Asm value :(

Code: Select all

#PbConstant = !AsmConstant
It's prohibited too :(

In fact my favorite choice is give a value at a PbConstant

Code: Select all

#PbConstant = 10
And find a way to give this same value at my AsmConstant automaticaly without write !AsmConstant = 10 :D

If you find a way....i promise no put a new question for a great time... 8)
At least ...........two day...is it enough ?? :oops: :lol:

So thanks for your interest 8)

Re: Copy PbConstant value in variable ASM [Resolved]

Posted: Sat May 26, 2012 11:42 pm
by Foz
I fail to see the problem:

Code: Select all

#NewConst = 7
#OldConst = 99

Procedure.l GetConstant(ConstantToGet)
  
  Select ConstantToGet
    Case #NewConst
      EnableASM
      MOV EAX, #NewConst
      DisableASM
    Case #OldConst
      EnableASM
      MOV EAX, #OldConst
      DisableASM
  EndSelect
  
  ProcedureReturn
  
EndProcedure

Debug "ConstantePB = " + Str(#NewConst)
Debug "ConstanteASM = " + Str(GetConstant(#NewConst))
Debug ""
Debug "ConstantePB = " + Str(#OldConst)
Debug "ConstanteASM = " + Str(GetConstant(#OldConst))

Re: Copy PbConstant value in variable ASM [Resolved]

Posted: Sun May 27, 2012 12:23 am
by bosker
The problem is that KCC doesn't want to write assembler executable statements - he wants to write assembler data statements and EnableASM doesn't help in that case.
The original problem was to find a way to use a constant at compile time to create a string of stars (*) whose length depends on the constant. Somebody (breeze4me) showed a way to do that using assembler constant and db statements because there doesn't seem to be a way to do it directly in PB. KCC also wants to use the constant in PB so declaring it as a assembler constant only isn't enough.

So...
That's where this thread came in. All the bits of assembler code that have been flying about are only really for testing the data declarations and other messing about. The real problem can be seen in this code (which appeared in an earlier post).

Code: Select all

EnableExplicit

Macro DeclareConstant(k)
#ConstantPB = k
! ConstantASM = k
EndMacro

DeclareConstant(9)       ; declare PB and ASM constants

Debug #ConstantPB
Debug PeekS(?T1)

DataSection
T1:
!   times ConstantASM db "**********"
Data.c  0       ; terminate the string
EndDataSection
The line after T1: is the key problem. Writing it without the ! doesn't compile and using the ! means #const doesn't work either. Did that all make sense?

Re: Copy PbConstant value in variable ASM [Resolved]

Posted: Sun May 27, 2012 8:13 am
by Foz
This is hurting my head thinking about it. Let's see if my definition is correct:

Constants are meaningful labels for fixed values that are automatically replaced during compile time.

Okay, on that pretense, and as my understanding permits, the challenge is for PureBasic and ASM to understand the same constants.

As PureBasic is the higher level, the we have to work with the PureBasic constants. Which when wrapping the statements in EnableASM/DisableASM blocks, it works.

Somewhere along the way there was a separation of the constants into PureBasic and ASM.

At what point do you need different compile time constants? If you need something that will only be set at the FASM compile time and not PB compile time, what is the point of wanting to use said constant in PB?

This is where I fail to see the problem...