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]

  It is important to know that various rules apply to the use of registers,
  and it is essential to be aware of these rules when writing assembly-
  language interface routines. Because the rules and conventions of usage
  vary by circumstance and by programming language, exact guidelines are not
  always available, but the general rules that follow will apply in most
  cases. (You will find additional guidance, and working models to copy, in
  the examples in Chapters 8 through 20.) Keep in mind, though, that the
  following rules are general, not absolute.

  Probably the most useful rule for using the registers is simply to use
  them for what they are designed for. The idea that each of the 8086
  registers has certain special uses may seem somewhat quirky, particularly
  to a programmer who is accustomed to working with a CPU that has a less
  specialized set of registers (such as the 68000, for example). On the
  8086, using the registers for their natural functions leads to cleaner,
  more efficient source code and ultimately to more reliable programs.

  For example, the segment registers are designed to contain segment values,
  so don't use them for anything else. (In 80286 protected mode you can't
  use them for anything else anyway without generating an error condition.)
  The BP register is intended for stack addressing; if you use it for
  anything else, you'll have to do some fancy footwork when you need to
  address values in the stack segment.

  Particular rules apply to the four segment registers (CS, DS, ES, and SS).
  The CS register should be changed only through intersegment jumps and
  subroutine calls.

  Most programmers use the DS register to point to a default data segment
  that contains the data most frequently used in a program. This means that
  the value in the DS register is usually initialized at the beginning of a
  program and then left alone. Should it be necessary to use DS to address a
  different segment, its original value is saved, the new segment is
  accessed, and then the original value is restored. In contrast, most
  people use the ES register as needed to access arbitrary segments in
  memory.

  The stack segment (SS) and stack pointer (SP) registers should usually be
  updated implicitly, either by PUSH and POP instructions or by CALL and RET
  instructions that save subroutine return addresses on the stack. When DOS
  loads a program into memory to be executed, it initializes SS and SP to
  usable values. In .COM programs, SS:SP points to the end of the program's
  default segment; in .EXE programs, SS:SP is determined explicitly by the
  size and location of the program's stack segment. In either case, it's
  rare that you need to change SS or SP explicitly.

  If you need to discard a number of values from the stack or reserve
  temporary storage space on top of the stack, you can increment or
  decrement SP directly:

  add sp,8                        ; discard four words (8 bytes)
                                  ; from stack
  sub sp,6                        ; add three empty words (6 bytes)
                                  ; to top of stack

  If you need to move the stack to a different location in memory, you must
  generally update both SS and SP at the same time:

  cli                             ; disable interrupts
  mov ss,NewStackSeg              ; update SS from a memory variable
  mov sp,NewStackPtr              ; update SP from a memory variable
  sti                             ; re-enable interrupts

  Be careful when you change SS and SP explicitly. If you modify SS but fail
  to update SP, SS will be specifying a new stack segment while SP will be
  pointing somewhere inside another stack segment--and that's asking for
  trouble the next time you use the stack.

  It's hard to be explicit about the use of the other registers. In general,
  most programmers try to minimize memory accesses by keeping the
  intermediate results of lengthy computations in registers. This is because
  it takes longer to perform a computation on a value stored in memory than
  on a value stored in a register. Of course, the 8086 has only so many
  registers to work with, so you may find yourself running out of registers
  before you run out of variables.

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