Computer Organization and Assembly Language
Lab 6
Program Flow Control
Objectives
After completing this lab,
• Students will be able to transfer control to any label or address unconditionally.
• Students will be able to transfer control to any label or address based on some conditions.
• Students will be able to implement customized loops instead of using built-in loops.
• Students will be able to use opposite conditional jumps to reduce number of jump
instructions inside loop.
• Students will be able to make a long conditional jump using short conditional jump
instructions.
Computer Organization and Assembly Language
Control in a program can be unconditionally or conditionally transferred to any location
based on status flags. There are instructions for transferring control.
Unconditional Jump
The basic syntax of JMP instruction:
JMP label
The "JMP" instruction transfers control to the address or label followed by it. This
instruction can move control within or outside of the current code segment. In the
following table, example 1 shows the jump inside the current code segment, called a "near
jump," and example 2 shows the jump outside the current code segment, called a "long
jump." For a short jump, the address that comes after the "JMP" instruction will be an
offset. For a long jump, the address will be a logical address made up of a segment address
and an offset.
Example-1: Transferring control to label Example-2: Transferring control to address
within same code segment. outside current code segment.
.model small .model small
.data .data
.code .code
Mov ax,@data Mov ax,@data
Mov ds,ax Mov ds,ax
Jmp Label1 Jmp 0x07C0:0x0000
Mov AX, BX Mov AX, BX
Mov CX, DX Mov CX, DX
Lable1:
Mov AX,1
Mov BX,2
.exit .exit
Computer Organization and Assembly Language
Conditional Jumps
Unlike JMP instruction that does an unconditional jump, there are instructions that do a
conditional jump (jump only when some conditions are in act). These instructions are
divided in three groups, first group just test single flag, second compares numbers as
signed, and third compares numbers as unsigned. All these three groups use status flags to
jump.
We know that the status flags are modified because of ALU instructions. Therefore, to
compare two numbers, we usually perform a subtraction operation on these two numbers.
We are not concerned with the result of the subtract operation but with the status flags that
are updated. Therefore, instead of the "SUB" instruction, the "CMP" instruction is used,
which discards the result and keeps the status of flags.
The basic syntax of CMP instruction:
CMP Destination Operand, Source Operand
Jump instructions that test single flag
The following table shows the conditional jump instructions that jump based on the value of a
single flag. In the instruction column, some rows contain more than one instruction that does the
same thing. They are even assembled into the same machine code. JE will be assembled as JZ, JC
will be assembled as JB, and so on. These names are used to make programs easier to understand,
code, and remember.
Opposite
Instruction Description Condition
Instruction
JZ , JE Jump if Zero (Equal). ZF = 1 JNZ, JNE
JC , JB, Jump if Carry (Below, JNC, JNB,
CF = 1
JNAE Not Above Equal). JAE
JS Jump if Sign. SF = 1 JNS
JO Jump if Overflow. OF = 1 JNO
JPE, JP Jump if Parity Even. PF = 1 JPO
Jump if Not Zero (Not
JNZ , JNE ZF = 0 JZ, JE
Equal).
Jump if Not Carry
JNC , JNB,
(Not Below, Above CF = 0 JC, JB, JNAE
JAE
Equal).
JNS Jump if Not Sign. SF = 0 JS
JNO Jump if Not Overflow. OF = 0 JO
Computer Organization and Assembly Language
Jump if Parity Odd
JPO, JNP PF = 0 JPE, JP
(No Parity).
Jump instructions for signed numbers
To compare two signed numbers, the instructions shown in the following table are used right after
the ‘CMP’ instruction.
Instruction Description Condition Opposite Instruction
Jump if Equal (=).
JE, JZ ZF = 1 JNE, JNZ
Jump if Zero.
Jump if Not Equal (<>).
JNE, JNZ ZF = 0 JE, JZ
Jump if Not Zero.
ZF = 0
Jump if Greater (>).
JG, JNLE and JNG, JLE
Jump if Not Less or Equal (not <=).
SF = OF
Jump if Less (<).
JL, JNGE SF <> OF JNL, JGE
Jump if Not Greater or Equal (not >=).
Jump if Greater or Equal (>=).
JGE, JNL SF = OF JNGE, JL
Jump if Not Less (not <).
ZF = 1
Jump if Less or Equal (<=).
JLE, JNG or JNLE, JG
Jump if Not Greater (not >).
SF <> OF
Jump instructions for unsigned numbers
To compare two unsigned numbers, the instructions shown in the following table are used right
after the ‘CMP’ instruction.
Instruction Description Condition Opposite Instruction
Jump if Equal (=).
JE, JZ ZF = 1 JNE, JNZ
Jump if Zero.
Jump if Not Equal (<>).
JNE, JNZ ZF = 0 JE, JZ
Jump if Not Zero.
CF = 0
Jump if Above (>).
JA, JNBE and JNA, JBE
Jump if Not Below or Equal (not <=).
ZF = 0
Computer Organization and Assembly Language
Jump if Below (<).
JB, JNAE, JC Jump if Not Above or Equal (not >=). CF = 1 JNB, JAE, JNC
Jump if Carry.
Jump if Above or Equal (>=).
JAE, JNB, JNC Jump if Not Below (not <). CF = 0 JNAE, JB
Jump if Not Carry.
CF = 1
Jump if Below or Equal (<=).
JBE, JNA or JNBE, JA
Jump if Not Above (not >).
ZF = 1
Limitation of Conditional Jump instructions
• All conditional jumps have one big limitation, unlike JMP instructions, they are short (one-
byte jumps having a range from -128 to 127 bytes). However, we can easily avoid this
limitation using a cute trick:
o Get conditional jump instruction from the tables above and make it jump to label_X.
o Under that label_X, Use JMP instructions to jump to the desired location.
• Emu8086 uses this trick implicitly for conditional jumps.
Computer Organization and Assembly Language
Program 1: To swap values of AX and BX registers if AX < BX.
1a: Program with straight conditional jumps. 1b: Program with opposite
conditional jump to reduce jumps.
.model small .model small
.data .data
.code .code
Mov ax,@data Mov ax,@data
Mov ds,ax Mov ds,ax
Mov ax,5 Mov ax,5
Mov bx,10 Mov bx,10
Cmp ax,bx Cmp ax,bx
JS swap JNS exit_cmp
Jmp exit_cmp
Swap:
Swap: XCHG ax,bx
XCHG ax,bx
exit_cmp:
exit_cmp:
.exit
.exit
Computer Organization and Assembly Language
Program 2: To iterate a loop while AX < BX using conditional jump for unsigned
numbers.
1a: Program with straight conditional 1b: Program with opposite conditional
jumps. jump to reduce jumps.
.model small .model small
.data .data
.code .code
Mov ax,@data Mov ax,@data
Mov ds,ax Mov ds,ax
Mov ax,5 Mov ax,5
Mov bx,10 Mov bx,10
compare: compare:
cmp ax,bx cmp ax,bx
JB iterate JAE exit_loop
jmp exit_loop
iterate: iterate:
inc ax inc ax
jmp compare jmp compare
exit_loop: exit_loop:
.exit .exit
Computer Organization and Assembly Language
Program 3: To iterate a loop while AX < BX using conditional jump for signed
numbers.
1a: Program with straight conditional jumps. 1b: Program with opposite
conditional jump to reduce jumps.
.model small .model small
.data .data
.code .code
Mov ax,@data Mov ax,@data
Mov ds,ax Mov ds,ax
Mov ax,-5 Mov ax,-5
Mov bx,0 Mov bx,0
compare: compare:
cmp ax,bx cmp ax,bx
JL iterate JGE exit_loop
jmp exit_loop
iterate: iterate:
inc ax inc ax
jmp compare jmp compare
exit_loop: exit_loop:
.exit .exit
Computer Organization and Assembly Language
Emu8086 Tutorial Step by Step
Step-1:
Double click on the icon on the desktop
Step-2:
The following window will appear. Click on new.
Computer Organization and Assembly Language
Step-3: Click on empty workspace and press OK.
Step-4: Type the code given in program-3a above and click on emulate.
Computer Organization and Assembly Language
Step-5: Keep clicking on "Single Step" to execute program instructions one by one and
observe the register values side by side and the number of times the loop is iterated. Stop
clicking "Single Step" when the ".exit" is highlighted to observe the final value of the AX
register.
Computer Organization and Assembly Language
Practice Exercise
Task-1
Write a program that declares and initializes an array of 20 elements and then calculates the
number of occurrences of a specific number in the array.
Task-2
Write a program that declares and initializes a word-type array of 20 elements and sorts it using
any sorting algorithm of your choice.