| www.retrosoftware.co.uk http://www.retrosoftware.co.uk/forum/ |
|
| Variable pointers http://www.retrosoftware.co.uk/forum/viewtopic.php?f=73&t=38 |
Page 1 of 1 |
| Author: | FrancisL [ Fri Feb 29, 2008 5:14 pm ] |
| Post subject: | Variable pointers |
Is there any way of obtaining the actual memory address of a variable or string? Kind regards, Francis. |
|
| Author: | SteveO [ Sat Mar 01, 2008 9:11 am ] |
| Post subject: | Re: Variable pointers |
There is no easy command in BASIC that gives you the address. However I read once in Electron User (back in about 86 !) how BASIC stores it's variables. Which is obviously the same for the BBC. I'll outline it here but if you ask Dave_E he may remember which one and you may be able to get the scan of the pages concerned. BTW don't bank on it being in '86, it was around then but could have been a year either way or so ! Basically ('scuse the pun). There are 52 initial pointers (104 bytes) allocated to point to the starting list of any variable beginning with a certain letter. So there is a a start pointer for the letter A,B C ..Z etc. as well as a,b,c ..z So when BASIC sees a variable it looks at the first letter to find out where all the variables beginning with that letter are stored. For the letter B for example the address would be stored at bytes 2 and 3 of the pointer list. It would get this address which would point to the data block for the first variable beginning with B in that list. In here is the variable name and actual value etc. It then compares the variable name with what it's looking for, if it's found then fine, however if not found then there is a pointer in this data block that points to the next variable beginning with B in the list. It then goes to that data block etc. until found. If not found (i.e. the next pointer is null) then will report variable not found error. So from this you can see that you can get a program to run much faster if you just use single letter variable names, although not as readable ! The worst scenario would be to use a lot of variables that all start with the same letter as BASIC will spend time searching down the list. Single letter entries will all have their own pointer direct to that entry. So that's roughly it, I just don't remember where things start in memory (such as the table of initial pointers) or if there's a BASIC machine code routine you can call to help you get the address (obviously there is one, but where it is and how easy is it to call I don't know). You could write your own assembler routine to get the address though armed with the above knowledge (well with the more specific knowledge in the Electron article). If you get that copy of Electron User you'll be fine. Wish I still had all my copies, they went for recycling many many years ago |
|
| Author: | FrancisL [ Sat Mar 01, 2008 1:21 pm ] |
| Post subject: | Re: Variable pointers |
Hmm... I think I might be making things more complicated for myself then if I went down that route. I guess I have been spoiled by other languages that allow you to do this quite easily. Kind regards, Francis. |
|
| Author: | RichTW [ Sun Mar 02, 2008 8:12 pm ] |
| Post subject: | Re: Variable pointers |
But what BBC Basic *can* do is let you define a memory address which can be used to hold an integer - for example: Code: DIM var% 3 !var%=69 PRINT "Value of var = ";!var PRINT "Address of var = ";var This is more-or-less equivalent to saying in C++: Code: int* var = new int; *var = 69; cout << "Value of var = " << *var << endl; cout << "Address of var = " << var << endl; BBC Basic's DIM statement is a little odd, in that you give it a value one less than the number of bytes you want to reserve. Of course, you can also assign an address to the variable yourself with: Code: var%=&900:REM use &900-&903 to store int !var%=69 This entity "!var%" can be used just like any other variable - it can even be made LOCAL inside a PROC: Code: DEF PROCwhatever LOCAL !var% !var%=50:REM change value of !var% ENDPROC:REM !var% will be restored to its old value here Maybe this is of some use? |
|
| Author: | FrancisL [ Sun Mar 02, 2008 11:21 pm ] |
| Post subject: | Re: Variable pointers |
Thanks Rich. Integer variables weren't really a problem since I can already do that. It was really more for string variables so I could manipulate the text directly in memory. As far as I can tell there is no easy way to do this. Kind regards, Francis. |
|
| Author: | GregC [ Sat Mar 08, 2008 12:46 pm ] |
| Post subject: | Re: Variable pointers |
You can write a small assembly language routine to pass back the address of a variable given by CALL. For instance Code: CALL code,addr%,string$ calls code with a parameter block at &600 giving the types and addresses of addr% and string$. The routine can take the address of string$ (via another parameter block) and store it in addr%. For example (clears throat:)Code: 10 test$=STRING$(255," ") :REM pre-allocate maximum memory for string HTH.20 test$="Hello there!" 30 ptra=&70:ptrb=&72 :REM address of pointers in user zero page 40 osbyte=&FFF4 50 DIM code% 256 60 FOR pass%=0 TO 3 STEP 3 70 P%=code% 80 [OPT pass% 90 .getaddr 100 LDA &600:CMP#2:BNE args \Quit unless two arguments 110 LDA &603:CMP#4:BNE args \Quit unless first argument is an integer 120 LDA &601:STA ptra \Set up pointer to integer variable 130 LDA &602:STA ptra+1 140 LDX &604:LDA &605 \Load XA with address of target 150 LDY &606:CPY#&81:BNE direct \If target is a string variable, then... 160 STX ptrb:STA ptrb+1 \there's an info block. set up a pointer 170 LDY#0:LDA(ptrb),Y:TAX \load XA with the actual string address. 180 INY:LDA(ptrb),Y 190 .direct 200 LDY#1:STA(ptra),Y \in all cases save XA in the integer var 210 TXA:DEY:STA(ptra),Y 220 LDA#&82:JSR osbyte \call OSBYTE &82, get high order address 230 TYA:LDY#3:STA(ptra),Y \set top half of integer 240 TXA:DEY:STA(ptra),Y \to the high order address. 250 RTS 260 .args 270 BRK:EQUB&1F:EQUS"Arguments":BRK \error if arguments incorrect 280] 290 NEXT 300 CALL getaddr,N%,$&3000:PRINT~N% 310 CALL getaddr,N%,test$:PRINT~N% 320 ?N%=ASC("C") 330 PRINT test$ --Greg |
|
| Author: | FrancisL [ Sat Mar 08, 2008 5:43 pm ] |
| Post subject: | Re: Variable pointers |
Nice work Greg! Kind regards, Francis. |
|
| Author: | FrancisL [ Sat Mar 08, 2008 6:17 pm ] |
| Post subject: | Re: Variable pointers |
I've just incorporated your routine into my adventure game. Having direct memory access to the strings dramatically increases the speed of my text routines! Thanks again! Kind regards, Francis. |
|
| Author: | jgharston [ Tue Aug 25, 2009 4:20 pm ] |
| Post subject: | Re: Variable pointers |
What you need is the ^ function from BBC BASIC for Windows, viz: address%=^variablename Lets you do funky things like s1$="":!^s1$=!^s2$ to make s1$ and s2$ point to the same string in memory. I've got some code somewhere that give you FNVar_Addr("varname") somewhere, I'll put it online sometime. |
|
| Page 1 of 1 | All times are UTC [ DST ] |
| Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group http://www.phpbb.com/ |
|