8086 INSTRUCTION SET
Stack Implementation in Memory
Original SP             In Use   Stack grows in
                        In Use   direction
                                 of decreasing
                        In Use   memory
                        In Use   addresses
         Direction of
           increasing   In Use
            memory      In Use
           addresses                      SS:SP
                        FREE
                        FREE
                        FREE
                        FREE
                                          SS
                Stack Implementation
                  in Memory (cont.)
• SS - Stack Segment
• SP (stack pointer) always points to the top of the stack
   – SP initially points to top of the stack (high memory address).
   – SP decreases as data is PUSHed
            PUSH AX ==> SUB SP, 2 ; MOV [SS:SP], AX
   – SP increases as data is POPed
            POP       AX ==> MOV AX, [SS:SP] ; ADD SP, 2
• BP (base pointer) can point to any element on the stack
                               EENG4005
                      PUSH Instruction
                         Example
                                                                         To
                                                                         address
                                                                         12FFF
     Register Array
                            PUSH BX                                   03800
AX
                                                           6A         037FF
BX          6AB3                    6AB3
                                                           B3         037FE
CX
DX                                  SH
                              PU
                                                 SH
                               re
                                               U
                            fo
                                             P
                       BX Be
                                      fter
                      SP
                                    A
SP         0800                SP
                                 BX
SS         0300                                                       03000
                                                      STACK segment
                                      EENG4005
                       POP Instruction
                          Example
                                                                     To
                                                                     address
                                                                     0FFFF
     Register Array
                                                                   01008
AX                               POP BX
                                                        39         01007
BX          392F                       392F
                                                        2F         01006
CX
DX
                            OP
                          rPe
                      BX Aft
                                          POP
                        SP
                                    fo re
                                  Be
SP         1006                 SP
                                  BX
SS         0000                                                    00000
                                                   STACK segment
                                        EENG4005
LOGICAL INSTRUCTIONS
            Assembler Directives
Assume
• Used to tell the assembler the name of the logical segment it
  should use for a specified segment.
• You must tell the assembler that what to assume for any
  segment you use in the program.
• Example, ASSUME: CODE tells the assembler that the
  instructions for the program are in segment named CODE.
DB – Defined Byte
• Used to declare a byte type variable or to set aside one or
  more locations of type byte in memory.
• Example, PRICES DB 49H, 98H, 29H: Declare array of 3 bytes
  named PRICES and initialize 3 bytes as shown.
     Assembler Directives(contd.)
DD – Define Double Word
• Used to declare a variable of type doubleword or to reserve a
  memory location which can be accessed as doubleword.
DQ – Define Quadword
• Used to tell the assembler to declare the variable as 4 words of
  storage in memory.
DT – Define Ten bytes
• Used to tell the assembler to declare the variable which is 10
  bytes in length or reserve 10 bytes of storage in memory.
DW – Define Word
• Used to tell the assembler to define a variable type as word or
  reserve word in memory.
     Assembler Directives(contd.)
END – End the program
• To tell the assembler to stop fetching the instruction and
  end the program execution.
• ENDP – it is used to end the procedure.
• ENDS – used to end the segment.
EQU – Equate
• Used to give name to some value or symbol.
EVEN – Align on Even memory address
• Tells the assembler to increment the location counter to the
  next even address if it is not already at an even address.
     Assembler Directives(contd.)
EXTRN
• Used to tell the assembler that the name or labels following
  the directive are in some other assembly module.
GLOBAL – Declares symbols as PUBLIC or EXTRN
• Used to make the symbol available to other modules.
• It can be used in place of EXTRN or PUBLIC keyword.
GROUP – Group related segment
• Used to tell the assembler to group the logical segments
  named after the directive into one logical segment.
• This allows the content of all the segments to be accessed
  from the same group.
     Assembler Directives(contd.)
INCLUDE – include source code from file
• Used to tell the assembler to insert a block of source code from
  the named file into the current source module. This shortens the
  source code.
LABEL
• Used to give the name to the current value in the location
  counter.
• The LABEL directive must be followed by a term which specifies
  the type you want associated with that name.
LENGTH
• Used to determine the number of items in some data such as
  string or array.
      Assembler Directives(contd.)
NAME
• Used to give a specific name to a module when the programs consisting of
  several modules.
OFFSET
• It is an operator which tells the assembler to determine the offset or
  displacement of named data item or procedure from the start of the
  segment which contains it.
ORG – Originate
• Tells the assembler to set the location counter value.
• Example, ORG 7000H sets the location counter value to point to 7000H
  location in memory.
• $ is often used to symbolically represent the value of the location counter. It
  is used with ORG to tell the assembler to change the location according to
  the current value in the location counter. E.g. ORG $+100.
     Assembler Directives(contd.)
PROC – Procedure
• Used to identify the start of the procedure.
PTR – Pointer
• Used to assign a specific type to a variable or a label.
• It is necessary to do this in any instruction where the type of
  the operand is not clear.
Public
• It is used to tell the assembler that the specified label or
  variable is accessible by other modules.
• This is useful in large programs which are generally written
  in modules.
8086 Interrupts
8086 Interrupts and Interrupt Responses
• The meaning of ‘interrupts’ is to break the sequence of
  operation
• While the CPU is executing a program, an interrupt
  breaks the normal sequence of execution of instructions,
  diverts its execution to some other program called
  Interrupt Service Routine (ISR)
• After executing ISR , the control is transferred back again
  to the main program
                       Contd..
• An 8086 interrupt can come from any one of the three
  sources
• One source is an external signal applied to the Non
  Maskable Interrupt (NMI) input pin or to the Interrupt
  (INTR) input pin
• An interrupt caused by a signal applied to one of these
  inputs is referred to as a hardware interrupt
                         Contd..
• A second source of an interrupt is execution of the
  Interrupt instruction, INT
• This is referred to as Software Interrupt
• The third source of an interrupt is some error condition
  produced in the 8086 by the execution of an instruction
• An example of this is the divide by zero error
Interrupt processing flow
   Main program
      Interrupt   N
         Req
       Accept     N
      Interrupt
  Get interrupt
     vector
   Jump to ISR
     Save PC
      Load PC
        Hardware Interrupts – Interrupt pins and timing
• x86 Interrupt Pins
  – INTR: Interrupt Request. Activated by a peripheral device to interrupt the processor.
         • Level triggered. Activated with a logic 1.
  – /INTA: Interrupt Acknowledge. Activated by the processor to inform the interrupting device
    the the interrupt request (INTR) is accepted.
         • Level triggered. Activated with a logic 0.
  – NMI: Non-Maskable Interrupt. Used for major system faults such as parity errors and power
    failures.
         • Edge triggered. Activated with a positive edge (0 to 1) transition.
         • Must remain at logic 1, until it is accepted by the processor.
         • Before the 0 to 1 transition, NMI must be at logic 0 for at least 2 clock cycles.
         • No need for interrupt acknowledgement.
INTR
INTA΄
D7-D0                                                                                  Vector
                         Contd..
                                       ISR procedure
Mainline Program       PUSH Flags
                                       PUSH registers
                      CLEAR IF , TF
                        PUSH CS                -
                        PUSH IP                -
                   FETCH ISR ADDRESS           -
                                        POP registers
                       POP IP
                                        IRET
                       POP CS
                      POP FLAGS
                        Contd..
1. It decrements SP by 2 and pushes the flag
   register on the stack
2. Disables INTR by clearing the IF
3. It resets the TF in the flag Register
4. It decrements SP by 2 and pushes CS on the
   stack
                    Contd..
5. It decrements SP by 2 and pushes IP on the
  stack
6.     Fetch the ISR address from the interrupt
     vector table
03FFH   Type 255 (Available)
03FCH
                               Available
                               Interrupts
        Type 32 (Available)      (224)
080H
        Type 31 (Reserved)
07FH
                               Reserved
                               Interrupts
                                  (27)
              Type 5
             Reserved
0014H
       Type 4 POINTER
010H    (OVERFLOW)
       Type 3 POINTER
00CH
       (BREAK POINT)
        Type 2 POINTER
008H
       (NON-MASKABLE)
       Type 1 POINTER
004H    (SINGLE STEP)
       Type 0 POINTER    CS base address
000H   (DIVIDE ERROR)    IP offset
      Interrupt Vector Table
INT Number    Physical Address
  INT 00                  00000
  INT 01                  00004
  INT 02                  00008
       :                    :
       :                    :
 INT FF                   003FC
                  Example
Find the physical address in the interrupt vector
    table associated with
a) INT 04H           b) INT FFH
    Solution:
a) 04H * 4 = 0FH
          Physical Address: 0000FH
b) FF * 4 = 3FCH
          Memory Address : 003FCH
                       Procedures
• Group of instructions that usually perform one task
• Reusable section of the software that is stored in memory
  once, but use as often as necessary
• The stack stores the return address whenever a procedure
  is called during the execution of the program
   – CALL pushes the address of the instruction following it on the
     stack
   – RET removes an address from the stack so the program returns
     to the instruction following the call
Passing parameters to and from procedures
Major ways of passing parameters to and from a
procedure:
• In register
• In dedicated memory locations accessed by
  name
• With pointers passed in registers
• With the stack
  Reentrant and Recursive procedures
• Reentrant procedures: The procedure which
 can be interrupted, used and “reentered”
 without losing or writing over anything.
• Recursive procedure: It is the procedure
 which call itself.
Writing and using Assembler
          Macros
                      Macros
• A macro inserts a block of statements at various
  points in a program during assembly
• Text substitutions made at compile time
  – NOT a procedure -- Code is literally dumped into
    program
  – Parameter names are substituted
  – Useful for tedious programming tasks
  – Instantiated within code segment.
                   Macros (cont.)
• General Format
        MACRO_NAME MACRO
      Param1,Param2,...,ParamN
              LOCAL MyLabel
              Your Code ...
              ... Param1 ...
               ...Param2 ...
              Your Code ...
              JMP MyLabel
              Your Code ...
  MyLabel:
             ... ParamN ...
              Your Code ...
              ENDM
    Macros vs Procedures (cont.)
• In the example the macro and procedure produce the
  same result
• The procedure definition generates code when the
  assembler encounters the PROC directive
• The macro does not emit any code when processing the
  statements between the MACRO and ENDM
   – Upon encountering Macro_1 in the mnemonic field, MASM
     assembles every statement between the MACRO and ENDM
     directives and emits that code to the output file
   – At run time, the processor executes these instructions without
     the call/ret overhead