Thursday, 29 December 2016

SET

Before we get into the details of Theory of Computations. The set is an essential topic which is required to be reviewed. So, let's have a quick review of this topic.

What is a Set?


- Set is a group of elements, having a common characteristic or property.
- Example: A = {1,2,3,4,5}
- Another way to specify a set is to give the properties that characterize elements of the sets.
- Example: A = {x | x is a positive integer less than or equal to 5}


Set Terminology


1. Belongs To (∈)

- x ∈ A means that x is an element of A.
- Using this notation we can specify the set {0,1,2,3,4} call it Z by writing
                   Z = {x | x ∈ N | x <= 5}
   Where N = set of natural numbers.


2. Subset

- Let A and B be two sets. Then A is a subset of B if every element of A is an element of B.
- It is represented by A⊆ B.
- If A is a subset of B and B is a subset of A, then A = B. Also if a is a subset of B but is not equal to B then it is represented as A⊂ B.


3. Universal Set

- The set U of all the elements we might ever consider in the discourse is called the universal set.


4. Complement

- Let A be a set, then the complement of A is the set consisting of all elements of the universal set that are not in A.
                 A' = {x | x∈ U \land \!\, x ∉ A}
- It is denoted by A'.
- Example:
               If U is a set of natural numbers and A = {1,2,3}, then
                 A' = {x | x∈ U \land \!\, x > 3}


Set Operations


Different operations can be performed on sets;


1. Union:

- If A and B are two sets, then the union of A and B is the set that contains all the elements that are in A and B including the ones in both A and B.
- Union is denoted by A∪B.
- Example:
   If A = {1,2} and B = {3, 4, 5} then A∪B = {1, 2, 3, 4, 5}.


2. Difference

- If A and B are two sets, then the difference of A from B is the set that contains all the elements that are in A but not in B.
- The difference is denoted by A-B.
- Example:
  If A= {1, 2, 3} and B = {3, 4, 5} then A-B = {1,2} also B-A = {4,5}.
- It can be seen that A-B ≠ B-A.


3. Intersection

- If A and B are two sets, then the intersection of A and B is the set that contains all the elements that are in both A and B.
- The intersection is denoted by A∩B.
- Example:
  If A= {1, 2, 3} and B = {3, 4, 5} then A∩B = {3}.


Disjoint Sets


- A and B sets are said to be disjoint if they contain no common element, i.e. A∩B = ∅, where ∅ is an empty set.
- Example:
  Let A= {1, 2, 3} and B = {4,5,6,7} then A and B are disjoint sets as  A∩B = ∅.


Some Standard Identities of Sets


Let A, B, C represent arbitrary sets and be the empty set and U be the Universal set, then


1. Commutative Laws:

- A∪B = B∪A
- A∩B = B∩A


2. Associative Laws:

- A∪(B∪C) = (A∪B)∪C
- A∩(B∩C) = (A∩B)∩C


3. Distributive Laws:

- A∩(B∪C) = (A∩B)∪(A∩C)
- A∪(B∩C) = (A∪B)∩(A∪C)


4. Idempotent Laws:

- A∪A = A
- A∩A = A


5. Absorption Laws:

- A∪(A∩B) = A
- A∩(A∪B) =  A

6. De Morgan Laws:

- (A∪B)' = A' ∩ B'
- (A ∩ B)' = A' U B'


7. Laws involving Complements:

