Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Peter Norton Programmer's Guide - Norton Guide http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]

  As we discussed earlier, the complete address of a memory location
  consists of a 16-bit segment value and a 16-bit offset within the segment.
  Four registers, called CS, DS, ES, and SS, are used to identify four
  specific segments of memory. Five offset registers, which we'll discuss
  shortly, can be used to store the relative offsets of the data within each
  of the four segments.

  Each segment register is used for a specific type of addressing:

  .  The CS register identifies the code segment, which contains the program
     that is being executed.

  .  The DS and ES registers identify data segments where data used in a
     program is stored.

  .  The SS register identifies the stack segment. (See page 32 for more
     information about stacks.)

  Programs rarely use four separate segments to address four different 64 KB
  areas of memory. Instead, the four segments specified in CS, DS, ES, and
  SS usually refer to overlapping or identical areas in memory. In effect,
  the different segment registers identify areas of memory used for
  different purposes.

  For example, Figure 2-6 shows how the values in the segment registers
  correspond to the memory used in a hypothetical DOS program. The values in
  the segment registers are chosen to correspond to the start of each
  logically different area of memory, even though the 64 KB areas of memory
  identified by each segment overlap each other. (See Chapter 20 for more
  about segments and the memory layout of DOS programs.)

  All 8086 instructions that use memory have an implied use of a particular
  segment register for the operation being performed. For example, the MOV
  instruction, because it acts on data, uses the DS register. The JMP
  instruction, which affects the flow of a program, automatically uses the
  CS register.

  This means that you can address any 64 KB segment in memory by placing its
  paragraph address in the appropriate segment register. For example, to
  access data in the video buffer used by IBM's Color Graphics Adapter, you
  place the paragraph address of the start of the buffer in a segment
  register and then use the MOV instruction to transfer data to or from the
  buffer.

              +---------------------+        .---+
     SS=2919H |       Stack         | 29190H     |- 2 KB
              |---------------------|        .---|
              |                     |            |
     DS=2419H |    Program data     |            |- 20KB
              |                     | 24190H     |
              |---------------------|        .---|
              |                     |            |
     CS=2019H |   Executable code   |            |- 16 KB
              |                     | 20190H     |
              +---------------------+        .---+
  Segment registers              Physical addresses

  Figure 2-6.  Segment usage in a typical DOS program. Each segment register
  contains the starting paragraph of a different area of memory.

  mov ax,0B800h                   ; move the segment value into DS
  mov ds,ax
  mov al,[0000]                   ; copy the byte at B800:0000
                                  ; into AL

  In interpreted BASIC you can use this method with the DEF SEG statement:

  DEF SEG = &HB800                ' move the segment value into DS
  X = PEEK(0000)                  ' copy the byte at B800:0000 into X

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson