Presentation is loading. Please wait.

Presentation is loading. Please wait.

Jumps, Loops and Branching. Unconditional Jumps Transfer the control flow of the program to a specified instruction, other than the next instruction in.

Similar presentations


Presentation on theme: "Jumps, Loops and Branching. Unconditional Jumps Transfer the control flow of the program to a specified instruction, other than the next instruction in."— Presentation transcript:

1 Jumps, Loops and Branching

2 Unconditional Jumps Transfer the control flow of the program to a specified instruction, other than the next instruction in sequential execution Jumps are performed with labels pointing to the address of the target instruction It is the equivalent of a goto statement Goto is good in assembly (no real alternative)!

3 Unconditional jumps Short jump –JMP SHORT label, where label is within –128/+127 bytes off the current instruction Near jump –JMP label, where label is within –32,768/+32,767 bytes off the current instruction Far jump –JMP label, where label is any memory address, in segment:offset format In protected mode near and far jumps are the same The assembler will know how far you are jumping and use the correct instruction

4 Conditional Jumps Jumps performed if a condition evaluates to true Conditions are evaluated based on the values of the bits in the FLAGS register Test S, Z, P, C, O If condition is true the jump is performed to the location specified If condition is false the code proceeds with the next instruction in sequence Short or near jumps in 80386

5 Comparisons CMP A, B Executes A-B without modifying A (non-destructive) CMP is usually followed by a conditional jump based on the outcome of the comparison CMP AL, 10h; If AL >= 10 jump JAE target CMP is a non-destructive SUB, it sets the flags exactly like the SUB instruction

6 Comparisons Unsigned CMP A, B –If (A<B), C=1 –If (A>B), C=0 –Z tested for equality/inequality Signed CMP A, B –If (S XOR O == 1) A B –Z tested for equality/inequality

7 Signed comparisons Case 1, Sign=1, Overflow=0 –A-B looks negative –There is no overflow, so A<B Case 2, Sign=0, Overflow=1 –A-B looks positive –But there is overflow so the sign bit is wrong and A<B Case 3, Sign=0, Overflow=0 –A-B looks positive –No overflow, so A>B Case 4, Sign=1, Overflow=1 –A-B looks negative –But overflow bit is set so the sign flag is wrong therefore A>B Fortunately, we don’t have to remember this stuff!

8 Conditional Jumps Syntax: JX, where X denotes a test condition J -> Jump N -> Not E -> Equal A/B -> Above/Below for unsigned arithmetic G/L -> Greater/Less for signed arithmetic

9 Conditional jump instructions JL, jump if less –Jumps if A<B, that is if S XOR O == 1 JA, JNBE (above == not (below or equal)) –Jumps if C=0 & Z=0 JBE, JNA (below or equal == not above) –Jumps if Z=1 | C=1 JAE, JNB, JNC (above or equal == not below==no carry) –Jumps if C=0

10 Conditional jump instructions JB, JNA, JC (below == not above == carry set) –Jumps if C=1 JE, JZ (equal == result is 0) –Jumps if Z=1 JNE, JNZ (not equal == result is not 0) –Jumps if Z=0 JNS (sign, no sign) –Jumps if S=0 JO –Jumps if O=1 JS –Jumps if S=1

11 Conditional jump instructions JNO –O=0 JG, JNLE (greater==not (less or equal)) –S=0, Z=0 JGE, JNL (greater or equal == not less) –S=0 JL, JNGE (less == not (greater or equal)) –S XOR O = 1 JLE, JNG (less or equal == not greater) –(S XOR O = 1) | Z=1 JCXZ (counter register is 0), useful for loops

12 Loops LOOP label –Combination of CMP and JNZ Decrement the CX register and if register has not become 0 jump to the specified label If CX becomes 0 the instruction following the LOOP instruction is executed

13 Example mov cx, 100;load count movsi, BLOCK1 movdi, BLOCK2 Again: lodsw; (load string – word) ; gets Block1 data ; AX = [SI]; SI = SI + 2 addAX, [ES:DI];add Block2 data stosw;store in Block2 ; [DI] = AX; DI = DI + 2 loopAgain;repeat 100 times ret

14 If then else in assembly mov ax, [a] mov bx, [b] cmp ax,bx ja true ; false instructions jmp done true: ; true instructions done” If (a>b) { /* true instructions */ } else { /* false instructions */ }

15 Do while in assembly begin: ; body instructions… mov ax, [a] mov bx, [b] cmp a,b je begin do { /* body instructions */ } while (a==b);

16 While do in assembly begin: mov ax, [a] mov bx, [b] cmp ax,bx jne done ; instructions jmp begin done: while (a==b) { /* instructions */ /* instructions */};

17 (Simple) For loop mov cx, 10 begin: ; instructions loop begin for (i=10;i>0;i--) { /* instructions */ }

18 Examples ;while (J >= K) do begin ;J := J - 1; ;K := K + 1; ;L := J * K; ;end; WhlLoop: movax, [J] cmpax, [K] jngeQuitLoop decword [J] incword [K] movax, [J] imul[K] mov[L], ax jmpWhlLoop QuitLoop: ;if (J <= K) then ;L := L + 1 ;elseL := L - 1 ; J, K, L are signed words movax, [J] cmpax, [K] jnelDoElse incword [L] jmpifDone DoElse: decword [L] ifDone:


Download ppt "Jumps, Loops and Branching. Unconditional Jumps Transfer the control flow of the program to a specified instruction, other than the next instruction in."

Similar presentations


Ads by Google