- (A')' = A
- A ∩ A' = ∅
- A ∪ A' = U


8. Laws involving empty set:

- A ∪ ∅ = Λ
- A ∩ ∅ = ∅


9. Laws involving Universal set:

- A ∪ U = U
- A ∩ U = A

Tuesday, 27 December 2016

Smallest of two 8-bit numbers

Here is an example of Assembly Language code for 8086. This example illustrates how the smallest of two 8-bit number using Direct Addressing Mode is done.


CODE  SEGMENT                                            // start of the logical code segment
          ASSUME CS:CODE                               // this instruction will inform
                                                                           the assembler the assumed name of CS.              START: MOV  AL, 09H                       // this instruction will move                                                                                                the operand 09H to the accumulator.
              MOV BL, 03H                                    // this instruction will move
                                                                              the operand 03H to the Register B.
              CMP  AL, BL                                             // this instruction will compare the
                                                                              content of the Register B with the
                                                                              content of the accumulator, by the
                                                                              means of subtraction.
              JC NEXT                                            // this instruction will cause a jump
                                                                                to the next label if a carry is
                                                                                generated.
              MOV [2035H], BL                               // if carry is not generated the result
                                                                               from Register B would be moved
                                                                               to the memory location 2035H.
 NEXT: MOV [2035H], AL                               // if carry is generated the result
                                                                               from accumulator would be moved
                                                                               to the memory location 2035H.
CODE  ENDS                                                    // end of the logical code segment
     END START                                                // end of the program




NOTE:
While actually implementing this code the keyword next should be replaced with the address of the instruction.

Largest of two 8-bit numbers

Here is an example of Assembly Language code for 8086. This example illustrates how the largest of two 8-bit number using Direct Addressing Mode is done.


CODE  SEGMENT                                            // start of the logical code segment
          ASSUME CS:CODE                               // this instruction will inform
                                                                           the assembler the assumed name of CS.              START: MOV  AL, 09H                       // this instruction will move                                                                                                the operand 09H to the accumulator.
              MOV BL, 03H                                    // this instruction will move
                                                                              the operand 03H to the Register B.
              CMP  AL, BL                                             // this instruction will compare the
                                                                              content of the Register B with the
                                                                              content of the accumulator, by the
                                                                              means of subtraction.
              JNC NEXT                                            // this instruction will cause a jump
                                                                                to the next label if no carry is
                                                                                generated.
              MOV [2035H], BL                               // if carry is generated the result from
                                                                               Register B would be moved to the
                                                                               memory location 2035H.
 NEXT: MOV [2035H], AL                               // if carry is not generated the result
                                                                               from accumulator would be moved
                                                                               to the memory location 2035H.
CODE  ENDS                                                    // end of the logical code segment
     END START                                                // end of the program




NOTE:
While actually implementing this code the keyword next should be replaced with the address of the instruction.

1's Complement

Here is an example of Assembly Language code for 8086. This example illustrates how the 1's complement of an 8-bit number using Direct Addressing Mode is done.


CODE  SEGMENT                                            // start of the logical code segment
          ASSUME CS:CODE                               // this instruction will inform
                                                                           the assembler the assumed name of CS.              START: MOV  AL, 07H                       // this instruction will move                                                                                                the operand 07H to the accumulator.
              NOT  AL                                             // this instruction will flip the
                                                                              content of the accumulator by
                                                                              finding 1's complement of its single
                                                                               operand.
              MOV [2035H], AL                               // the result would be moved to
                                                                                the memory location 2035H.
CODE  ENDS                                                    // end of the logical code segment
     END START                                                // end of the program

2's Complement

Here is an example of Assembly Language code for 8086. This example illustrates how the 2's complement of an 8-bit number using Direct Addressing Mode is done.


CODE  SEGMENT                                            // start of the logical code segment
          ASSUME CS:CODE                               // this instruction will inform
                                                                           the assembler the assumed name of CS.              START: MOV  AL, 07H                       // this instruction will move                                                                                                the operand 07H to the accumulator.
              NEG  AL                                             // this instruction will negate the
                                                                              content of the accumulator by
                                                                              finding 2's complement of its single
                                                                               operand.
              MOV [2035H], AL                               // the result would be moved to
                                                                                the memory location 2035H.
CODE  ENDS                                                    // end of the logical code segment
     END START                                                // end of the program
          

Subtraction of two 8-bit numbers

Here is an example of Assembly Language code for 8085. This example illustrates how the subtraction of two 8 bit numbers using Direct Addressing Mode is done.


CODE  SEGMENT                                            // start of the logical code segment
          ASSUME CS:CODE                               // this instruction will inform
                                                                           the assembler the assumed name of CS.              START: MOV  AL, 09H                       // this instruction will move                                                                                                the operand 09H to the accumulator.
              MOV BL, 03H                                    // this instruction will move
                                                                              the operand 03H to the Register B.
              SUB  AL, BL                                             // this instruction will subtract the
                                                                              content of the Register B from the
                                                                              content of the accumulator.
              MOV [2035H], AL                               // the result would be moved to
                                                                                the memory location 2035H.
CODE  ENDS                                                    // end of the logical code segment
     END START                                                // end of the program
          

Addition of two 16-bit numbers with carry

Here is an example of Assembly Language code for 8086. This example illustrates how the addition of two 16 bit numbers using Direct Addressing Mode is done.


CODE  SEGMENT                                            // start of the logical code segment
          ASSUME CS:CODE                               // this instruction will inform
                                                                           the assembler the assumed name of CS.              START: MOV  AL, 99H                             // this instruction will move
                                                                              the operand 99H to the accumulator.
              MOV BL, 90H                                    // this instruction will move
                                                                              the operand 90H to the Register B.
              ADC  AL, BL                                             // this instruction will add the
                                                                              content of the Register B with the
                                                                              content of the accumulator, the result
                                                                               would be stored in AL and the carry
                                                                                bit would be stored in AH.
              MOV [2035H], AL                               // the result would be moved to
                                                                                the memory location 2035H.
CODE  ENDS                                                    // end of the logical code segment
     END START                                                // end of the program
          

Monday, 26 December 2016

Multiplication of two 8-bit numbers

Here is an example of Assembly Language code for 8086. This example illustrates how the multiplication of two 8 bit numbers using Direct Addressing Mode is done.


CODE  SEGMENT                                            // start of the logical code segment
          ASSUME CS:CODE                               // this instruction will inform
                                                                           the assembler the assumed name of CS.              START: MOV  AL, 02H                             // this instruction will move
                                                                              the operand 02H to the accumulator.
              MOV BL, 03H                                    // this instruction will move
                                                                              the operand 03H to the Register B.
              MUL  BL                                             // this instruction will multiply the
                                                                              content of the Register B with the
                                                                              content of the accumulator.
              INT  21H                                             // call the interrupt handler 0x21
                                                                              which is the DOS Function dispatcher.
CODE  ENDS                                                    // end of the logical code segment
     END START                                                // end of the program


Addition of two 8-bit numbers (Direct Addressing Mode)

Here is an example of Assembly Language code for 8085. This example illustrates how the addition of two 8 bit numbers using Direct Addressing Mode is done.

This can be done in two ways:

Way 1:

DATA  SEGMENT                                             // start of the logical data segment
          SUM  DB  ?                                              // this instruction directs the
                                                                             assembler to reserve one
                                                                             memory location and leave
                                                                             it uninitialized it for the                                                                                                             variable named SUM.
DATA  ENDS                                                     // end of the logical data segment


CODE  SEGMENT                                            // start of the logical code segment
          ASSUME CS:CODE, DS:DATA            // this instruction will inform
                                                                              the assembler that CS and                                                                                                             DS will be assumed names
                                                                              for the code and data logical
                                                                              segments respectively.
START: MOV  AX, [5000H]                              // this instruction will move
                                                                              the operand from the memory
                                                                              location 5000H to the accumulator.
              MOV  BX, [5001H]                              // this instruction will move
                                                                              the operand from the memory
                                                                              location 5001H to the Register B.
              ADD  AX, BX                                      // this instruction will add both
                                                                             numbers and will move the
                                                                             result to the accumulator
              MOV [1504H], AX                              // this instruction will move
                                                                              the result from the accumulator
                                                                              to the memory location 1504H.
              HLT                                                       // Halt
CODE  ENDS                                                    // end of the logical code segment
     END START                                                // end of the program


Way 2:

CODE  SEGMENT                                            // start of the logical code segment
          ASSUME CS:CODE                               // this instruction will inform
                                                                              the assembler the assumed name of CS.              START: MOV  AL, 05H                                   // this instruction will move
                                                                              the operand 05H to the accumulator.
              MOV BL, 03H                                    // this instruction will move
                                                                              the operand 03H to the Register B.
              ADD AL, BL                                      // this instruction will add both
                                                                             numbers and will move the
                                                                             result to the accumulator
              MOV [2000H], AL                              // this instruction will move
                                                                              the result from the accumulator
                                                                              to the memory location 2000H.
CODE  ENDS                                                    // end of the logical code segment
     END START                                                // end of the program

Sunday, 25 December 2016

The 8086 String Instruction

1. MOVS:

- MOVS instruction is used for moving a string of ASCII characters from one place in the memory to the another place.

2. MOVSB:

- MOVSB instruction will perform all the actions in repeat unit loop.
- The MOVSB instruction will copy a byte from the location pointed to by the Direct Index Register.
- It will then automatically increment SI to point to the next source location.
- If you add a special prefix called the repeat prefix in front of the MOVSB instruction, the MOVSB instruction will be repeated and CX decremented until CX is counted down to zero.
- The source index register, SI must contain the offset of the start of the source string and the start of the destination location.
- Also, the number of string elements to be moved must be loaded into the CX register.
- If the direction flag is cleared with CLD instruction, then the pointers in SI and DI will automatically be incremented after each string operation. If the direction flag is set with an STD instruction, then the pointers in SI and DI will be automatically decremented after each string operation.
- It is necessary to initialize the extra segment register, for string instructions an offset in DI is added to the segment base represented by the number in the extra segment register to produce a physical address.
- If DS and ES are initialized with the same value then SI and DI will point to locations in the same segment.

3. MOVSW:

- The MOVSB instruction can be used to move a string of words depending on the state of the direction flag, SI and DI will automatically be incremented or decremented by 2 after each word move.

4. MOVSB: 

- A single MOVSB  instruction can cause the 8086 to move up to 65,536 bytes from one location in memory to another.

'$':

- A '$' is used to symbolically represent the current value of the location at any point.
- This trick with the $ sign allows you to load the number of string elements in CX symbolically, rather than having to manually count the number. This trick has the further advantage that if the password is changed and the program reassembled, the instruction that loads CX with the string length will automatically use the new value.

Jumps, Flags and Conditional Jumps


1. Flags

- Flags indicate whether some condition is present or not.


2. Jump

- Jump instructions are used to tell the computer the address to fetch its next instruction from.
- There are two types of jump instructions:
                      a. Conditional Jump Instruction
                      b. Unconditional Jump Instruction


a. Conditional Jump Instruction

- Conditional jump evaluates the state of specified flag to determine whether to fetch its next instruction from the jump destination location or to fetch its next instruction from the next sequential memory location.

Fig. 1. Flow chart showing the working of conditional jump

b. Unconditional Jump Instruction

- When the 8086 executes a jump(JMP) instruction, it loads a new number into the instruction pointer register and in some cases, it also loads a new number into the code segment register.

- If the jump destination is in the same code segment, the 8086 only has to change the contents of the instruction pointer. This type if a jump is referred to as a near or intrasegment jump.

 If the jump destination is in the different code segment, the 8086 has to change the contents of the instruction pointer and the code segment to make the jump. This type of jump is referred to as far jump or intersegment jump.

- Near and far jumps are further described as either
   i) Direct
      - If the destination address for the jump is specified directly as a part of the instruction, then the jump is described as direct. It is further classified as direct near jump and direct far jump.
  ii) Indirect
      - If the destination address for the jump is contained in a register or memory location, the jump is referred to as an indirect jump.

