Branch GRP UK
Branch GRP UK
Conditional jumps
Uncondi-tional jump
Iteration instructions
CALL instructions
Return instructions
Conditional Jump instructions Conditional Jump instructions in 8086 are just 2 bytes long. 1-byte opcode followed by 1-byte signed displacement (range of 128 to +127). Conditional Jump Instructions Jumps based on a single flag Jumps Based on a single flag JZ JNZ JS JNS JC JNC JP JNP JO JNO r8 r8 r8 r8 r8 r8 r8 r8 r8 r8 ;Jump if zero flag set (if result is 0). JE also means same. ;Jump if Not Zero. JNE also means same. ;Jump if Sign flag set to 1 (if result is negative) ;Jump if Not Sign (if result is positive) ;Jump if Carry flag set to 1. JB and JNAE also mean same. ;Jump if No Carry. JAE and JNB also mean same. ;Jump if Parity flag set to 1. JPE (Jump if Parity Even) also means same. ;Jump if No Parity. JPO (Jump if Parity Odd) also means same. ;Jump if Overflow flag set to 1 (if result is wrong) ;Jump if No Overflow (if result is correct) JNE is abbreviation for Jump if Not Equal. JNAE is for Jump if Not Above or Equal. JNB for Jump if Not Above. Jumps based on more than one flag
JE is abbreviation for Jump if Equal. JB is abbreviation for Jump if Below. JAE for Jump if Above or Equal.
JZ, JNZ, JC and JNC used after arithmetic operation JE, JNE, JB, JNAE, JAE and JNB are used after a compare operation.
Examples for JE or JZ instruction Ex. for forward jump Only examples using JE instruction given for forward and backward jumps. CMP SI, DI JE SAME ADD CX, DX ;Executed if Z = 0 : (if SI not equal to DI) : SAME: SUB BX, AX ;Executed if Z = 1 (if SI = DI)
Ex. for backward jump BACK: SUB BX,AX Should be <=127 bytes : : CMP SI, DI JE BACK ADD CX,DX ;Executed if Z = 1 (if SI=DI)
Jumping beyond -128 to +127? Requirement CMP SI, DI JE SAME ADD CX, DX : : SUB BX, AX Then do this! CMP SI, DI JNE NEXT JMP SAME ADD CX, DX : : SUB BX, AX
NEXT:
SAME:
15
Range for JMP (unconditional jump) can be +2 = + 32K. JMP instruction discussed in detail later
Terms used in comparison Above and Below used for comparing Unsigned numbers. Greater than and less than used when comparing signed numbers. All Intel microprocessors use this convention. Accordingly, all the following statements are true. 95H is above 65H 95H is less than 65H 65H is below 95H 65H is greater than 95H Unsigned comparison - True Signed comparison True (as 95H is negative, 65H is positive) Unsigned comparison - True Signed comparison - True
Jump based on multiple flags Conditional Jumps based on multiple flags are used after a CMP (compare) instruction. JBE / JNA instruction Jump if Below or Equal or Jump if Not Above Jump if Cy = 1 OR Z= 1 Below OR Equal No Jump if Cy = 0 AND Z = 0 Surely Above Ex. CMP BX, CX JBE BX_BE
BX_BE (BX is Below or Equal) is a symbolic location JNBE / JA instruction Jump if Not (Below or Equal) or Jump if Above Jump if Cy = 0 AND Z= 0 Surely Above JLE / JNG instruction Jump if Less than OR Equal or Jump if Not Greater than Jump if [(S=1 AND V=0) OR (S=0 AND V=0)] OR Z=1 [(surely negative) or (wrong answer positive!)] or Equal i.e. [S XOR V=1] OR Z=1 No Jump if [(S=0 AND V=0) OR (S=1 AND V=1)] AND Z=0 [(surely positive) or (wrong answer negative!)] and not equal i.e.[S XOR V=0] AND Z=0 No Jump if Cy = 1 OR Z = 1 Below OR Equal Ex. CMP BX, CX JBE BX_BE
JNLE / JG instruction Jump if Not (Less than OR Equal) or Jump if Greater than Jump if [(S=0 AND V=0) OR (S=1 AND V=1)] AND Z=0 [(surely positive) or (wrong answer negative!)] and not equal i.e. S XOR V=0 AND Z=0 JL / JNGE instruction Jump if Less than or Jump if NOT (Greater than or Equal) Jump if [S=1 AND V=0] OR [S=0 AND V=1] (surely negative)or (wrong answer positive!) i.e. S XOR V=1 Note: When S=1, result cannot be 0 No Jump if [S=0 AND V=0] OR [S=1 AND V=1] (surely positive) or (wrong answer negative!) i.e.S XOR V=0 No Jump if [(S=1 AND V=0) OR (S=0 AND V=1)] OR Z=1 [(surely negative) or (wrong answer positive!)] or equal i.e.S XOR V=1 OR Z=1
JNL / JGE instruction Jump if Not Less than or Jump if Greater than OR Equal Jump if [S=0 AND V=0] OR (S=1 AND V=1) (surely positive) or (wrong answer negative!) i.e. S XOR V=0 Note: When S=0, result can be >= 0 Unconditional Jump instruction Unconditional Jump Instruction No Jump if [S=1 AND V=0] OR (S=1 AND V=1) (surely negative) or (wrong answer positive!) i.e.S XOR V=1
Near Unconditional Jump instruction Near Jump Direct Jump (common) 2-bytes Short Jump (EB r8) 3-bytes Long Jump (E9 r16) 7 15 Range: + 2 Range: +2 Indirect Jump (uncommon) 2 or more bytes Starting with FFH Range: complete segment
Three Near Jump and two Far Jump instructions have the same mnemonic JMP, but they have different opcodes
Short Jump Instruction 2 byte (EB r8) instruction with Range: -128 to +127 bytes For Backward jump: Assembler knows the quantum of jump. Generates Short Jump code if <=128 bytes is the required jump. Generates code for Long Jump if >128 bytes is the required jump. For Forward jump: Assembler doesnt know jump quantum in pass 1. Assembler reserves 3 bytes for the forward jump instruction. If jump distance turns out to be >128 bytes, the instruction is coded as E9 r16 (E9H = Long jump code). If jump distance becomes <=128 bytes, the instruction is coded as EB r8 followed by code for NOP (E8H = Short jump code).
SHORT Assembler Directive Assembler generates only 2 byte Short Jump code for forward jump, if the SHORT assembler directive is used. JMP SHORT Programmer should ensure that the Jump distance is <=127 bytes Long Jump instruction 3-byte (E9 r16) instruction with Range: -32768 to +32767 bytes Long Jump can cover entire 64K bytes of Code segment : : SAME: MOV CX, DX SAME
CS:0000H
: : JMP BKWD : :
Long Jump or Short Jump? Can be treated as a small (20H) Backward Branch! CS:0000H : : CS:0010H JMP FRWD : FRWD = CS:FFF0H CS:FFFFH : : :
: : : : JMP BKWD : : Jump distance =FFE0H. Too very long backward jump
Intra segment indirect Jump It is also called Near Indirect Jump. It is not commonly used. Instruction length: 2 or more bytes Range: complete segment Ex.1: JMP DX If DX = 1234H, branches to CS:1234H. 1234H is not signed relative displacement. Ex. 2: JMP wordptr 2000H[BX] If BX contents is 1234H DS:3234H 5678H
DS:3236H AB22H
Far Jump
Direct Jump (common) 5 bytes, opcode EA, 2 byte offset, 2 byte segment value
Range: anywhere in memory Range: anywhere in memory As stated earlier, three Near Jump and two Far Jump instructions have the same mnemonic JMP but different opcodes. Inter segment Direct Jump instruction Also called Far Direct Jump. It is the common inter segment jump scheme It is a 5 byte instruction. 1 byte opcode (EAH), 2 byte offset value, 2 byte segment value Ex. JMP Far ptr LOC Inter segment Indirect Jump instruction Also called Far Indirect Jump. It is not commonly used. Instruction length depends on the way jump location is specified. It can be a minimum of 2 bytes. Ex. JMP DWORD PTR 2000H[BX] If BX contents is 1234H branch takes place to location ABCDH:5678H. It is a 4-byte instruction. DS:3234H 5678H DS:3236H ABCDH Iteration Instructions Iteration instructions provide a convenient way to implement loops in a program Iteration instructions
LOOP
LOOPZ or LOOPE
LOOPNZ or LOOPNE
JCXZ
LOOP Instruction Let us say, we want to repeat a set of instructions 5 times. For 8085 processor MVI C, 05H AGAIN: MOV B, D : DCR C JNZ AGAIN For 8086processor MOV CX, 0005H AGAIN: MOV BX, DX : LOOP AGAIN
General format: LOOP r8; r8 is 8-bit signed value. It is a 2 byte instruction. Used for backward jump only. Maximum distance for backward jump is only 128 bytes. LOOP AGAIN is almost same as: DEC CX JNZ AGAIN LOOP instruction does not affect any flags. If CX value before entering the iterative loop is: 0005, then the loop is executed 5 times till CX becomes 0 0001, then the loop is executed 1 time till CX becomes 0 0000, then the loop is executed FFFF+1 = 10000H times! JCXZ Instruction Jump if CX is Zero is useful for terminating the loop immediately if CX value is 0000H It is a 2 byte instruction. It is used for forward jump only. Maximum distance for forward jump is only 127 bytes. Ex. MOV CX, SI JCXZ SKIP AGAIN: MOV BX, DX : : LOOP AGAIN SKIP: ADD SI, DI
LOOPZ instruction LOOP while Zero is a 2-byte instruction. It is used for backward jump only. Backward jump takes place if after decrement of CX it is still not zero AND Z flag = 1. LOOPE is same as LOOPZ. LOOPE is abbreviation for LOOP while Equal. LOOPE is normally used after a compare instruction. Ex. MOV CX, 04H BACK: SUB BX, AX MOV BX, DX
CALL instruction is used to branch to a subroutine. There are no conditional Call instructions in 8086. CALL instructions Near CALL or Intra segment CALL Far CALL or Inter segment CALL
Near Direct CALL instruction It is a 3-byte instruction. It has the format CALL r16 and has the range + 32K bytes. Covers the entire Code segment. It is the most common CALL instruction. It is functionally same as the combination of the instructions PUSH IP and ADD IP, r16. Ex. CALL Compute Near Indirect CALL instruction Not commonly used. Instruction length depends on the way the called location is specified. Ex.1: CALL AX ; If (AX) = 1234H, branches to procedure at CS: 1234H. 1234H is not relative displacement.
Ex. 2: CALL word ptr 2000H[BX] If BX contents is1234H Branches to subroutine at CS:5678H DS:3234H 5678H DS:3236H ABCDH Far Direct CALL instruction It is a 5-byte instruction. 1-byte opcode, 2-byte offset, 2-byte segment value. Far direct CALL is functionally same as: PUSH CS PUSH IP
IP = 2-byte offset value provided in CALL CS = 2-byte segment value provided in CALL Ex. CALL far ptr Compute Far Indirect CALL instruction Not commonly used. Instruction length depends on the way the called location is specified. Ex. CALL dword ptr 2000H[BX] If BX contents is1234H bBranches to subroutine at ABCDH:5678H DS:3234H DS:3236H Conditional CALL? What if we want to branch to subroutine COMPUTE only if Cy flag = 0? Solution: JC NEXT CALL COMPUTE NEXT: RETURN instructions RET is abbreviation for Return from subroutine RET instructions Near RET or Intra segment RET RET Near RET instruction It is 1-byte instruction. Opcode is C3H. It is functionally same as : POP IP Ex: Compute Proc Near ; indicates it is a NEAR procedure : : RET Compute ENDP ; end of procedure Compute RET d16 Far RET or Inter segment RET RET RET d16 ; execute only if Cy = 0 5678H ABCDH
In fact, default procedure type is NEAR Near RET d16 instruction It is a 3-byte instruction. 1-byte opcode (C2H) and 2-byte data. It is functionally same as: POP IP SP = SP + d16 Ex. RET 0004H RET d16 is useful for flushing out the parameters that were passed to the subroutine using the stack
Use of RET d16 instruction Main Program : : PUSH Var1 PUSH Var2 CALL Compute : : Subroutine PROC : : RET 0004H ENDP
IP Var2 Var1
COMPUTE
IP Var2 Var1
COMPUTE
Far RET instruction It is 1-byte instruction. Opcode is CBH. It is functionally same as: POP IP + POP CS Ex. SINX Proc Far : : RET SINX ENDP ; indicates it is a FAR procedure
Default procedure type is NEAR Far RET d16 instruction It is a 3-byte instruction. 1-byte opcode (CAH) and 2-byte data. It is functionally same as: POP IP + POP CS + ADD SP, d16 Ex. RET 0006H RET d16 is useful for flushing out the parameters that were passed to the subroutine using the stack.