Professional Documents
Culture Documents
MP 7
MP 7
Outline
• Unconditional jump
• Conditional branching
• Construction of loops
3
Unconditional Jump
JMP
• Short jump 2-byte instruction that allows jumps or branches to memory
locations within +127 and -128 bytes from the memory location following the
jump
OPCODE DISP
JMP SHORT Label
• Near jump 3-byte instruction that allows jumps or branches within +/-
32Kb from the instruction in the current code segment
OPCODE DISP low DISP high
JMP Label
• Far jump 5-byte instruction that allows a jump to any memory location
within the entire memory space
OPCODE IP low IP high CS low CS high
JMP Label
• For 80386 and above the near jump (5-byte) is within +/-2G if the machine
operates in the protected mode and +/-32K bytes if operates in the real mode
4
5
JMP
6
; R denotes Relocatable
Conditional Branching
• Logic and arithmetic instructions set flags
• Flags provide state information from previous instruction(s)
• Using flags we can perform conditional jumping, i.e.,
transfer program execution to some different place within
the program
if condition was true
• jump back or forward in your code to the location specified
• instruction pointer (IP) gets updated (to point to the instruction to
which execution will jump)
if condition was false
• continue execution at the following instruction
• IP gets incremented as usual
8
Numerical Comparison
• CMP(comparison) compares A to B
• a subtraction that only changes the flag bits
• useful for checking the entire contents of a register or a memory
location against another value
• usually followed by a conditional jump instruction
CMP AL, 10h ;compare with 10h (contents of AL does not change)
JAE SUPER ;if 10h or above then jump to memory location SUPER
Numerical Comparison
Condition Code Settings
CMP Oprnd1, Oprnd2
Z: equality/inequality Z: equality/inequality
BX AX
AX – BX = 6 – (-3) = 6 + 3 = 9 = 1001
So s = 1, overflow (o = 1)
-8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 Therefore AX >= BX
AX BX
AX – BX = 2 – 4 = -2 = 1110
So s = 1, no overflow (o = 0)
-8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 Therefore AX < BX
15
Conditional Branching
(cont.)
• Terminology used to differentiate between jump
instructions that use the carry flag and the overflow flag
• Above/Below unsigned compare
• Greater/Less signed (+/-) compare
Summary of
Conditional Jump Instructions
Command Description Condition
Summary of
Conditional Jump Instructions
Command Description Condition
CMP AX, BX
JA true_label
….
<False Processing>
….
JMP done_label
….
true_label: <True processing>
….
done_label: <resume execution>
19
Examples
; if (J <= K) then ; while (J >= K) do begin
; L := L + 1 ; J := J - 1;
; else L := L - 1 ; K := K + 1;
; J, K, L are signed words ; L := J * K;
; end;
WhlLoop:
MOV AX, J MOV AX, J
CMP AX, K CMP AX, K
JNEL DoElse JNGE QuitLoop
INC L DEC J
JMP ifDone INC K
DoElse: MOV AX, J
DEC L IMUL AX, K
ifDone: MOV L, AX
JMP WhlLoop
QuitLoop:
21
LOOP Instruction
• LOOP instruction
• Combination of a decrement CX and a conditional jump
• LOOP decrements CX (ECX if in 32-bit mode) and if CX 0 it
jumps to the address indicated by the label
• if CX becomes a 0, the next sequential instruction executes
ADDS PROC NEAR
MOV CX, 100 ; load count
MOV SI, OFFSET BLOCK1 ; BLOCK1 in DS
MOV DI, OFFSET BLOCK2 ; BLOCK2 in ES
Again:
LODSW ;get Block1 data; AX =DS:[SI]; SI = SI + 2
ADD AX, ES:[DI] ;add Block2 data
STOSW ;store in Block2; ES:[DI] = AX; DI = DI + 2
LOOP Again ;repeat 100 times
RET
ADDS ENDP
22
LOOPNE Instruction
• The LOOPNE instruction is useful for controlling loops that stop on
some condition or when the loop exceeds some number of iterations
…..
String1 BYTE “This string contains lower case characters”, 0
String2 BYTE 128 dup (0)
……..
23