The direct near and Short type jump instructions

- A near-type jump instruction can cause the next instruction to be fetched from anywhere in the current code segment.

- A 16 bit signed displacement means that the jump can be to a location anywhere from +32,767 to -32,768 bytes from the current instruction pointer location.

- A positive displacement is an ahead jump. and a negative displacement is a backward jump.

- A special case of the direct near type jump instruction is the direct short type jump.

- If the destination for the jump is within a displacement range of +127 to -127 bytes from the current instruction pointer location, the destination can be reached with just an 8-bit displacement.

Saturday, 24 December 2016

Assembly Language Program Development Tools


1. Editor

- An editor is a program which allows you to create a file containing the assembly language statements for your program.
Example: PC-Write, Wordstar.

- As you type in your program, the editor stores the ASCII codes for the letters and numbers in successive RAM locations.

- When you have typed in all your program, you then save the file on the hard disk. This file is called source file and the extension is .asm.


2. Assembler

- An assembler program is used to translate the assembly language mnemonics for instructions to corresponding binary codes. When you run the assembler, it reads the source file of your program from the disk where you have saved it after editing.

- On the first pass through the source program, the assembler determines the displacement of named data items, the offset of labels, etc. and puts this information in a symbol table.

- On the second pass through the source program, the assembler produces the binary code for each instruction and inserts the offsets, etc. that it calculated during the first pass.

