8086 PROGRAMMING - REGISTER USE

CPU has 14 registers, 16 bits each

AX  Accumulator          BP Base Pointer          
BX  Base register        SP Stack Pointer 
CX  Count register       DI Destination Index 
DX  Data register        SI Source Index

CS  Code Segment  - Executable Code
DS  Data Segment  - Data Area
SS  Stack Segment - Stack Data
ES  Extra Segment - Extra Data   

Flags -  Condition Flag bits
IP    -  Instruction Pointer  
Also we can use half registers:
AX = AH + AL, BX = BH + BL, CX = CH + CL, DX = DH + DL where H = High 8 bits and L = Low 8bits

AX,BX,CX,DX are often used as accumulators. Unfortunately, each register has limits on which instruction/addressing mode combinations work with it.


PROGRAM STRUCTURE

A program must have a code segment, a data segment, and a stack segment. The way memory is laid out for these areas is called the MEMORY MODEL.

DEBUG automatically assumes they are all in the same physical segment, the same 16-bit range. This is called a Tiny memory model, where CS=DS=SS and SP = FFFE.

In DEBUG you have to arrange Data area and Code area. DEBUG builds Stack from the other end of segment toward your Code and Data.

If you use an assembler you must use a directive to tell Assembler and Linker how to allocate segments.


MEMORY MODELS

Defines how memory space is allocated in terms of the number of 64K segments that are to be used at runtime:

Tiny CS = DS = SS
Small CS = 1 segment, DS = SS = 1 seg
Compact CS = 1 segment, DS = SS = Multiple segments
Medium CS = multiple segments, DS = SS = 1 segment
Large CS = multiple segments, DS = SS = Multiple segments, Arrays < 64K bytes
Huge same as Large, but Array size not limited to 64K bytes


.COM FILE STRUCTURE

          ORG  0100h
          JMP  START   ; GOTO START, jump over Data Block
          db  'THIS IS A DATA STRING.'
          db   0dh,0ah
          dw   1234

START:    mov ax,0000   ; Sum = 0
          mov bx,0200   ; Start Data
          mov cx,0010h  ; Count 10h Words

LOOP:     add ax,[bx]   ; Sum = Sum + Word
          dec cx
          jnz LOOP      ; If not ZERO, branch to LOOP
          int 20h       ; Exit to DOS