Address of a variable: Difference between revisions

no edit summary
(Add Zig)
No edit summary
Line 1,613:
 
In addition some folks have written binary Python modules which implement "peek" and "poke" operations, but these are non-standard.
 
=={{header|QBA64}}==
 
<lang QB64>
' Adapted from QB64wiki example a demo of _MEM type data and _MEM functions
Type points
x As Integer
y As Integer
z As Integer
End Type
 
Dim Shared m(3) As _MEM
Dim Shared Saved As points, first As points
 
m(1) = _Mem(first.x)
m(2) = _Mem(first.y)
m(3) = _Mem(first.z)
 
print "QB64 way to manage address of variable..."
first.x = 3: first.y = 5: first.z = 8
Print first.x, first.y, first.z
Save first
first.x = 30: first.y = 50: first.z = 80
Print first.x, first.y, first.z
 
RestoreIt
Print first.x, first.y, first.z
 
_MemFree m(1)
_MemFree m(2)
_MemFree m(3)
End
 
Sub Save (F As points)
Saved.x = F.x
Saved.y = F.y
Saved.z = F.z
End Sub
 
Sub RestoreIt
_MemPut m(1), m(1).OFFSET, Saved.x
_MemPut m(2), m(2).OFFSET, Saved.y
_MemPut m(3), m(3).OFFSET, Saved.z
End Sub
</lang>
 
<lang QB64>
' Full demonstration of being together Qbasic and QB64 methods for accessing and modifying memory directly
Dim A As Integer, B As String, C(1 To 4) As Double
 
Print " OLD FASHION MODE OF QBASIC / QUICKBASIC"
Sleep 2: Cls
'Print
Print " GETTING ADDRESS OF VARIABLE"
Print "address of variable QuickBasic compatible in 16 bit OS is emulated in QB64"
Print "Qbasic and QuickBasic used as address a Segment and an Offset"
Print "and by cause many QBasic statements change addresses of variables "
Print "for reorganization of RAM it is higly reccomanded to use"
Print "immediately the address of variable got by Varptr and Varseg"
Print "address of A "; VarSeg(A); "-"; VarPtr(A)
Print "address of the first byte of string B "; VarSeg(B); "-"; VarPtr(B)
Print "address of the 2nd element of array C"; VarSeg(C(2)); "-"; VarPtr(C(2))
Print "address of string B from actual data Segment"; SAdd(B)
Print
Print "Direct access to ram video example "
Print " setting Text video ram as Segment": Def Seg = &HB800
Print "reading at offset 240 of text video ram "; Peek(240)
Print "changing at offset 240 of text video ram with"; Asc("@"); " from e to @"
Poke 240, Asc("@")
Print "changing foreground and background color of a character in text video ram "
Poke 241, Peek(241) Xor &B110111
Poke 265, Peek(265) Xor &B010101
Poke 267, Peek(267) Xor &B001010
Def Seg
Print
Print " CREATING VARIABLE AT A SPECIFIC ADDRESS"
Print " created variable D as single at "; VarSeg(d); "-"; VarPtr(d)
Print "Using the variable in normal way"
Print d
d = 100
Print d
Print " press any key to continue..."; Input$(1)
Cls
Print "Only QB64 methods... _MEM functions"
Dim M As _MEM
 
A = 2: Print "A is "; A
M = _Mem(A) ' it accesses to variable A
 
_MemPut M, M.OFFSET, 10 As INTEGER ' it modifies value of A
Print " and now A is "; A
 
Print "d is "; d
d = _MemGet(M, M.OFFSET, Integer) ' it gets value of A into d
Print "and now d is "; d
_MemFree M
End
</lang>
 
 
=={{header|R}}==