- The assembler generates 2 files on the floppy disk or hard disk. The first file is called object file (.obj).

- The second file generated by assembler is called the assembler list file and is given extension (.lst).


3. Linker

- A linker is a program used to join several object files into one large object file.

- The linker produces a link file which contains the binary codes for all the combined modules. The linker also produces a link map file which contains the address information about the linked files (.exe).


4. Locator 

- A locator is a program used to assign the specific address of where the segments of object code are to be loaded into memory.

- A locator program called EXE2BIN comes with the IBM PC Disk Operating System (DOS). EXE2BIN converts a .exe file to a .bin file which has physical addresses.


5. Debugger

- A debugger is a program which allows you to load your object code program into system memory, execute the program and troubleshoot or debug it.

- The debugger allows you to look at the contents of registers and memory locations after your program runs.

- It allows you to change the contents of registers and memory locations and re-run the program.

- Some debuggers allow you to stop execution after each instruction so that you can check or alter after each register contents.

- A debugger also allows you to set a breakpoint at any point in your program. If you insert a breakpoint at any point in your program, the debugger will run the program up to the instruction where you put the breakpoint and then stop the execution.


6. Emulator

- An emulator is a mixture of hardware and software.

- It is used to test and debug the hardware and software of an external system, such as the prototype of a microprocessor based instrument. Part of the hardware of an emulator is a multiwire cable which connects the host system to the system being developed.

