160 likes | 338 Views
Branching Instructions. Module M17.2 Section 11.1. Calculating Branching Displacements. Calculating Branching Displacements. Note: Displacement is only 8 bits. Therefore, the program can only branch forward +127 bytes, or backward -128 bytes. But you should NEVER have to to this!.
E N D
Branching Instructions Module M17.2 Section 11.1
Calculating Branching Displacements Note: Displacement is only 8 bits. Therefore, the program can only branch forward +127 bytes, or backward -128 bytes.
But you should NEVER have to to this! A possible way to branch conditionally more than +127 or -128 bytes is as follows: Replace JE distant next: --------- with JNE next JMP distant next: ----------
Branching Example 1Branch on Z flag 0000 B9 03 00 LOOP1: MOV CX,3 0003 49 LOOP2: DEC CX 0004 75 FD JNE LOOP2 0006 74 F8 JE LOOP1
Branching Example 2Branch on S flag 0000 B1 7D LOOP1: MOV CL,7DH 0002 FE C1 LOOP2: INC CL 0004 79 FC JNS LOOP2 0006 78 F8 JS LOOP1
Branching Example 3Branch on C flag 0000 B0 2E LOOP1: MOV AL,2EH 0002 FE C8 LOOP2: DEC AL 0004 3C 2B CMP AL,2BH 0006 73 FA JNB LOOP2 0008 72 F6 JC LOOP1
Problem: Go through a loop 20010 (C8H) times Trial solution: MOV CL,0 ;set CL = 0 LOOP: INC CL ;increment CL CMP CL,0C8H ;compare CL toC8H JL LOOP ;loop if CL < 200 How many times is the statement INC CL executed?
Same as: 0000 B9 03 00 L1: MOV CX,3 0003 E2 FE L2: LOOP L2 0005 EB F9 JMP L1 Branching Example 1Branch on Z flag 0000 B9 03 00 LOOP1: MOV CX,3 0003 49 LOOP2: DEC CX 0004 75 FD JNE LOOP2 0006 74 F8 JE LOOP1
Repeat While / Repeat Until Loop L1: --- --- --- LOOP L1 L1: --- --- --- CX = CX - 1 repeat while CX /= 0 L1: --- --- --- CX = CX - 1 repeat until CX = 0
Do While Loop JCXZ NEXT L1: --- --- --- LOOP L1 NEXT: --- Do while CX /= 0 --- --- --- CX = CX - 1 end do NEXT: ---