January 20, 2021, 01:09:26 PM

### Author Topic: [bb] Push/Pop Data Functions by N [ 1+ years ago ]  (Read 641 times)

#### BlitzBot

• Jr. Member
•  • Posts: 1 ##### [bb] Push/Pop Data Functions by N [ 1+ years ago ]
« on: June 29, 2017, 12:28:43 AM »
Title : Push/Pop Data Functions
Author : N
Posted : 1+ years ago

Description : Simple stuff to push and pop data to and from banks.

I use these functions heavily in my latest engine, so although simple they're very, very useful.

Code :
Code: BlitzBasic
1. Function PushObject(Bank,Value)
2.         Position = BankSize(Bank)
3.         ResizeBank(Bank,Position+4)
4.         PokeInt(Bank,Position,Value)
5.         Return Position/4
6. End Function
7.
8. Function PopObject(Bank)
9.         R = PeekInt(Bank,BankSize(Bank)-4)
10.         PokeInt(Bank,BankSize(Bank)-4,0)
11.         ResizeBank(Bank,BankSize(Bank)-4)
12.         Return R
13. End Function
14.
15. Function PushFloat(Bank,Value#)
16.         Position = BankSize(Bank)
17.         ResizeBank(Bank,Position+4)
18.         PokeFloat(Bank,Position,Value)
19.         Return Position/4
20. End Function
21.
22. Function PopFloat#(Bank)
23.         R# = PeekFloat(Bank,BankSize(Bank)-4)
24.         PokeInt(Bank,BankSize(Bank)-4,0)
25.         ResizeBank(Bank,BankSize(Bank)-4)
26.         Return R#
27. End Function
28.
29. Function PushString(Bank,Text\$)
30.         Position% = BankSize(Bank)
31.         ResizeBank(Bank,BankSize(Bank)+(Len(Text\$)+1))
32.         PokeByte(Bank,Position,0)
33.         For n = 1 To Len(Text\$)
34.                 PokeByte(Bank,Position+n,Asc(Mid(Text\$,n,1)))
35.         Next
36.         Return Position/4
37. End Function
38.
39. Function PopString\$(Bank)
40.         n = BankSize(Bank)-1
41.         Repeat
42.                 K = PeekByte(Bank,n)
43.                 If K > 0 Then Text\$ = Chr(K)+Text\$
44.                 n = n - 1
45.         Until K = 0
46.         ResizeBank(Bank,n+1)
47.         Return Text\$
48. End Function
49.
50. Function EraseObject(Bank,Index)
51.         Size = BankSize(Bank)
52.         If Index <= Size Then
53.                 R% = GetObject(Bank,Index)
54.                 If Index = Size Then
55.                         PokeInt(Bank,(Index)*4,0)
56.                         ResizeBank(Bank,Size-4)
57.                 Else
58.                         n = Index * 4
59.                         While n < Size - 4
60.                                 PokeInt(Bank,n,PeekInt(Bank,n+4))
61.                                 n = n + 4
62.                         Wend
63.                         ResizeBank(Bank,Size-4)
64.                 EndIf
65.         EndIf
66.         Return R
67. End Function
68.
69. Function GetObject(Bank,Index)
70.         Return PeekInt(Bank,Index*4)
71. End Function
72.
73. Function GetFloat(Bank,Index)
74.         Return PeekFloat(Bank,Index*4)
75. End Function
76.
77. ;; The name on this one is a bit vague- it returns the amount of 4-byte objects in a bank.  Of course, it's just a BankSize()/4, but it saves time typing
78. ;; Does not work if you've pushed a string onto the bank
79. Function Objects(Bank)
80.         Return BankSize(Bank)/4
81. End Function

Techlord(Posted 1+ years ago)

Heres a simple Stack using arrays
Code: [Select]
`Dim stack(size) ;Element stack(0) is used as pointer;Push Stackstack(0)=stack(0)+1:stack(stack(0))=value;Pop Stackvalue=stack(stack(0)):stack(0)=stack(0)-1`A stack of strings
Code: [Select]
`Dim stack\$(255);Push Stackstack(0)=Str(Int(stack(0))+1):stack(stack(0))="string";Pop Stackstring\$=stack(stack(0)):stack(0)=Str(Int(stack(0))-1)`

N(Posted 1+ years ago)

<a href="codearcs96c7.html?code=1160" target="_blank">Ding[/url] [/i]