Friday, 18 November 2016

Addition of two 8 bit numbers (Immediate Addressing Mode)

Here is an example of Assembly Language code for 8085. This example illustrates how addition of two 8 bit numbers using Immediate Addressing Mode is done.

DATA SEGMENT                                             // start of the logical segment
          FI_NO  DB  05H                                     // this instruction directs the
                                                                              assembler to reserve one
                                                                              memory location and initialize
                                                                              it with the value of 05 in
                                                                              hexadecimal for the variable
                                                                              named FI_NO.
          SE_NO  DB 03H
          AV_SUM  DB  ?                                     // this instruction directs the
                                                                             assembler to reserve one
                                                                             memory location and leave
                                                                             it uninitialized it for the                                                                                                             variable named AV_SUM.
DATA ENDS                                                     // end of the logical segment

CODE SEGMENT                                            // start of the logical segment
         ASSUME  CS:CODE  DS:DATA            // this instruction will inform
                                                                              the assembler that CS and                                                                                                             DS will be assumed names
                                                                              for the code and data logical
                                                                              segments respectively.
START: MOV  AX  DATA                               // data segment is initiallised
              MOV DS  AX                                      
              MOV AL  FI_NO                               // this instruction will get the
                                                                             first number in accumulator
              MOV BL  SE_NO                              // this instruction will get the
                                                                             second number in B register.
              ADD AL  BL                                      // this instruction will add both
                                                                             numbers and will move the
                                                                             result to the accumulator
              MOV AV_SUM  AL                           // this instruction will move
                                                                              the result from the accumulator
                                                                              to the memory location which
                                                                              was left uninitialized with the
                                                                              variable named AV_SUM.
CODE ENDS                                                    // end of the logical segment
     END START                                                // end of the program

Tuesday, 11 October 2016

Constructing the Machine Codes for 8086 Instructions

- Since there are such a large number of possible codes for the 8086 instructions, it is impractical to list them all in a simple table.

- Instead, we use a template for each basic instruction type and fill in bits within this template to indicate the desired addressing mode, data type, etc.

- In other words we build up the instruction code on a bit by bit basis.


MOV instruction coding format

- At least 2 code bytes are required for the instruction.

- The upper 6 bits of the first byte are an opcode which indicates the general type of instruction.

- The 'W' bit in first word is used to indicate whether a byte of a word is being moved. If byte, make W=0 else if word make W=1.

- In this instruction one operand must always be a register, so 3 bits in the second byte are used to indicate which register is involved.

- The 'D' bit in the first byte of the instruction is used to indicate whether the data is being moved to the register identified in 'REG' field of the second byte or from that register.

- If the instruction is moving data to the register identified in the REG field, make D=1.

- If the instruction is moving data from that register, make D=0.

- The 2-bit field labelled 'MOD' and the 3-bit field labeled 'R/M' in the second byte of the instruction code are used to specify the desired addressing mode for the other operand.


Sunday, 18 September 2016

Assembler Directives

Assembler

- An assembler is a program used to convert an assembly language program into the equivalent machine code modules which may further be converted to executable codes.

- The assembler decides the address of each label and substitutes the values for each of the constants and variables,

- It then forms the machine code for the mnemonics and data in the assembly language program.

- While doing these things, the assembler may find out syntax errors.


Assembler Directives

