Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Varseg and Varptr

Status
Not open for further replies.

Barok

MIS
Dec 13, 2002
134
CA
Currently i am working on a pixel*tile scrolling engine. after countless hours of reading, i figured that in order to carry out my goal, i would need to read about varseg and varptr. I couldn't find any useful things on the web. could anyone tell me about the commands like what the do, about them, and how to use them?
 
varptr gives you the offset and varseg gives you the segment of whatever you need them for. like if i wanted to find the offset of an array i have it would look like this


dim array&(10)
arrayoffset=varptr(array&(0))
 
Varseg and Varptr are the special way qbasic has to point to a memory position.
Dos is able to access to 1Meg of memory, 1Meg in binary is a number of 20 bits. As the processor Dos was designed for was 16 bits (8086) the memory addresses are handled as two 16 bit words, the segment and the address, as in &ha000:&h0001

The Qbasic keywords using addresses to access directly to memory by are PEEK, POKE, BLOAD and BSAVE.

To access a memory position you first
DEF SEG =segment
and then you perform the needed operations
p.e
poke Offset,n 'sets to n the byte at segment:eek:ffset



VARSEG and VARPTR are functions allowing your program to know where in memory are your variables, so you can peek and poke them. VARSEG returns the segment and VARPTR returns offset.
Code:
]
a%=2
DEF SEG=varseg(a%)
poke varptr(a%),4
print a%            'should print 4
for the string variables VARPTR must be substituted by SADD
Code:
a$=" " 
DEF SEG=VARSEG(A$)
POKE SADD(A$),65 
PRINT A$          'should print 'A'
Be careful with direct peeks and pokes, none of the QB's safety nets work there. You can corrupt the data, the program or cause a General Protection Fault in Windows, by poking to the wrong place, so ALWAYS save your program before running.

the segment of the graphics screen is &HA000 (no need of a VARSEG to know it, it's fixed) so
Code:
screen 13
def seg =&ha000
poke 10,15      'should plot a white dot at column 10

Don't forget QB drawing primitives clip automatically everything you draw to the limits of the screen,and with direct pokes you must do yourself the clipping. If you poke outside the screen limits your program can be corrupted or cause a windows fault. Antoni
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top