Board index » delphi » Seg function in BP7

Seg function in BP7

 Hi,
        I was wondering if anyone could explain to me howq the seg function works and what it does as I have seen it around in sample pascal programs when
they aer used on virtual screens

var  
  virscr : pointer;
  virseg : word;

begin
  getmem(virscr, 64000) {I understand this allocates 64000 bytes to virscr}
  virseg := seg(virscr^); {This i don't understand...}

                Thanks Nish

 

Re:Seg function in BP7


In article <47kv38$...@sunb.ocs.mq.edu.au>, s3036...@hardy.ocs.mq.edu.au (MEHRA Swati) says:

Quote
>        I was wondering if anyone could explain to me howq the seg function works and what it does as I have seen it around in sample pascal programs when
>they aer used on virtual screens

>var  
>  virscr : pointer;
>  virseg : word;
>begin
>  getmem(virscr, 64000) {I understand this allocates 64000 bytes to virscr}
>  virseg := seg(virscr^); {This i don't understand...}

To begin with, you need to make sure that the memory you allocate is begins
at offset 0.  So instead, use the following:

uses Memory;

var VirtScr : pointer;
    VirtSeg : word;

begin
   VirtScr := MemAllocSeg( 64000 );
   if( VirtScr = NIL ) then Exit;
   VirtSeg := Seg( VirtScr^ );

     To explain about how Seg works, remember that a pointer in TP always
has both the segment and offset.  So Seg just returns the segment part.
If you're trying to use the memory for a virtual screen, then when you
write your graphics routines, instead of having to write always to $A000
you can specify VirtSeg instead.  Take, for example a sample snippet from
a graphics library I'm writing:

var VidSegment : word;  { this is normally set to $A000 }
    VidWidth   : integer; { this is normally 320 }
    VidPenColor : byte;   { the color to write }

procedure PutPointAt( X, Y : integer ); assembler;
asm
    MOV ES,VidSegment      { load the output segment }
    MOV AX,Y
    IMUL WORD PTR VidWidth
    ADD AX,X
    MOV BX,AX
    MOV AL,VidPenColor
    MOV ES:[BX],AL
end; { procedure PutPointAt }

    In this example, PutPointAt writes to the segment specified by
VidSegment.  So if you want to write to the virtual screen instead we
can then do:

    VidSegment := Seg( VirtScr^ );
    PutPointAt( 50, 50 );   { written to the virtual screen instead }

which will work fine assuming that the memory allocated starts at an
offset of 0.

Other Threads