- For completing all these tasks(mentioned above), an assembler needs some hints from the programmer, i.e. the required storage for a particular constant or variable, logical names of the segments, types of the different routines and modules, end of file, etc.

- These types of hints are given to the assembler using some predefined alphabetical strings called assembler directives.

_________________________________________________________________________________

Assembler Directives help the assembler to correctly understand the assembly language programs to prepare the codes.
_________________________________________________________________________________

Now, lets learn about some of these directives;

1. Define Byte (DB):
- The DB directive is used to reserve byte or bytes of memory locations in the available memory.
   Example:
                  LIST DB 01H, 02H, 03H, 04H
   This statement directs the assembler to reserve four memory locations for a list named LIST and          initialize them with the above specified values.

                  MESSAGE DB 'GOOD MORNING'
   This makes the assembler reserve the number of bytes of memory equal to the number of characters    in the string named MESSAGE and initialize those locations by the ASCII equivalent of these            characters.

                  VALUE DB 50H
   This statement directs the assembler to reserve 50H memory bytes and leave them uninitialised for      the variable named VALUE. 

2. Define Word (DW):
- Used to reserve the number of memory words (16-bit) in the available memory.
   Example:
                  WORDS DW 1234H, 4567H, 78ABH, 045CH
                   WDATA DW 5 DUP (6666H)
   Reserve five words i.e. 10 bytes of memory for a word label WDATA and initialise all the word          locations with 6666H.

3. Define Quad Word (DQ):
- This directive is used to direct the assembler to reserve four words (8 bytes) of memory for the            specified variable and may initialize it with the specified values.

4. Define Ten Bytes (DT)

5. Double Word (DD)

6. Assume logical segment name (ASSUME):
- Used to inform the assembler the names of the logical segments to be assumed for different                  segments used in the program.
   Example:
                  ASSUME DS:DATA

7. End of Program (END):
- Marks the end of assembly language program.
- When the assembler comes across this END directive, it ignores the source lines available later on.

8. End of Procedure (ENDP):
- To mark end of a particular procedure, the name of procedure may appear as a prefix with the              directive ENDP.
   Example: PROCEDURE STAR
                             .
                             .
                             .
                    STAR ENDP

9. End of Segment (ENDS):
- Marks end of a logical segment.
   Example: DATA SEGMENT
                          .
                          .
                          .
                    DATA ENDS

- The SEGMENT and ENDS directives are used to identify a group of data items or group of instructions that you want to be put together in a particular segment.

- A group of data statements or a group of instruction statements contained between SEGMENT and ENDS directives is called a LOGICAL SEGMENT.

- A logical is not usually given a physical starting address when it is declared. After the program is assembled and perhaps linked with other assembled program modules, it is then assigned the physical address where it will be loaded in memory to be run.

Saturday, 27 August 2016

Data Addressing Modes of 8086

     1. Immediate Addressing Mode
In this mode, 8 or 16 bit data(operand) can be specified as part of the instruction.
____________________________________
|      OP Code        |      Immediate Operand   |
|______________|_____________________|

Example: MOV CL, 03H
              = Moves the 8 bit data  03H into CL

                MOV DX, 0525H
              = Moves the 16 bit data 0525H into DX

A constant value such as "VALUE" can be defined by the assembler EQUATE directive such as
  
              VALUE EQU 35H
Example: MOV BH, VALUE
             = Used to load 35H into BH


     2. Register Addressing Mode
The operand to be accessed is specified as residing in an internal register of 8086.

Example: MOV DX, CX
             = Move 16 bit content of CX into DX

                MOV CL, DL
             = moves 8 bit content of DL into CL


     3. Direct Addressing Mode
The instruction Opcode is followed by an Effective Address(EA), this effective address is directly used as the 16 bit offset of the storage location of the operand from the location specified by the current value in the selected segment register.

The default segment is always DS.

PA= DS:EA
Segment override prefix (SOP)
PA =  _    _
          | CS
          | DS |      :         [ Direct Address ]
          |  SS |
          |  ES |
          --    -- 


     4. Register Indirect Addressing Mode
The EA is specified in either pointer(BX) register or an index (SI or DI) register. The 20 bit physical address is computed using DS and EA.

