1344 Alp Of 8086

6,651 views

Published on

visit: www.techbed.blogspot.com

0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
6,651
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
317
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

1344 Alp Of 8086

  1. 1. Unit – 6 (Introduction to 8086) Reference: D.V. Hall (Chapters – 4, 5) Prepared and Presented by - Rajvir Singh
  2. 2. Topics <ul><li>Simple sequence programs </li></ul><ul><li>Jumps, Flags and Conditional Jumps </li></ul><ul><li>Programming Structures </li></ul><ul><ul><li>Decision making and Loops in 8086 </li></ul></ul><ul><li>Instruction Timing and Delay Loops </li></ul><ul><li>Strings </li></ul><ul><li>Procedures </li></ul><ul><li>Macros </li></ul>
  3. 3. 1. Simple Sequence Programs <ul><li>Instructions are executed one after another in a sequence </li></ul>Instruction1 Instruction2 Instruction3
  4. 4. Example Simple Sequence Programs <ul><li>Two’s Complement of a number </li></ul><ul><li>XOR using AND, OR and NOT operations </li></ul><ul><li>Multiplication and Division of Numbers </li></ul><ul><li>Finding the Average of two numbers </li></ul><ul><li>Conversion of two ASCII codes to packed BCD </li></ul>
  5. 5. 2. Jumps, Flags & Conditional Jumps <ul><li>Unconditional jump instruction </li></ul><ul><ul><li>8086 always jumps to the specified jump destination </li></ul></ul><ul><li>Conditional jump instruction </li></ul><ul><ul><li>8086 determines the state of the specified flag </li></ul></ul><ul><ul><li>Jump may or may not be taken to the specified jump destination </li></ul></ul>
  6. 6. Unconditional Jumps <ul><li>Mnemonic – JMP </li></ul><ul><li>E.g. JMP NEXT </li></ul><ul><li>How 8086 calculates jump address? </li></ul><ul><ul><li>CS + IP </li></ul></ul><ul><li>8086 executes a jump instruction by </li></ul><ul><ul><li>loading new number into IP register </li></ul></ul><ul><ul><li>In some cases it may load a new value into CS as well </li></ul></ul>Memory Segmentation
  7. 7. Unconditional Near Jumps <ul><li>Near Jump </li></ul><ul><ul><li>The jump destination is in the same code segment </li></ul></ul><ul><ul><li>To execute the jump, only the contents of Instruction Pointer (IP) register needs to be changed </li></ul></ul>Code Segment CS: 3000H IP: 0000H JMP NEXT INC BX CS: 3000H IP: 0010H NEXT: CS: 3000H IP: 0020H
  8. 8. Unconditional Far Jumps <ul><li>Far Jump </li></ul><ul><ul><li>The jump destination is in a different code segment </li></ul></ul><ul><ul><li>To execute the jump, 8086 has to change the contents of Code Segment (CS) register and IP register </li></ul></ul>Code Segment A CS: 3000H IP: 0000H JMP NEXT INC BX CS: 3000H IP: 0010H NEXT: CS: 6000H IP: 0050H Code Segment B
  9. 9. Unconditional Direct Jumps <ul><li>destination address for the jump is specified directly as part of the instruction </li></ul><ul><li>E.g. </li></ul><ul><ul><ul><li>JMP NEXT </li></ul></ul></ul><ul><li>The direct jump can be a near or a far jump </li></ul><ul><li>Direct near-type jump </li></ul><ul><ul><li>within a displacement range of +32767 to -32768 bytes from the current IP location </li></ul></ul><ul><li>Direct short-type jump </li></ul><ul><ul><li>within a displacement range of +127 to -128 bytes from the current IP location </li></ul></ul>
  10. 10. Unconditional Indirect Jumps <ul><li>destination address for the jump is contained in a register or memory location </li></ul><ul><li>E.g. </li></ul><ul><ul><ul><li>JMP BX </li></ul></ul></ul><ul><li>The indirect jump can be a near or a far jump </li></ul>
  11. 11. Summary of Unconditional Jump Types <ul><li>Direct Near-type (within segment) jump </li></ul><ul><ul><li>Displacement range +32767 to -32768 bytes relative to Instruction Pointer </li></ul></ul><ul><li>Direct Short-type (within segment) jump </li></ul><ul><ul><li>Displacement range +127 to -128 bytes relative to Instruction Pointer </li></ul></ul><ul><li>Direct Far-type (inter-segment) jump </li></ul><ul><li>Indirect Near-type (within segment) jump </li></ul><ul><li>Indirect Far-type (inter-segment) jump </li></ul>
  12. 12. 8086 Conditional Flags <ul><li>8086 has six conditional flags </li></ul><ul><ul><li>Carry Flag (CF) </li></ul></ul><ul><ul><li>Parity flag (PF) </li></ul></ul><ul><ul><li>Auxiliary-carry flag (AF) </li></ul></ul><ul><ul><li>Zero flag (ZF) </li></ul></ul><ul><ul><li>Sign flag (SF) </li></ul></ul><ul><ul><li>Overflow flag (OF) </li></ul></ul>
  13. 13. Carry Flag <ul><li>Is SET </li></ul><ul><ul><li>if result of 8-bit addition is greater than 8-bits </li></ul></ul><ul><ul><li>if result of 16-bit addition is greater than 16-bits </li></ul></ul><ul><ul><li>if there is a borrow during subtraction </li></ul></ul><ul><li>Used in compare instruction of 8086 </li></ul>
  14. 14. Carry Flag <ul><li>For e.g. </li></ul><ul><li>CMP BX, CX </li></ul><ul><li>----------------------------------------------------- </li></ul><ul><li>CONDITION CF ZF </li></ul><ul><li>----------------------------------------------------- </li></ul><ul><li>CX > BX 1 0 </li></ul><ul><li>CX < BX 0 0 </li></ul><ul><li>CX = BX 0 1 </li></ul><ul><li>----------------------------------------------------- </li></ul>
  15. 15. Parity Flag <ul><li>SET for EVEN parity </li></ul><ul><li>If the lower 8-bits of the destination operand has an even no. of 1’s </li></ul>
  16. 16. Auxiliary Carry Flag <ul><li>Used in BCD addition or subtraction </li></ul><ul><li>Is SET </li></ul><ul><ul><li>If a carry is produced when the least significant nibbles of 2 bytes are added </li></ul></ul>
  17. 17. Zero Flag <ul><li>Is SET </li></ul><ul><ul><li>If the result of a arithmetic or logic operation is zero </li></ul></ul><ul><ul><li>For e.g. </li></ul></ul><ul><ul><li>SUB BX, CX </li></ul></ul><ul><ul><li>AND BX, CX </li></ul></ul><ul><ul><li>CMP BX, CX </li></ul></ul><ul><ul><li>DEC BX </li></ul></ul>
  18. 18. Sign Flag <ul><li>8086 uses 2’s complement sign-and-magnitude form to represent signed numbers </li></ul><ul><li>The most significant bit of the byte is used as a ‘sign bit’ </li></ul><ul><ul><li>A ‘0’ in this bit position indicates a positive no. </li></ul></ul><ul><ul><li>A ‘1’ indicates a negative no. </li></ul></ul><ul><li>Sign flag is SET if ‘sign bit’ is ‘1’ </li></ul>
  19. 19. Overflow Flag <ul><li>Is SET </li></ul><ul><ul><li>If the result of a signed operation is larger than the number of bits available to represent it </li></ul></ul><ul><ul><li>For e.g. </li></ul></ul><ul><ul><li>Addition of signed numbers </li></ul></ul><ul><ul><li> 01110101 (+117) </li></ul></ul><ul><ul><li>+ 00110111 (+55) </li></ul></ul><ul><ul><li> 10101100 (+172) </li></ul></ul><ul><ul><li>The result (+172) is larger than the range of 8-bit signed numbers (-128 to +127) </li></ul></ul>
  20. 20. Conditional Jumps <ul><li>8086 determines the state of the specified flag </li></ul><ul><li>Jump may or may not be taken to the specified jump destination </li></ul><ul><li>All conditional jumps are short jumps </li></ul><ul><ul><li>i.e specified jump destination address must be in the range of -128 to +127 bytes from the jump instruction </li></ul></ul><ul><li>Conditional jump instructions are used to implement decision making & iteration-based programming structures </li></ul>
  21. 21. 8086 Conditional Jump Instructions For e.g. CMP BL, DH ; compare BL and DH JA HEATER_OFF ;jump if BL is above DH ‘ above’ & ‘below refer to comparison of two unsigned values ‘ greater’ & ‘less’ refer to comparison of two signed values Equal/Zero JE/JZ CF = 0 JNC Greater/not less or equal JG/JNLE Above/not below or equal JA/JNBE Jump IF MNEMONIC
  22. 22. 3. Programming Structures <ul><li>Decision making Structures </li></ul><ul><ul><li>Simple IF-THEN </li></ul></ul><ul><ul><li>IF-THEN-ELSE </li></ul></ul><ul><ul><li>Multiple IF-THEN-ELSE </li></ul></ul><ul><li>Iteration-based Structures </li></ul><ul><ul><li>WHILE-DO </li></ul></ul><ul><ul><li>REPEAT-UNTIL </li></ul></ul>
  23. 23. IF-THEN Structure <ul><li>Format </li></ul><ul><ul><li>IF condition THEN </li></ul></ul><ul><ul><li>action </li></ul></ul><ul><li>E.g. </li></ul><ul><ul><li>IF carry THEN </li></ul></ul><ul><ul><li>increment register AH </li></ul></ul>
  24. 24. A program using IF-THEN <ul><li>;a program to add two 8-bit values with carry </li></ul><ul><li>MOV AH, 00H ;clear register AH </li></ul><ul><li>MOV BL, 99H </li></ul><ul><li>MOV BH, 99H </li></ul><ul><li>ADD BH, BL ;BH = BH + BL </li></ul><ul><li>MOV AL, BH ;store LSB in AL </li></ul><ul><li>JNC END ;jump if CF = 0 </li></ul><ul><li>INC AH ;store carry in AH </li></ul><ul><li>END: INT 3 ;end of program </li></ul><ul><li>Result: 99H + 99H = 0132H </li></ul><ul><li>AH = 01H AL = 32H </li></ul>
  25. 25. IF-THEN-ELSE Structure <ul><li>Format </li></ul><ul><ul><li>IF condition THEN </li></ul></ul><ul><ul><li>action1 </li></ul></ul><ul><ul><li>ELSE </li></ul></ul><ul><ul><li>action2 </li></ul></ul><ul><li>E.g. </li></ul><ul><ul><li>IF B > C THEN </li></ul></ul><ul><ul><li>A = B </li></ul></ul><ul><ul><li>ELSE </li></ul></ul><ul><ul><li>A = C </li></ul></ul>
  26. 26. A program using IF-THEN-ELSE <ul><li>;a program to determine the largest of two 16-bit values </li></ul><ul><li>;store the largest value in register AX </li></ul><ul><li>MOV BX, 9901H </li></ul><ul><li>MOV CX, 9902H </li></ul><ul><li>CMP BX, CX ;compare BX and CX </li></ul><ul><li>JA COPY_B ;jump if BX is above CX </li></ul><ul><li>COPY_C: MOV AX, CX ;store CX in AX </li></ul><ul><li>JMP END ;jump to label END </li></ul><ul><li>COPY_B: MOV AX, BX ;store BX in AX </li></ul><ul><li>END: INT 3 ;end of program </li></ul><ul><li>Result: AX = CX = 9902H </li></ul>
  27. 27. Multiple IF-THEN-ELSE Structure <ul><li>Format </li></ul><ul><ul><li>IF condition1 THEN </li></ul></ul><ul><ul><li>action1 </li></ul></ul><ul><ul><li>ELSE IF condition2 THEN </li></ul></ul><ul><ul><li>action2 </li></ul></ul><ul><ul><li>ELSE </li></ul></ul><ul><ul><li>action3 </li></ul></ul><ul><li>E.g. </li></ul><ul><ul><li>IF B = 3 THEN </li></ul></ul><ul><ul><li>A = 33H </li></ul></ul><ul><ul><li>ELSE IF B =2 THEN </li></ul></ul><ul><ul><li>A = 32H </li></ul></ul><ul><ul><li>ELSE </li></ul></ul><ul><ul><li>A = 31H </li></ul></ul>
  28. 28. A program using Multiple IF-THEN-ELSE Structure <ul><li>;a program to read a keypress from keyboard and display </li></ul><ul><li>;it on a ASCII display </li></ul><ul><li>IN AL, 80H ;read from keyboard </li></ul><ul><li>MOV BL, AL ;copy data to register BL </li></ul><ul><li>CHK_3:CMP BL, 03H ;compare BL with 3 </li></ul><ul><li>JNE CHK_2 ;jump if BL not equals 3 </li></ul><ul><li>MOV AL, 33H ;store ASCII code for ‘3’ in AL </li></ul><ul><li>JMP DISP ;display AL on ASCII display </li></ul><ul><li>CHK_2:CMP BL,02H ;compare BL with 2 </li></ul><ul><li>JNE SET_1 ;jump if BL not equals 2 </li></ul><ul><li>MOV AL, 32H ;store ASCII code for ‘2’ in AL </li></ul><ul><li>JMP DISP ;display AL on ASCII display </li></ul><ul><li>SET_1: MOV AL, 31H ;store ASCII code for ‘1’ in AL </li></ul><ul><li>DISP: OUT 81H, AL ;display AL on ASCII display </li></ul><ul><li>END: INT 3 ;end of program </li></ul>
  29. 29. Iterations using WHILE-DO Structure <ul><li>Format </li></ul><ul><ul><li>WHILE condition is TRUE DO </li></ul></ul><ul><ul><li>action </li></ul></ul><ul><li>E.g. </li></ul><ul><li> WHILE Temp < 100 DO </li></ul><ul><ul><li>Keep Heater ON </li></ul></ul>
  30. 30. A program using WHILE-DO Structure <ul><li>;a program to keep Heater ON WHILE temp < 100 </li></ul><ul><li>MOV AL, 00H </li></ul><ul><li>CHK: CMP AL, 64H ;compare AL with 100 </li></ul><ul><li>JB H_ON ;jump if AL is below 100 </li></ul><ul><li>MOV AL, 00H ;load AL to switch heater OFF </li></ul><ul><li>OUT 81H, AL ;Switch Heater OFF </li></ul><ul><li>JMP END </li></ul><ul><li>H_ON: MOV AL, 01H ;load AL to switch heater ON </li></ul><ul><li>OUT 81H, AL ;Switch Heater ON </li></ul><ul><li>READ: IN AL, 80H ;read from temp. sensor </li></ul><ul><li>JMP CHK ;display AL on ASCII display </li></ul><ul><li>END: INT 3 ;end of program </li></ul>
  31. 31. Iterations using REPEAT-UNTIL Structure <ul><li>Format </li></ul><ul><ul><li>REPEAT </li></ul></ul><ul><ul><li>action </li></ul></ul><ul><ul><li>UNTIL condition is TRUE </li></ul></ul><ul><li>E.g. </li></ul><ul><li>REPEAT </li></ul><ul><li>Keep Heater ON </li></ul><ul><ul><li>UNTIL temp < 100 </li></ul></ul>
  32. 32. A program using REPEAT-UNTIL Structure <ul><li>;a program to determine SUM of first TEN ;natural numbers </li></ul><ul><li>MOV CL, 0AH ;set up loop counter </li></ul><ul><li>MOV AL, 00H ;set initial sum to 0 </li></ul><ul><li>MOV BL, 01H ;first number in BL </li></ul><ul><li>BACK:ADD AL, BL </li></ul><ul><li>INC BL </li></ul><ul><li>DEC CL </li></ul><ul><li>JNZ BACK </li></ul><ul><li>END: INT 3 ;end of program </li></ul>
  33. 33. Using LOOP Instruction of 8086 <ul><li>;a program to determine SUM of first TEN ;natural numbers </li></ul><ul><li>MOV CX, 000AH ;set loop count </li></ul><ul><li>MOV AL, 00H ;set initial sum to 0 </li></ul><ul><li>MOV BL, 01H ;first number in BL </li></ul><ul><li>BACK:ADD AL, BL </li></ul><ul><li>INC BL </li></ul><ul><li>LOOP BACK </li></ul><ul><li>END: INT 3 ;end of program </li></ul>
  34. 34. 4. Instruction Timing and Delay Loops <ul><li>Each instruction takes a certain number of clock cycles to execute </li></ul><ul><li>For e.g. </li></ul><ul><ul><li>MOV BX, 2540H ; 4 clock cycles </li></ul></ul><ul><ul><li>DEC BX ; 2 clock cycles </li></ul></ul><ul><ul><li>JNZ BACK ; 16/4 clock cycles </li></ul></ul><ul><ul><li>LOOP (if CX != 0) ; 17 clock cycles </li></ul></ul>
  35. 35. Delay Loops <ul><li>DELAY: MOV CX, 0FFFFH ;4 clock cycles </li></ul><ul><li>BACK: DEC CX ; 2 clock cycles </li></ul><ul><li>JNZ BACK ;16/4 clock cycles </li></ul><ul><li>DELAY: MOV CX, 0FFFFH ;4 clock cycles </li></ul><ul><li>BACK: LOOP BACK ;17 clock cycles </li></ul><ul><li>Exercise: W rite a REPEAT-UNTIL loop that takes 500us to complete on 8086 with a 5MHz clock. </li></ul>
  36. 36. 5. Strings <ul><li>A string is a series of bytes or words stored in successive memory locations </li></ul><ul><li>8086 can perform the following operations on strings </li></ul><ul><ul><li>Moving a string from one place in memory to another </li></ul></ul><ul><ul><li>Compare two strings </li></ul></ul><ul><ul><li>Search a string for a specified character </li></ul></ul>
  37. 37. Moving a String <ul><li>MOVSB/ MOVSW Instruction </li></ul><ul><ul><li>Copies a byte or word from a location in the data segment to the location in the extra segment </li></ul></ul><ul><ul><li>Offset of source in data segment must be in SI register </li></ul></ul><ul><ul><li>Offset of destination in extra segment must be in DI register </li></ul></ul><ul><ul><li>For multiple byte/word moves the count is stored in CX register </li></ul></ul>
  38. 38. Role of Direction flag in String Moves <ul><li>DF = 0 </li></ul><ul><ul><li>SI & DI will be incremented by 1/2 after every byte/word is moved </li></ul></ul><ul><li>DF = 1 </li></ul><ul><ul><li>SI & DI will be decremented by 1/2 after every byte/word is moved </li></ul></ul>
  39. 39. A program to copy a string of bytes from one location in memory to another
  40. 40. LEA, CLD & REP Instructions <ul><li>LEA (Load Effective Address) </li></ul><ul><ul><li>this instruction determines the offset of the variable or memory location named as the source and puts it in the specified 16-bit register </li></ul></ul><ul><li>CLD (Clear Direction Flag) </li></ul><ul><li>REP </li></ul><ul><ul><li>A prefix written before one of the string instruction </li></ul></ul><ul><ul><li>Causes string instruction to be repeated until CX=0 </li></ul></ul>
  41. 41. <ul><li>DATA SEGMENT </li></ul><ul><li>MSG1 DB ‘TIME FOR A NEW HOME’ </li></ul><ul><li>MSG2 DB ‘JUMP OVER TO MSG3’ </li></ul><ul><li>MSG3 DB 23 DUP(0) </li></ul><ul><li>DATA ENDS </li></ul><ul><li>CODE SEGMENT </li></ul><ul><li>ASSUME CS:CODE, DS: DATA, ES: DATA </li></ul><ul><li>START:MOV AX, DATA </li></ul><ul><li>MOV DS, AX ;initialize data segment register </li></ul><ul><li>MOV ES, AX ;initialize extra segment register </li></ul><ul><li>LEA SI, MSG1 ;point SI at source string </li></ul><ul><li>LEA DI, MSG3 ;point DI at destination string </li></ul><ul><li>MOV CX, 19 ;use CX register as counter </li></ul><ul><li>CLD ;clear direction flag, so counter increments </li></ul><ul><li>REP MOVSB ;move string bytes until all moved </li></ul><ul><li>CODE ENDS </li></ul>
  42. 42. Procedures <ul><li>A procedure is a sequence of instructions written to perform a particular task </li></ul><ul><li>replacing a set of frequently used set of instructions by a procedure saves program memory space </li></ul><ul><li>A CALL instruction in the main program causes 8086 to the execute the set of instructions contained in a procedure </li></ul><ul><li>A RET instruction at the end of procedure returns execution to the next instruction in the main program </li></ul>
  43. 43. MAINLINE OR CALLING PROGRAM CALL PROCEDURE INSTRUCTIONS RET NEXT MAINLINE INSTRUCTIONS
  44. 44. Advantages of using Procedures <ul><li>Saves program memory space </li></ul><ul><li>Problem can be divided in modules </li></ul><ul><li>Allows reusability of code </li></ul><ul><li>Disadvantage </li></ul><ul><ul><li>Takes more time to execute </li></ul></ul>
  45. 45. 8086 CALL Instruction <ul><li>8086 performs these operations when a CALL instruction is executed </li></ul><ul><ul><li>Stores the address of the instruction after the CALL instruction on the stack </li></ul></ul><ul><ul><li>Changes the contents of Instruction Pointer (IP) register and in some cases the Code Segment (CS) register to contain the starting address of the procedure </li></ul></ul>
  46. 46. Types of CALL Instruction <ul><li>Direct Near (within segment) CALL </li></ul><ul><li>Direct Far (inter segment) CALL </li></ul><ul><li>Indirect Near (within segment) CALL </li></ul><ul><li>Indirect Far (within segment) CALL </li></ul>
  47. 47. 8086 RET Instruction <ul><li>When 8086 does a near CALL, it pushes the instruction pointer (IP) value (for the instruction after CALL) on the stack </li></ul><ul><li>A RET instruction at the end of a procedure pops this value back to instruction pointer to return to the calling program. </li></ul>
  48. 48. The 8086 Stack <ul><li>A stack is a Last-Input-First-Output read/write memory </li></ul><ul><li>It is used to </li></ul><ul><ul><li>Store return addresses when a procedure is called </li></ul></ul><ul><ul><li>Save contents of registers for the calling program while a procedure executes </li></ul></ul><ul><ul><li>Hold data or addresses that will be acted upon by the procedure </li></ul></ul>
  49. 49. Initializing Stack Memory <ul><li>STACK_SEG SEGMENT STACK </li></ul><ul><li>DW 40 DUP(0) </li></ul><ul><li>STACK_TOP LABEL WORD </li></ul><ul><li>STACK_SEG ENDS </li></ul><ul><li>CODE_SEG SEGMENT </li></ul><ul><li>ASSUME CS:CODE_SEG, SS: STACK_SEG </li></ul><ul><li>MOV AX, STACK_SEG ;initialize stack </li></ul><ul><li>MOV SS, AX ;segment register </li></ul><ul><li>LEA SP, STACK_TOP ;initialize stack pointer </li></ul><ul><li>------------------------------ ;program instructions </li></ul><ul><li>CODE_SEG ENDS </li></ul><ul><li>END </li></ul>

×