You are on page 1of 18

Branching & Flow of Control

Assembly Language Programming


Unconditional Jump

• JMP [operator] destination


• Intrasegment direct E9 disp16
• Intrasegment direct short EB disp8
• Intrasegment indirect FF mod 100 r/m ??
• Intersegment direct EA disp16 seg16
• Intersegment indirect FF mod 101 r/m ??
• operator : SHORT, NEAR PTR, or FAR PTR
• NEAR PTR is the usual default

2/4/2015 Ali Saleh - Assembly Language Programming 2


Specifying the Jump Target

• Instruction Label
• A symbolic name defined to be an address in the code
segment of a program
• A label may be attached to any point in the code of a
program
• a_Label: mov ax,bx
• inc si
• jmp a_Label
• Labels definitions usually have a colon at the end
signifying them as NEAR

2/4/2015 Ali Saleh - Assembly Language Programming 3


Executing a Jump

• Intrasegment jumps are caused by changing the IP register to a new


value
• Short jumps add a signed 8-bit displacement to IP
• Near jumps add a signed 16-bit displacement to IP
• Intersegment jumps change both the CS and IP registers
• Far jumps simply assign new values to these registers

2/4/2015 Ali Saleh - Assembly Language Programming 4


Sample Jump Encodings
1106:0100 JMP lbl1 EB2A
1106:0102 NEG cx

lbl1:
1106:012C mov ax,bx
• 012C-0102=002A
--------------------------------------------
lbl1:
1106:0100 mov ax,bx
1106:0102 JMP lbl1 EBFC
1106:0104 .. ..
• 0100-0104=-4=FC
--------------------------------------------

1106:0107 JMP lbl1 E9F5FE


...
1106:FFFF lbl1:
• FFFF-010A=FEF5

2/4/2015 Ali Saleh - Assembly Language Programming 5


Conditional Jumps

• Jxxx destination
• There are 30 some variations that interrupt sequential flow based on various
flag settings
• JNZ - Jump if zero flag is clear (0) meaning the result of a previous
operation was non-zero
• JC - Jump if a previous operation caused the carry flag to be set (1)

2/4/2015 Ali Saleh - Assembly Language Programming 6


Table 11.1
Some Conditional Jump Instructions
Operation Mnemonic Opcode Branch Test
Jump on equal zero JE/JZ 74 Z=1
Jump on not equal zero JNE/JNZ 75 Z=0
Jump on not sign JNS 79 S=0
Jump on sign JS 78 S=1
Jump on not below (not carry) JNB/JAE/JNC 73 C=0
Jump on below (carry) JB/JNAE/JC 72 C=1
Jump on overflow JO 70 O=1
Jump on not overflow JNO 71 O=0
Jump on parity (even) JP/JPE 7A P=1
Jump on not parity (odd) JNP/JPO 7B P=0

2/4/2015 Ali Saleh - Assembly Language Programming 7


Range of Conditional Jumps

• All conditional jumps are SHORT


• range is -128 to +127 bytes
• 80386+ allow larger distances
• Combine a conditional and unconditional jump to overcome this
range limitation
Problem Alternative
jz too_far ; jnz is_close
;use code at jmp near ptr too_far
;right! is_close:

2/4/2015 Ali Saleh - Assembly Language Programming 8


Using Conditional Jumps

• Conditional jumps typically follow an instruction that alters the flag


bits
• CMP destination, source
• Computes (destination-source) and sets flag bits
• result is not stored
• flags allow us to decide <, <=, >, >=, ==, <>, etc
• we can also interpret the results meaningfully for signed or unsigned data

2/4/2015 Ali Saleh - Assembly Language Programming 9


Implementing an IF-THEN

unsigned int n; ;if (n>7)


if (n>7) do_it(); mov ax,n
• If n is a signed int, use cmp ax,7
jng (not greater) jna skip_it
• unsigned: ;then-part
• above, below call do_it
• signed ;end if
• less, greater skip_it:

2/4/2015 Ali Saleh - Assembly Language Programming 10


Implementing an IF-then-ELSE

char n; ;if (n=='7')


if (n=='7') cmp n,'7'
do_it(); jne else_label
else ;then-part
do_that(); call do_it
• Document the control jmp short endif_label
structures and keep the
parts in the usual order else_label:
call do_that
Endif_label:

2/4/2015 Ali Saleh - Assembly Language Programming 11


Implementing a WHILE

int n=12; Mov n,12


while (n>0) n=n-2; ;while (n>0)
while_label:
• This loop could be
optimized by keeping n cmp n,0
in a register and storing jle end_while
to memory only at end ;loop-body
of loop sub n,2
jmp while_label
end_while:

2/4/2015 Ali Saleh - Assembly Language Programming 12


Compound Conditions

char n; int w,x; ;if(n>='A'&&w==x)


if (n>='A' && w==x) cmp n,'A'
jl no_go
whatever();
mov ax,w
• This example uses
cmp ax,x
short-circuit evaluation
• if the first condition is jne no_go
false it immediately skips ;then-part
past the then-part call whatever
no_go:

2/4/2015 Ali Saleh - Assembly Language Programming 13


Compound Conditions - OR

char n,k; unsigned int w; ;if(n<>k||w<=10)


if (n<>k || w<=10) mov ah,n
cmp ah,k
whatever();
jne then_
• This example uses
cmp w,10
short-circuit evaluation
• if the first condition is ja end_if
true it immediately skips then_:
to the then-part call whatever
end_if:

2/4/2015 Ali Saleh - Assembly Language Programming 14


LOOP
n=0;
• LOOP destination
• decrements CX but does for (x=9;x>0;x--)
not change any flags n=n+x;
• if CX is not zero after the
decrement, control is
transferred to the ;for(x=9;x>0;x--)
destination label mov n,0
• This is a SHORT jump only mov cx,9
top_loop:
add n,cx ;n=n+x
loop top_loop

2/4/2015 Ali Saleh - Assembly Language Programming 15


JCXZ destination

• Directly compares CX to ;while(x>0)do_it();


0 and jumps to the mov cx,x
destination if equal jcxz skip_it
• This instruction does top_loop:
not affect the flags call do_it
• It is commonly used to loop top_loop
bypass the first iteration skip_it:
of a loop if the count is
already 0

2/4/2015 Ali Saleh - Assembly Language Programming 16


LOOPZ/E and LOOPNZ/E

• Enhancement of the • Remember that LOOP


LOOP instruction decrements CX, but this
• The state of the ZERO does not affect the
Flag may also cause flags!
loop termination • LOOPZ == LOOPE
• Loop while ZF/equal && • LOOPNZ==LOOPNE
CX!=0 • Some action inside the
• Loop while (NZ/ not loop should affect the
equal) && CX!=0 zero flag (cmp ?)

2/4/2015 Ali Saleh - Assembly Language Programming 17


LOOPNE Example

• This program accepts at mov ah,1


most 9 characters from mov cx,9
the keyboard next_char:
• When the 9th character int 21h
is pressed (or the enter cmp al,13
key is used) the loopne next_char
number of keypresses ;determine count
is displayed mov ax, 0239h
sub al,cl
mov dl,al
int 21h

2/4/2015 Ali Saleh - Assembly Language Programming 18

You might also like