Example: MOV [DI], BX
             If [DS] = 5004, [DI]=0020, [BX]=2456, PA=[50060]
             The content of BX is moved to memory locations 50060H and 50061H.

               MOV AL, [START+BX]
              The 8 bit content of this memory location is moved to AL.
               The kind of addressing used in this particular example is known as Based Addressing Mode,                since BX pointer is used. 
               
               Indexed Addressing Mode: MOV BH, START [SI]
                                                         Here Physical Address(PA) will be [START]+[SI]+[DS]

               Based Indexed Addressing Mode: MOV ALPHA[SI][BX], CL
                                                         Here PA will be ALPHA+[SI]+[BX]+[DS]


     5. Input-Output Mode (Direct):
Here port number is an 8-bit immediate operand.

Example: OUT 05H, AL
                Outputs [AL] to 8-bit port 05H.

          Input-Output (Indirect):
The port number is taken from DX.

Example: IN AL, DX
              If [DX] = 5040,
              8 bit content by port 5040 is moved to AL.


     6. Relative Addressing Mode
Example: JNC START
             If CY=0, then PC is loaded with current PC contents plus 8 bt signed value of START,                        otherwise the next instruction is executed.


     7. Implied Addressing Mode
Instruction using this operating mode have no operands.

Example: CLC which clears the carry flag.

Tuesday, 16 August 2016

Importance Of Segmentation

-The segment:offset scheme requires only a 16bit number to represent the base address for a segment and only 16bit offset to access any location in a segment.

- This means that the 8086 has to manipulate and store only 16 bit quantities instead of 20bit quantities.
This makes for an easier interface with 8 and 16bit wide memory boards and with the 16 bit registers in the 8086.

- Each time the CPU switches from one user's program to the next, it must access a new section of data. Segmentation makes this switching quite easy.

Monday, 15 August 2016

The Execution Unit

8086 Internal Architecture

Control Circuitry, Instruction Decoder And ALU

- The Execution Unit (EU) contains circuitry which directs internal operations.

- A decoder in the EU translates instructions fetched from memory into a series of actions which the EU carries out.

- The EU has AND, OR, XOR, increment, decrement, complement or shift binary numbers.

Flag Register

- A flag is a flip-flop which indicates some condition produced by the EU of an instruction or controls certain operations of the EU.

- A 16-bit flag register in the EU contains 9 active flags.

- 6 of 9 flags are used to indicate some condition produced by an instruction.

- The six additional flags in this group are:-
  + Carry Flag (CF)
  + Parity Flag (PF)
  + Auxiliary Carry Flag (AF)
  + Zero Flag (ZF)
  + Sign Flag (SF)
  + Overflow flag (OF)

- The 3 other flags in the flag register are used to control certain operations of the processor.
These flags are different from six conditional flags described above in the way they are set or reset.

- The six conditional flags are set or reset by the EU on the basis of the results of some arithmetic or logic operation.

- The control flag is deliberately set or reset with specific instructions you put in your program.

- The 3 control flags are:-
   + Trap Flag (TF): which is used for single stepping through a program.
   + Intrupt Flag (IF): which is used to allow or prohibit the intruption of a program; and
   + Direction Flag (DF): which is used with string instructions.

General Purpose Registers

- EU has 8 general purpose registers, labelled AH, AL, BH, BL, CH, CL, DH and DL.

- These registers can be used individually for storage of 8-bit data.

- AL register is also called ACCUMULATOR.

- Certain pairs of general purpose registers can be used together to store 16-bit data words. Eg: AH & AL, BH & BL, CH & CL, DH & DL.

- AH & AL pair is reffered as AX register. Similarly, BH & BL pair is reffered as BX; CH & CL pair is reffered as CX; DH & DL is reffered as DX register.

- The 8086 general purpose register set was designed this way so that many programs written for 8080 and 8085 microprocessor could easily be translated to run on the 8086.

- The advantage of using internal registers for the temporary storage of data is that, since data is already in EU, it can be accessed much more quickly than it could be accessed from external memory.

8086 Flag Register Format


- Carry Flag- Set(1) by carry out of Most Significant Bit.

- Parity flag- Parity is a term used to indicate whether a binary word has an even number of 1(s) or an odd number of 1(s). A binary number with an even number of 1(s) is said to have even parity.

- Auxiliary Flag- It has significance in BCD addition or BCD addition or BCD subtraction. If a carry is produced when the least significant nibbles of 2 bytes are added, the flag will be Set. If subtraction of least significant nibbles requires a borrow then the auxiliary carry/borrow flag will be set.

