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]

  The fourteenth and last register, called the flags register, is really a
  collection of individual status and control bits called flags. The flags
  are maintained in a register, so they can be either saved and restored as
  a coordinated set or inspected as ordinary data. Normally, however, the
  flags are set and tested as independent items--not as a set.

  There are nine 1-bit flags in the 8086's 16-bit flags register, leaving 7
  bits unused. (The 80286 and 80386 use some of the unused flags to support
  protected-mode operation.) The flags can be logically divided into two
  groups: six status flags, which record processor status information
  (usually indicating what happened with a comparison or arithmetic
  operation), and three control flags, which direct some of the 8086
  instructions. Be prepared to see a variety of notations for the flags,
  including distinct names for whether they are set (1) or clear (0). The
  terms used in Figures 2-7 and 2-8 are the most common.

  --------------------------------------------------------------------------
  The Stack
    The stack is a built-in feature of the 8086. It provides programs with a
    place to store and keep track of work in progress. The most important
    use of the stack is to keep a record of where subroutines were invoked
    from and what parameters were passed to them. The stack can also be used
    for temporary working storage, although this is less fundamental and
    less common.

    The stack gets its name from an analogy to a spring-loaded stack of
    plates in a cafeteria: New data is "pushed" onto the top of the stack
    and old data is "popped" off. A stack always operates in
    last-in-first-out (LIFO) order. This means that when the stack is used
    to keep track of where to return to a program, the most recent calling
    program is returned to first. This way, a stack maintains the orderly
    workings of programs, subroutines, and interrupt handlers, no matter how
    complex their operation.

    A stack is used from the bottom (highest address) to the top (lowest
    address) so that when data is pushed onto the top of the stack, it is
    stored at the memory addresses just below the current top of the stack.
    The stack grows downward so that as data is added, the location of the
    top of the stack moves to lower and lower addresses, decreasing the
    value of SP each time. You need to keep this in mind when you access the
    stack, which you are likely to do in assembly-language interface
    routines.

    Any part of any program can create a new stack space at any time, but
    this is not usually done. Normally, when a program is run, a single
    stack is created for it and used throughout the operation of the
    program.

    There is no simple way to estimate the size of stack that a program
    might need, and the 8086's design does not provide any automatic way of
    detecting when stack space is in short supply or exhausted. This can
    make programmers nervous about the amount of space that should be set
    aside for a stack. A conservative estimate of how much stack space to
    maintain is about 2 KB (2048 bytes), the default amount allocated by
    many high-level language compilers.

                                             +------+.--- Bottom of stack
                                       :1008 | 5E00 |
          +------+.--- Bottom of stack       |------|
    :1008 | 5E00 |                     :1006 | 4D00 |
          |------|                           |------|
    :1006 | 4D00 |                     :1004 | 3C00 |.--- Old top of stack
          |------|                           |------|
    :1004 | 3C00 |.--- Top of stack    :1002 | 2B00 |.--- Top of stack
          |------|     (SP=1004)             |------|     (SP=1002)
    :1002 |      |                     :1000 |      |
          |------|                           +-.--.-+
    :1000 |      |                             |  |
          +------+                             PUSH
    a. Stack before a PUSH             b. Stack after a PUSH
    ----------------------             ---------------------

                              +------+.--- Bottom of stack
                        :1008 | 5E00 |
                              |------|
                        :1006 | 4D00 |
                              |------|
                        :1004 | 3C00 |.--- Top of stack
                              |------|     (SP=1004)
                        :1002 |      |
                              |------|
                        :1000 |      |
                              +-|--|-+
                                .  .
                                POP
                        c. Stack after a POP
                        --------------------

  --------------------------------------------------------------------------

  Code        Name                     Use
  --------------------------------------------------------------------------
  CF          Carry flag               Indicates an arithmetic carry
  OF          Overflow flag            Indicates signed arithmetic overflow
  ZF          Zero flag                Indicates zero result, or equal
                                       comparison
  SF          Sign flag                Indicates negative result/comparison
  PF          Parity flag              Indicates even number of 1 bits
  AF          Auxiliary carry flag     Indicates adjustment needed in
                                       binary-coded decimal (BCD) arithmetic
                                       operations
  --------------------------------------------------------------------------

  Figure 2-7.  The six status flags in the 8086's flags register.

  Code        Name                     Use
  --------------------------------------------------------------------------
  DF          Direction flag           Controls increment direction in
                                       string operations (CMPS, LODS, MOVS,
                                       SCAS, STOS)
  IF          Interrupt flag           Controls whether interrupts are
                                       enabled
  TF          Trap flag                Controls single-step operation (used
                                       by DEBUG) by generating an interrupt
                                       at the end of every instruction
  --------------------------------------------------------------------------

  Figure 2-8.  The three control flags in the 8086's flags register.

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