Introduction to Computer Engineering by Richard E. Haskell Branching Instructions Module M17.2 Section 11.1
Introduction to Computer Engineering by Richard E. Haskell
Calculating Branching Displacements
Introduction to Computer Engineering by Richard E. Haskell Calculating Branching Displacements Note: Displacement is only 8 bits. Therefore, the program can only branch forward +127 bytes, or backward -128 bytes.
Introduction to Computer Engineering by Richard E. Haskell 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: But you should NEVER have to to this!
Introduction to Computer Engineering by Richard E. Haskell Branching Example 1 Branch on Z flag 0000 B LOOP1: MOV CX, LOOP2: DEC CX FD JNE LOOP F8 JE LOOP1
Introduction to Computer Engineering by Richard E. Haskell Branching Example 2 Branch on S flag 0000 B1 7D LOOP1: MOV CL,7DH 0002 FE C1 LOOP2: INC CL FC JNS LOOP F8 JS LOOP1
Introduction to Computer Engineering by Richard E. Haskell Branching Example 3 Branch on C flag 0000 B0 2E LOOP1: MOV AL,2EH 0002 FE C8 LOOP2: DEC AL C 2B CMP AL,2BH FA JNB LOOP F6 JC LOOP1
Introduction to Computer Engineering by Richard E. Haskell
Problem: Go through a loop (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?
Introduction to Computer Engineering by Richard E. Haskell
Branching Example 1 Branch on Z flag 0000 B LOOP1: MOV CX, LOOP2: DEC CX FD JNE LOOP F8 JE LOOP B L1: MOV CX, E2 FE L2: LOOP L EB F9 JMP L1 Same as:
Introduction to Computer Engineering by Richard E. Haskell Repeat While / Repeat Until Loop L1: LOOP L1 L1: CX = CX - 1 repeat while CX /= 0 L1: CX = CX - 1 repeat until CX = 0
Introduction to Computer Engineering by Richard E. Haskell Do While Loop JCXZ NEXT L1: LOOP L1 NEXT: --- Do while CX /= CX = CX - 1 end do NEXT: ---