Difference between revisions of "Address of a variable"
Revision as of 01:49, 8 March 2015
BBC BASIC for Windows implements the
^identifier function (as
opposed to the
num ^ num operator) to return the address
of a variable. For example,
^fred% returns the address where
the data for the variable
fred% is stored.
This can be implemented on other versions of BBC BASIC with the following code, which is also available in the AddrOf BASIC library.
DEFFNAddrOf_65:DIM A% 31:P%=A% [OPT2:LDA &604:STA &70:LDA &605:STA &71:LDY #0 LDA &601:STA (&70),Y:INY:LDA &602:STA (&70),Y:INY LDA #0:STA (&70),Y:INY:STA (&70),Y:RTS:]:=A%
DEFFNAddrOf_Z80:DIM A% 23:P%=A% [OPT 2:LD L,(IX+5):LD H,(IX+6):LD A,(IX+2):LD (HL),A INC HL:LD A,(IX+3):LD (HL),A:INC HL:LD (HL),0:INC HL LD (HL),0:RET:]:=A%
BBC BASIC for Windows implements
^identifier. DOS BASIC does
not, and the following code must be used.
DEFFNAddrOf_86:DIM A% 19:P%=A% [OPT 2:MOV AX,DS:[BP+2]:MOV BX,DS:[BP+5]:MOV DS:[BX],AX MOV AX,0:MOV DS:[BX+2],AX:RETF:]:=A%
PDP-11 BASIC implements
^identifier natively. The following
code can be used for compatibility. Current versions of PDP-11 BASIC
does not have an inline assembler, and the code must be created manually.
DEFFNAddrOf_PDP:DIM A% 15:!A%=&C1D80:A%!4=&81D81:A%!8=&A311009:A%!12=870002:=A%
This creates the following code:
MOV 12(SP),R0:MOV 8(SP),R1:MOV R0,(R1):CLR 2(R1):RTS PC
DEFFNAddrOf_ARM:DIM A% 15 [OPT2:LDR R0,[R9]:LDR R1,[R9,#8]:STR R1,[R0]:MOV PC,R14:]:=A%
Once the appropriate
FNAddrOf_* function is called to set up the
machine code, for instance
addrof%=FNAddrOf_65, then the
address of a variable can be found with
CALL addrof%,variable,result%, leaving the address in
result% must be a pre-existing integer
addrof% with fewer than two parameters, or
with a non-integer result will have undefined results.
BBC BASIC for Windows allows
^FNname. This code only supports those variables that can be
passed to the
result% holds the address of the variable's
data block, as with parameters passed to
CALL. This will be
||result% -> 5-byte floating point value|
||result% -> 4-byte integer value|
||result% -> string information block|
The string information block is different on different platforms. See CALL for details.
REM Print exponent of PI num=PI CALL addrof%,num,A% PRINT "Exponent: ";?A%
REM Load 4-byte file into a variable fred%=0:addr%=0:CALL addrof%,fred%,addr% OSCLI "LOAD data "+STR$~A%
REM Swap byte order big%=&12345678 little%=big% CALL addrof%,little%,A% tmp%=A%?0:A%?0=A%?3:A%?3=tmp% tmp%=A%?1:A%?1=A%?2:A%?2=tmp%
Jgharston 00:06, 5 December 2009 (UTC)