Page 1 of 1

Restore from a variable.

Posted: Mon Nov 26, 2018 5:20 am
by jassing
Is there a trick I can use to restore a label passed as a variable?

ie:

Code: Select all

procedure test( myLabel )
  restore (myLabel)
 ...
endprocedure

test( ?dat1 )
datasection
  dat1:
  data.b 1,2,3,4
enddatasection

Re: Restore from a variable.

Posted: Mon Nov 26, 2018 5:47 am
by citystate
macros might do the trick

Code: Select all

Macro test(label)
  Restore label
EndMacro

test(dat1)
For i=1 To 4:Read.b a.b:Debug a:Next
test(dat2)
For i=1 To 4:Read.b a.b:Debug a:Next

DataSection
  dat2:
  Data.b 5,6,7,8
  dat1:
  Data.b 1,2,3,4
EndDataSection

Re: Restore from a variable.

Posted: Mon Nov 26, 2018 7:49 am
by Demivec
jassing wrote:Is there a trick I can use to restore a label passed as a variable?
Here's a link to a thread topic that addresses your need, 'restore label as a parameter to a procedure'.

I'll repost the resulting code from that thread here also for redundancy:

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 code was produced by srod with additional contributions by luis and myself.

A similar trick could be used to make note of the current value of the PB_DataPointer so that it could be restored later after an intervening read from a different data position might occur (i.e. from another thread and using a mutex) before restoring the pointer to its former position.

@Edit: added remdundant posting of complete code solution

Re: Restore from a variable.

Posted: Mon Nov 26, 2018 8:36 am
by jassing
Perfect!!!

Thank you.