restore label as a parameter to a procedure?

Just starting out? Need help? Post your questions and find answers here.
sys64802
Enthusiast
Enthusiast
Posts: 105
Joined: Sat Sep 12, 2015 6:55 pm

Re: restore label as a parameter to a procedure?

Post by sys64802 »

@Ramses800

As explained above, better to use this variant:

Code: Select all

Procedure RestoreEx (*address) 
 CompilerIf (#PB_Compiler_Processor = #PB_Processor_x86)
  !mov eax, [p.p_address]
  !mov [PB_DataPointer], eax
  CompilerElse   
  !mov rax, [p.p_address]
  !mov [PB_DataPointer], rax  
 CompilerEndIf
EndProcedure
The other one using a fixed esp offset is just wrong, or at least not prudent.
But really, no, is wrong.
Ramses800
User
User
Posts: 27
Joined: Wed Nov 05, 2014 3:12 pm
Location: Sweden

Re: restore label as a parameter to a procedure?

Post by Ramses800 »

Good point, sys64802! Thanks for the input!
Former VB6 developer adventuring in a brave, new Purebasic world!
User avatar
Demivec
Addict
Addict
Posts: 4086
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: restore label as a parameter to a procedure?

Post by Demivec »

Here is an update to the previous code that sets the DataSection read pointer. This one also returns the previous pointer's value.

Code: Select all

Procedure RestoreEx (*address) 
  ;sets DataSection read pointer to *address and returns previous DataSection read pointer 
  CompilerIf (#PB_Compiler_Processor = #PB_Processor_x86)
    !mov ecx, [PB_DataPointer]
    !mov eax, [p.p_address]
    !mov [PB_DataPointer], eax
    !mov eax, ecx
  CompilerElse   
    !mov rcx, [PB_DataPointer]
    !mov rax, [p.p_address]
    !mov [PB_DataPointer], rax  
    !mov rax, rcx
  CompilerEndIf
  
  ProcedureReturn 
EndProcedure

CompilerIf #PB_Compiler_IsMainFile
  ;demo
  DataSection
    Data.i 1, 2, 3, 4, 5, 6, 7
    obj:
    Data.i 1001, 2002, 3003, 4004, 5005, 6006, 7007
  EndDataSection
    
  Define var, i, oldReadPtr
  For i = 1 To 3
    Read.i var
    Debug var
  Next
  
  oldReadPtr = RestoreEx(?obj)
  Debug "oldPtr =" + oldReadPtr
  For i = 1 To 3
    Read.i var
    Debug var
  Next
  
  oldReadPtr = RestoreEx(oldReadPtr)
  Debug "oldPtr =" + oldReadPtr
  
  For i = 1 To 3
    Read.i var
    Debug var
  Next
CompilerEndIf
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: restore label as a parameter to a procedure?

Post by skywalk »

Ok, this ASM worked great but now is time to try PB6 C backend.
How to read a DataSection within a procedure parameter containing the data label?

Code: Select all

Procedure RestoreX(DataLabel.i)
  CompilerSelect #PB_Compiler_Processor
  CompilerCase #PB_Processor_x64
    !MOV rax, [p.v_DataLabel]   ; instead of -> !MOV rax, [rsp+64]
    !MOV [PB_DataPointer], rax
  CompilerCase #PB_Processor_x86
    !MOV eax, [p.v_DataLabel]   ; instead of -> !MOV eax, [esp+8]
    !MOV [PB_DataPointer], eax
  CompilerDefault
      MessageRequester("RestoreX", "Unknown Processor.", #MB_ICONERROR)
  CompilerEndSelect
EndProcedure
; Example use within a procedure.
Procedure Do1()
  RestoreX(?ds_MyDatasectionStuff)
  Read.i i
EndProcedure
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
BarryG
Addict
Addict
Posts: 3293
Joined: Thu Apr 18, 2019 8:17 am

Re:

Post by BarryG »

kinglestat wrote: Tue Dec 16, 2008 3:18 pmwas more worried about the steaks
https://youtu.be/TY8O0dShFoo?t=22
User avatar
idle
Always Here
Always Here
Posts: 5049
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: restore label as a parameter to a procedure?

Post by idle »

Code: Select all

Procedure RestoreX(DataLabel.i)
  CompilerIf #PB_Compiler_Backend = #PB_Backend_Asm
    
    CompilerSelect #PB_Compiler_Processor
      CompilerCase #PB_Processor_x64
        !MOV rax, [p.v_DataLabel]   ; instead of -> !MOV rax, [rsp+64]
        !MOV [PB_DataPointer], rax
      CompilerCase #PB_Processor_x86
        !MOV eax, [p.v_DataLabel]   ; instead of -> !MOV eax, [esp+8]
        !MOV [PB_DataPointer], eax
      CompilerDefault
        MessageRequester("RestoreX", "Unknown Processor.", #MB_ICONERROR)
    CompilerEndSelect 
  CompilerElse 
    !*pb_datapointer = v_datalabel;
  CompilerEndIf 
  
EndProcedure
; Example use within a procedure.
Procedure Do1()
  RestoreX(?ds_MyDatasectionStuff)
  Read.i i
  Read.i i
  Debug i 
  
EndProcedure

DataSection 
  ds_MyDatasectionStuff:
  Data.i 12345,23456,34567 
EndDataSection   

Do1()
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: restore label as a parameter to a procedure?

Post by skywalk »

Thanks idle!
Very close, but the 1st element read back from the datasection is corrupted.

Code: Select all

Procedure RestoreX(datalabel.i)
  CompilerIf #PB_Compiler_Backend = #PB_Backend_Asm
    CompilerSelect #PB_Compiler_Processor
    CompilerCase #PB_Processor_x64
      !MOV rax, [p.v_datalabel]   ; instead of -> !MOV rax, [rsp+64]
      !MOV [PB_DataPointer], rax
    CompilerCase #PB_Processor_x86
      !MOV eax, [p.v_datalabel]   ; instead of -> !MOV eax, [esp+8]
      !MOV [PB_DataPointer], eax
    CompilerDefault
      MessageRequester("RestoreX", "Unknown Processor.", #MB_ICONERROR)
    CompilerEndSelect
  CompilerElse
    !*pb_datapointer = v_datalabel;
  CompilerEndIf
EndProcedure
Procedure Do1(datalabel.i)
  Protected.i i,d
  RestoreX(datalabel)
  For i = 1 To 4
    Read.i d
    Debug Str(i) + "," + Str(d)
  Next i
EndProcedure
DataSection
  ds_Stuff:
  Data.i -1, 2, 3, 4
  ds_Stuff2:
  Data.i -2, 20,30,40
EndDataSection
Debug "ds_Stuff1"
Do1(?ds_Stuff)
Debug "ds_Stuff2"
Do1(?ds_Stuff2)
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: restore label as a parameter to a procedure?

Post by mk-soft »

Without '*'

@skywalk: Please set your Bug-Report to [NoBug/DONE]

Code: Select all

Procedure RestoreX(datalabel.i)
  CompilerIf #PB_Compiler_Backend = #PB_Backend_Asm
    CompilerSelect #PB_Compiler_Processor
    CompilerCase #PB_Processor_x64
      !MOV rax, [p.v_datalabel]   ; instead of -> !MOV rax, [rsp+64]
      !MOV [PB_DataPointer], rax
    CompilerCase #PB_Processor_x86
      !MOV eax, [p.v_datalabel]   ; instead of -> !MOV eax, [esp+8]
      !MOV [PB_DataPointer], eax
    CompilerDefault
      CompilerError "RestoreX: Unknown Processor."
    CompilerEndSelect
  CompilerElse
    !pb_datapointer = v_datalabel;
  CompilerEndIf
EndProcedure

Procedure Do1(datalabel.i)
  Protected.i i,d
  RestoreX(datalabel)
  For i = 1 To 4
    Read.i d
    Debug Str(i) + "," + Str(d)
  Next i
EndProcedure

DataSection
  ds_Stuff:
  Data.i -1, 2, 3, 4
  ds_Stuff2:
  Data.i -2, 20,30,40
EndDataSection
Debug "ds_Stuff1"
Do1(?ds_Stuff)
Debug "ds_Stuff2"
Do1(?ds_Stuff2)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: restore label as a parameter to a procedure?

Post by skywalk »

Thanks!
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: restore label as a parameter to a procedure?

Post by skywalk »

Doh! Dropped my bug report too soon. :shock:
Small code works, but not in larger library. How to debug this?
pbcompilerc wrote:21617 lines processed.
Error: Assembler
error: 'pb_datapointer' undeclared (first use in this function); did you mean 'pb_mapitem'?
pb_datapointer = v_datalabel;
^~~~~~~~~~~~~~
pb_mapitem
purebasic.c:14666:1: note: each undeclared identifier is reported only once for each function it appears in

Code: Select all

static integer f_restorex(integer v_datalabel); //<-- Line 989
// Procedure RestoreX(datalabel.i)
static integer f_restorex(integer v_datalabel) {
integer r=0;
// 
// CompilerIf #PB_Compiler_Backend = #PB_Backend_Asm
// !pb_datapointer = v_datalabel;
pb_datapointer = v_datalabel; //<-- Line 14666
// CompilerEndIf
// EndProcedure
r=0;
end:
return r;
}
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
idle
Always Here
Always Here
Posts: 5049
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: restore label as a parameter to a procedure?

Post by idle »

Have you dumped the c maybe it's renamed.
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: restore label as a parameter to a procedure?

Post by skywalk »

Yes, but dumping C is with pbcompilerc.exe on command line.
I do not know where the IDE compilation puts the amalgamated C file?
The line numbers are slightly different from IDE compile to command line compile because I cannot load resource and images from command line.
Inspecting the command line C file, it did not rename the variable.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: restore label as a parameter to a procedure?

Post by kenmo »

I know this is an old topic, but I must ask... Why all the effort to use inlined ASM/C to manipulate the PB internals? :?:

Once you're beyond the basic usage of Restore and Read, why not just use pointers and Peek?

It just seems strange to use ASM/C manipulation when you can accomplish it with regular PB code and maybe a few helper procedures.
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: restore label as a parameter to a procedure?

Post by skywalk »

Quite the opposite.
2 lines for ASM and 1 line for C backend, and we can use native Read.x within Procedures.
Of course, I would use the pointer approaches if these simpler alternatives were not available.

The same can be said of all PB use.
I use SendMessage() for some gui affects.
I use PB native and custom SQLite api commands.
Whatever gets the job done reliably. :wink:
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Post Reply