Page 1 of 1

Quickest way to input up to 10 strings into a structure ?

Posted: Wed Jun 05, 2013 11:36 am
by Primoz128
Well i am gona use a structure which is gona have a map(1 key) and a list with up to 10 elements.

But now i don't know what would be the quickest way to input up to 10 elements into the list... trough procedure it would require a lot of arguments... that isn't viable if you ask me.

Maybe "Data" ? Though i got no idea how to use it...

Re: Quickest way to input up to 10 strings into a structure

Posted: Wed Jun 05, 2013 12:14 pm
by spikey

Re: Quickest way to input up to 10 strings into a structure

Posted: Wed Jun 05, 2013 2:43 pm
by davido
Hi spikey:

Nice example.
I read the manual a lot; must have missed this one.

Re: Quickest way to input up to 10 strings into a structure

Posted: Wed Jun 05, 2013 3:34 pm
by TI-994A
Primoz128 wrote:...what would be the quickest way to input up to 10 elements into the list... trough procedure it would require a lot of arguments... Maybe "Data" ?
Hello Primoz128. Please take a look at these examples. The first one populates a LinkedList with elements read from a data block directly, while the second encapsulates the process in a reusable procedure:

Code: Select all

NewList scientists.s()

Restore scientists
For readData = 1 To 8
  Read.s name$
  AddElement(scientists())
  scientists() = name$
Next readData

ResetList(scientists())
While NextElement(scientists())
  Debug scientists()
Wend

DataSection
  scientists:    
  Data.s "Albert Einstein", "Isaac Newton", "Charles Darwin", "Guglielmo Marconi"
  Data.s "Galileo Galilei", "Leonardo da Vinci", "René Descartes", "Thomas Edison" 
EndDataSection

Code: Select all

Procedure asmRestore(address)
  !MOV EAX, [ESP+PS0]
  !MOV [PB_DataPointer],EAX
EndProcedure

Procedure fillList(List emptyList.s(), dataLabel.i, elements.i)
  asmRestore(dataLabel)
  For readData = 1 To elements
    Read.s name$
    AddElement(emptyList())
    emptyList() = name$
  Next readData
EndProcedure

NewList scientists.s()
fillList(scientists(), ?scientists, 8)

ResetList(scientists())
While NextElement(scientists())
  Debug scientists()
Wend

DataSection
  scientists:    
  Data.s "Albert Einstein", "Isaac Newton", "Charles Darwin", "Guglielmo Marconi"
  Data.s "Galileo Galilei", "Leonardo da Vinci", "René Descartes", "Thomas Edison" 
EndDataSection
We have to thank srod for the asmRestore() routine that facilitates the restoring of data blocks through label addresses. Hope it helps.

Re: Quickest way to input up to 10 strings into a structure

Posted: Wed Jun 05, 2013 4:07 pm
by luis
TI-99, that asmRestore is wrong for a general use because uses PS0.
That's a constant defined in the ASM code generated by PB, it's "PS" + a number and differs for every procedures. So PS0 is valid only if asmRestore it's the first procedure defined.
Moreover its value is dependent by the number and type of parameters of the procedure, so if it works when it's not the first procedure it's just by chance (the PS0 constant of another procedure has by chance the right value to access the right param in the asmRestore proc).

You can use this one instead:

Code: Select all

Procedure RestoreEx (*address) 
; [DESC]
; Restore the data pointer using a variable instead of a label.
; 
; [INPUT]
; *address : A pointer inside the data section.
;
; [RETURN]
; NONE
; 
; [NOTES]
; Example:
;
; DataSection
;  Data.i 10, 20, 30
; label_test:
;  Data.i 40, 50, 60
;  Data.i 70, 80, 90
; EndDataSection
; 
; *ptr = ?label_test
; RestoreEx (*ptr) 
; Read.i a
; Debug a ; 40
 
 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

Re: Quickest way to input up to 10 strings into a structure

Posted: Wed Jun 05, 2013 9:23 pm
by bosker
Or even...
No restore and no assembler.

Code: Select all

EnableExplicit

Procedure fillList(List emptyList.s(), dataLabel.i)
    Protected *StrAdr.String

    Repeat
        *StrAdr = datalabel         ; get string pointer address

        If Len(*StrAdr\s)           ; end of list marked by empty string
            AddElement(EmptyList())
            EmptyList() = *StrAdr\s
            datalabel + SizeOf(Integer)
        EndIf
    Until Len(*StrAdr\s) = 0
EndProcedure

NewList scientists.s()
fillList(scientists(), ?scientists)

ResetList(scientists())

While NextElement(scientists())
    Debug scientists()
Wend

; Data is declared as string pointers rather than strings
DataSection
scientists:
    Data.i @"Albert Einstein", @"Isaac Newton", @"Charles Darwin", @"Guglielmo Marconi"
    Data.i @"Galileo Galilei", @"Leonardo da Vinci", @"René Descartes", @"Thomas Edison"
    Data.i @""      ; list terminator (so I don't have to count how many)
EndDataSection


Re: Quickest way to input up to 10 strings into a structure

Posted: Thu Jun 13, 2013 5:09 pm
by Primoz128
Thanks to you all. Gona try and implement it soon enough.