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