Segments in QB (DEF SEG)


This is a very old article written by me (around 2001, I guess) and it is just here for record.

This tutorial is very complex and technical. So, I recommend you to sit in some cool place where you can relax and read this. Maybe, you can take a print out and save it for later use. OK, here it lies…

Now, when Computers where first invented, they did not think that we will be playing with Gigabytes today and so they invented Computers which could support maximum of 1 MB RAM. In this 1 MB RAM, system programs(DOS, Device Drivers, BIOS, Video) all sit in and leave only 640 KB of Memory for use to you. However, this is not important. What you need to know is that maximum memory you can use in DOS is 1 MB. Just remember that 1 MB = 1048576 Bytes or 220 bytes.

Now, processor accesses memory using a set of wires on the motherboard called as bus. To access 1 MB of RAM, the processor needs 20 wires(Each wire carries a 1 or a 0, and 1 MB = 220 bytes, remember?). Using these 20 wires, processor can access memory of 1 MB. Got it? Be sure that you understood all this. Then, proceed next.

Now, programs are written in a way that they access a fixed memory address. Like, I can make a program which uses memory addresses 10000 – 11000(Don’t get confused, these are the byte numbers which are used to refer to the memory) and you can make a program which accesses memory addresses 11000 – 12000. Now, even if both the programs run together, they run fine. But just assume, that you also write programs to use memory at 10000 – 11000, then both the programs crash as the data they store in the memory is corrupted by the other program. Like, my program corrupts your data and your program corrupts mine.

So, to avoid this, the people invented the segment:offset combination to access memory. You can think of segments as your file folders and offset as slots to store files(Here, it is only bytes). Or, you can think of segment as your bag and offset as each section in your bag. With this way, I can store my data into offset no. 1000 – 1100 and you can also store the data in 1000 – 1100 offsets and still run fine provided they are in different segments. This technology implemented by Intel and others allows you to run the program free of many worries and program relocation(changing the segment of the program so that it does not interfere with other code) is taken care by the OS. So, you take the segment number given to you by DOS and store your data in it by using the offset number. This offset number is the only needed one for your data storage and access. If you change the segment and use the same offset, then you will get another data.

Now for DEF SEG statement, DEF SEG gets really very very simple now. It only changes the segment from which you are accessing the data, so if you use the segment:offset combination and read into the memory, you get the data. Note that this doesn’t have any effect on any QB variables even though they are stored in the memory using the same technique(QB protects them). This only has effects on PEEK/POKE statements that are used to read and write in memory. DEF SEG defines the segment and PEEK/POKE define the offset. So, you get segment:offset combination and read/write in the memory. With this, you can do amazing things. Some of the code in my library is using this technique(but it doesn’t use QB’s DEF SEG, it uses it’s ASM equivalent). If you want to know, some of the things that you can do by this, they are Fast PSET in various modes, system info, etc..