Upload
meljun-cortes-mbampa
View
246
Download
4
Embed Size (px)
DESCRIPTION
MELJUN CORTES Conditional Jump and Loop Instructions
Citation preview
* Property of STIPage 1 of 19
Conditional Jump and Loop Instruction
Computer Organization and Assembly Language
The Jcc Instructions
Ø The Jcc (Conditional Jump) instructions are jumps that may or may not transfer control depending on the state of the status flags at the time the instruction is executed.
Ø Format: Jcc operand
Ø Action: If the specified condition cc is true, it will jump to the address specified in the operand; otherwise the next instruction is executed.
* Property of STIPage 2 of 19
Conditional Jump and Loop Instruction
Computer Organization and Assembly Language
The Jcc Instructions
(CF or ZF) = 1below or equal/not aboveJBE/JNA
CF = 1below/not above nor equalJB/JNA
CF = 0above or equal/not belowJAE/JNB
(CF or ZF) = 0above/not below nor equalJA/JNBE
Condition“Jump if …”Mnemonic
Comparing Unsigned Numbers
* Property of STIPage 3 of 19
Conditional Jump and Loop Instruction
Computer Organization and Assembly Language
The Jcc Instructions
ZF=1 or (SF≠OF)
less than or equal/not greater than
JLE/JNG
SF ≠ OFless than/not greater than or equal
JL/JNGE
SF = OFgreater than or equal/not less than
JGE/JNL
ZF=0 or (SF=OF)
greater than/not less than or equal
JG/JNLE
Condition“Jump if …”Mnemonic
Comparing Signed Numbers
* Property of STIPage 4 of 19
Conditional Jump and Loop Instruction
Computer Organization and Assembly Language
The Jcc Instructions
OF = 0no overflow JNO
OF = 1overflowJO
PF = 0no parity/parity oddJNP/JPO
PF = 1parity/parity evenJP/JPE
SF = 0not signJNS
SF = 1signJS
CF = 0not carryJNC
CF = 1carryJC
ZF = 0not equal to/not zeroJNE/JNZ
ZF = 1equal to/zeroJE/JZ
Condition“Jump if …”Mnemonic
Test Flags
* Property of STIPage 5 of 19
Conditional Jump and Loop Instruction
Computer Organization and Assembly Language
Example – Jcc Instruction
1. MOV AL, A5H
MOV BL, 73H
CMP AL, BL
JA L1
* Property of STIPage 6 of 19
Conditional Jump and Loop Instruction
Computer Organization and Assembly Language
Example – Jcc Instruction
2. MOV AL, A5H
MOV BL, 73H
CMP AL, BL
JG L1
* Property of STIPage 7 of 19
Conditional Jump and Loop Instruction
Computer Organization and Assembly Language
Example – Jcc Instruction
3. MOV AL, 00H
MOV CL, 03H
AGAIN: ADD AL, 02H
DEC CL
JNZ AGAIN
* Property of STIPage 8 of 19
Conditional Jump and Loop Instruction
Computer Organization and Assembly Language
Example – Jcc Instruction
4. MOV AL, 7FH
MOV BL, 75H
ADD AL, BL
JO NEXT
* Property of STIPage 9 of 19
Conditional Jump and Loop Instruction
Computer Organization and Assembly Language
Example – Jcc Instruction
5. MOV AL, 01H
DEC AL
JG L1
* Property of STIPage 10 of 19
Conditional Jump and Loop Instruction
Computer Organization and Assembly Language
Example – Jcc Instruction
6. Circumventing the limitation of the short-label:
MOV BX, 0002HMOV CX, 0002HCMP BX, CXJE L1
:::
L1: ::
* Property of STIPage 11 of 19
Conditional Jump and Loop Instruction
Computer Organization and Assembly Language
The JCXZ Instruction
Ø The JCXZ (Jump if CX is 0) instruction transfers control to the target operand if CX is 0.
Ø Format: JCXZ short-label
Ø Action: If CX = 0, then jump to the address specified by the operand.
* Property of STIPage 12 of 19
Conditional Jump and Loop Instruction
Computer Organization and Assembly Language
Program Tracing Example
Unless otherwise stated, determine the contents of all the affected general-purpose registers and the flags after executing the following programs (assume that all registers and flags are initially 0):
MOV AL, 23HMOV CH, 6BHXOR AL, CHTEST AL, 77HJZ L1AND CH, 33H
L1: XOR CH, AAHTEST CH, ALJZ L2NOT ALOR CH, AL
L2: NOT CH
What will be AX and CX after executing this program?
* Property of STIPage 13 of 19
Conditional Jump and Loop Instruction
Computer Organization and Assembly Language
The LOOP Instruction
Ø The LOOP instruction decrements CX by 1 and transfers control to the target location if CX is not 0; otherwise, the instruction following LOOP is executed.
Ø Format: LOOP short-label
Ø Action: CX [CX] - 1
If CX ≠ 0, then jump to the address specified by the operand; otherwise execute the instruction following the LOOP.
* Property of STIPage 14 of 19
Conditional Jump and Loop Instruction
Computer Organization and Assembly Language
Example - LOOP Instruction
MOV CX, 0003H
MOV AX, 0000H
L1: INC AX
LOOP L1
What is the value of AX and CX?
* Property of STIPage 15 of 19
Conditional Jump and Loop Instruction
Computer Organization and Assembly Language
The LOOPE/LOOPZ Instruction
Ø The LOOPE/LOOPZ (Loop while equal/Loop while zero) instruction decrements CX by 1 and transfers control to target location if CX is not zero and if ZF is 1; otherwise the instruction following LOOPE/LOOPZ is executed.
Ø Format: LOOPE short-labelLOOPZ short-label
Ø Action: CX [CX] - 1
If CX ≠ 0 and ZF = 1, then jump to the address specified by the operand; otherwise execute the instruction following the LOOPE/LOOPZ.
* Property of STIPage 16 of 19
Conditional Jump and Loop Instruction
Computer Organization and Assembly Language
Example - LOOPE/LOOPZ Instruction
MOV CX, 0005HMOV SI, FFFFHMOV AL, 01H
L1: INC SICMP BETA [SI], ALLOOPE L1
01H
01H
03H
01H
01HBETA:
* Property of STIPage 17 of 19
Conditional Jump and Loop Instruction
Computer Organization and Assembly Language
The LOOPNE/LOOPNZ Instruction
Ø The LOOPNE/LOOPNZ (Loop while not equal/Loop while not zero) instruction decrements CX by 1 and transfers control to the target location if CX is not zero and ZF is 0; otherwise the instruction following LOOPNE/LOOPNZ is executed.
Ø Format: LOOPNE short-labelLOOPNZ short-label
Ø Action: CX [CX] - 1
If CX ≠ 0 and ZF = 0, then jump to the address specified by the operand; otherwise execute the instruction following the LOOPNE/LOOPNZ.
* Property of STIPage 18 of 19
Conditional Jump and Loop Instruction
Computer Organization and Assembly Language
Example - LOOPNE/LOOPNZ Instruction
MOV CX, 0005HMOV SI, FFFFHMOV AL, 01H
L1: INC SICMP BETA [SI], ALLOOPNE L1
04H
07H
01H
05H
03HBETA:
* Property of STIPage 19 of 19
Conditional Jump and Loop Instruction
Computer Organization and Assembly Language
Program Tracing Example
Unless otherwise stated, determine the contents of all the affected general-purpose registers and the flags after executing the following programs (assume that all registers and flags are initially 0):
MOV AX, 000AHMOV CX, 0014H
L1: SUB AX, 0001HLOOP L1LOOPZ L1LOOPNZ L1
What will be AX and CX after executing this program?