Presentation on theme: "Jump Condition. Topic Control Flow Structure – Conditional Jump – Unconditional Jump Control Flow Structures – IF-THEN – IF-THEN-ELSE – CASE Branches."— Presentation transcript:
Topic Control Flow Structure – Conditional Jump – Unconditional Jump Control Flow Structures – IF-THEN – IF-THEN-ELSE – CASE Branches with Compound Conditions
An Example of Jump Display the entire IBM character set.MODEL SMALL.CODE.STARTUP MOV AH, 2; display char function MOV CX, 256; no. of chars to display MOV DL, 0; DL has ASCII code for null char PRINT_LOOP: INT 21H; display a char INC DL; increment ASCII code DEC CX; decrement counter JNZ JNZ PRINT_LOOP:; keep going if CX not 0.EXIT END Calls system routine/functions Section 6-1 of Assembly Language Programming Book The function number Statement label Labels r needed in cases where one instruction refers to another.
Conditional Jumps JNZ Syntax: Jxxxdestination_label True or False [no gray area – like our minds!] JNZ is an example of conditional jump instruction – Checks the Z flag. If Z = 0 then jump to the location Three categories of conditional jumps – Signed jumps, for signed interpretation – Unsigned jumps, for unsigned interpretation – Single-flag jumps, operates on settings of individual flags
How to decide? Implement? CPU looks at the FLAGS register If jump conditions r TRUE – the CPU adjusts the IP to point to the destination_label, so that the instruction at this label will be done next. If FALSE – no change in IP
1. Signed Conditional Jumps OpcodesDescriptionCondition for jumps JG/JNLE Jump if Greater than Jump if Not Less than or Equal to ZF = 0 and SF = OF JGE/JNL Jump if Greater than or Equal to Jump if Not Less than SF = OF JL/JNGE Jump if Less than Jump if Not Greater than or Equal to SF <> OF JLE/JNG Jump if less than or equal Jump if not greater than ZF = 1 or SF <> OF
2. Unsigned Conditional Jumps SymbolDescriptionCondition for jumps JA/JNBEJump if above Jump if not below or equal CF = 0 and ZF = 0 JAE/JNBJump if above or equal Jump if not below CF = 0 JB/JNAEJump if below Jump if not above or equal CF = 1 JBE/JNAJump if below or equal Jump if not above CF = 1 or ZF = 1
3. Single-Flag Jumps SymbolDescriptionCondition for jumps JE/JZJump if equal Jump if equal to zero ZF = 1 JNE/JNZJump if not equal Jump if not zero ZF = 0 JCJump if carryCF = 1 JNCJump if no carryCF = 0 JOJump if overflowOF = 1 JNOJump if no overflowOF = 0 JSJump if sign negativeSF = 1 JNSJump if nonnegative signSF = 0 JP/JPEJump if parity evenPF = 1 JNP/JPOJump if parity oddPF = 0
Range of a Conditional Jump The destination label must precede the Jump instruction by no more than 126 bytes Or, follow by no more than 127 bytes LABEL : ; statement JNZ LABEL 126 bytes JZLABEL ; statements LABEL: ; statement 127 bytes ref
CMP Instruction The jump condition is often provided by the CMP (compare) instruction CMP destination, source dest[contents] – source[contents] It is like SUB, except that destination is not changed Destination may not be a constant The result is not stored but the flags are affected CMP AX, BX JG BELOW ;JG – jump if > CMP AX, 10 JG BELOW If AX = 7FFFh, and BX = 0001h, the result is 7FFFh h = 7FFEh. ZF = SF = OF = 0, JG is satisfied, so control transfers to label BELOW
Signed vs. Unsigned Jumps Each signed jump corresponds to an analogous unsigned jump – e.g., signed JG (if >) corresponds to unsigned JA (if above) – Use depends on the interpretation The jumps operate on different flags SymbolDescriptionCondition for jumps JG/JNLEJump if greater than Jump if not less than or equal to ZF = 0 and SF = OF JA/JNBEJump if above Jump if not below or equal CF = 0 and ZF = 0 Wrong jumps wrong results! [same as life]
Until 4/11 Wrong jumps wrong results! [same as life]
Signed vs. Unsigned Jumps cont. For signed interpretation, let us take – AX = 7FFFh, BX = 8000h and we execute Even though 7FFFh > 8000h in a signed sense, the program does not jump to BELOW_LABEL why? Because 7FFFh < 8000h in an unsigned sense JA, which is the unsigned jump CMP AX, BX JA BELOW_LABEL
With standard ASCII character set [character code 0-31 for control chars; for printable characters] – either signed/unsigned jumps may be used. Why? Because the sign bit of a byte containing a character code is always zero . BUT, unsigned jumps should be used when comparing extended ASCII characters [code 80h ~ FFh] Signed vs. Unsigned Jumps cont. working with CHAR
Extended ASCII codes (character code ) There are several different variations of the 8-bit ASCII table. E.g., ISO , also called ISO Latin-1. Codes contain the Microsoft® Windows Latin-1 extended characters.
The JMP Instruction JMP (jump) instruction causes an unconditional jump Syntax is: JMP can be used to get around the range restriction [126/127 byte] Flags – no change JMP destination/target_label TOP: ; body of the loop, say 2 instructions DEC CX; decrement counter JNZ TOP; keep looping if CX > 0 MOV AX, BX TOP: ; the loop body contains so many instructions ; that label TOP is out of range for JNZ. Solution is- DEC CX JNZ BOTTOM JMP EXIT BOTTOM: JMP TOP EXIT: MOV AX, BX Section 6-3: Assembly Language Programming ref
TOP: ; the loop body contains so many instructions ; that label TOP is out of range for JNZ. Solution is- DEC CX JNZ BOTTOM JMP EXIT BOTTOM: JMP TOP EXIT: MOV AX, BX When CX=0 - It will not Jump to BOTTOM - It will go to next instr. JMP EXIT - JMP TOP is unconditional – just Jump!
Lets Jump to another lecture?
References Ch 6, Assembly Language Programming – by Charls Marut Some materials are from Dr. Sazzad, NSU