- Zero Flag- It is set to 1 if the result of an arithmetic/logic operation is zero.

- Sign Flag- The sign flag is a copy of MSB of the destination byte or the destination word. Here, 0 is for positive and 1 is for negative. This flag can be used to determine whether an operand has been decremented beyond zero.

- Trap Flag- single step trap flag.

- Interrupt Enable Flag- Enabled/Disabled based on the Intrupt acceptation state.

- Direction Flag- Used for Strings

- Overflow Flag- This flag will be set if the result of a signed operation is too large to fit in the number of bits available to represent it.

The Bus Interface Unit

8086 Internal Architecture

The Queue

- While the EU is decoding an instruction or executing an instruction which does not require use of the buses, the BIU fetches upto 6 instruction byte for the following instructions.
- The BIU stores these pre-fetched bytes in a first in first out register set called Queue.
- When the EU is ready for its next instruction, it simply reads the instruction byte(s) for the instruction from the queue in the BIU.
- This is much faster than sending out an address to the system memory and waiting for memory to send back the next instruction byte or bytes.
- Fetching the next instruction while the current instruction executes is called Pipelining.


Segment Registers
- The 8086 BIU sends out 20 bit addresses, so it can address any of 2^20 bytes in memory.
- However, at any given time the 8086 works with only 4 segments within this 1 Mb range.
- Four segment registers in the BIU are used to hold the upper 16 bits of the starting address of four memory segments that the 8086 is working with at a particular time.
- The four segment registers are:-
   + Code Segment (CS) register:
                                                    The register contains the initial address of the code segment. The address plus the offset value contained in the Instruction Pointer(IP) indicates the address of an instruction to be fetched for execution.

   + Stack Segment (SS) register:
                                                    The initial address of the stack segment plus value in the Stack Pointer(SP) is used for stack operations.

   + Extra Segment (ES) register:
                                                    ES is used by some string operations. Contains initial address of the extra segment. String instructions always use ES and Destination Index(DI) registers to calculate the physical address for the destination.

   + Data Segment (DS) register:
                                                  The initial address of the current data segment address plus offset value in instruction causes a reference to a specific location in the data segment.


- The BIU always inserts zeros for the lowest 4 bits (nibble) of the 20bit starting address for segment.
For example: if the Code Segment register contains 348AH, then the code segment will start at 348A0H.
- This constraint was put on the location of Segment so that it is only necessary to store and manipulate 16 bit numbers when working with the starting address of a segment.
- The part of a segment starting address stored in a segment register is often called the Segment Base.
- A stack is a section of memory set aside to store address and data while a subprogram executes.
- The extra Segment register and the data Segment register are used to hold the upper 16 bits of the starting address of 2 memory segments that are used for data.

One way four 64Kb segments might be positioned within the 1 Mb address space of an 8086.


Instruction Pointer

- The instruction pointer register holds the 16-bit address, or offset of the next code byte within this code segment.
- The value contained in the IP is reffered to as an offset because this value must be offset from(added to) the segment base address in CS to produce the required 20bit physical address sent out by the BIU.


Addition of IP to CS to produce the physical address of the code byte

- An alternative way of representing a 20bit physical address is the segment base:offset form. For the address of a code byte, the format for this alternative form will be CS:IP. Eg: 348A:4214


Stack Segment Register and Stack Pointer Register

- The 8086 allows you to set aside an entire 64Kb segment as stack. The upper 16 bits of the starting address for this segment are kept in the stack segment register.
- The Stack Pointer(SP) register in the execution unit holds the 16bit offset from the start of the segment to the memory location where a word was most recently stored on the stack.
- The memory location where a word was most recently stored is called the top of the stack.

Addition of SS and SP to produce the physical address of the top of the stack

- The physical address can also be represented as SS:SP i.e. 5000:FFE0H.



Execution Unit

Pointer and Index Registers in The Execution Unit
- In addition to the stack pointer register(SP), the EU contains a 16 bit base pointer(BP) register.
- It also contains 16bit source index(SI) register and a 16 bit destination index (DI) register.
- Those 3 register can be used for temporary storage of data just as the general purpose registers.
- However, their main purpose(use) is to hold the 16bit offset of a data word in one of the segments.
- The physical address of the data in memory is given by adding SI to DS.