Control Structures
Control Structures
Adelaiye OJ
Objectives
Definition
Conditional Statement
Compare
Jump Functions
• Loops
• Relationship With HLL Syntax
Control Structures
• Sequence: {S1, S2,….sk}
• Conditional: if statement, case statement
• Loop: while loop, for loop
• Jump: goto, exception raising
Control structures
• A control structure is a set of instructions that
analyses variables and chooses a direction in
which to go based on given parameters.
• The term flow control details the direction the
program takes
• The control structure changes either by
conditional statements
• Conditional execution in assembly language is
accomplished by several looping and branching
instructions. These instructions can change the
flow of control in a program.
Conditional instructions
• Unconditional jump
• This is performed by the JMP instruction
• Conditional execution often involves a transfer of
control to the address of an instruction that does
not follow the currently executing instruction.
• Transfer of control may be forward, to execute a
new set of instructions
• Or backward, to re-execute the same steps
Conditional instructions
• Conditional jump
• This is performed by a set of j-conditions
depending upon the condition
• The conditional instructions transfer the control
by breaking the sequential flow and they do it by
changing then offset value in IP.
CMP Instruction
• The CMP instruction compares two operands
• It is generally used in conditional execution
• This instruction basically subtracts one operands
from the other for comparing whether the
operands are equal or not
• It does not disturb the destination or source
operands. It is used along with the conditional
jump instruction for decision making
SYNTAX
• The syntax showing how compare is used is:
CMP
destination, source
• CMP compares two numeric data fields.
• The destination operand could be either in
register or in memory
• The source operand could be a constant
(immediate) data, register or memory
Examples
CMP DX,00: Compare the DX value
with zero
JE L7: if yes jump to label L7
.
.
L7:….
Example 2:
CMP is often used for comparing
whether a counter value has reached
the number of times a loop needs to b e
run.
INC EDX
CMP EDX,10 ; Compares whether the
;counter has reached 10
JLE LP1 ; if it is less than or equal to
10
Unconditional Jump
• Performed by the JMP instructions.
• Conditional execution often involves a transfer of
control to the address of an instruction that does
not follow the currently executing instruction.
• Transfer of control may be forward, to execute a
new set of instructions or backward, to re-
execute the same steps.
SYNTAX
• The JMP instruction provides a label name where
the flow of control is transferred immediately.
JMP label
Example
MOV AX, 00 ; Initializing AX to 0
MOV BX, 00 ;initializing BX to 0
MOV CX, 01 ; Initializing CX to 1
L20:
ADD AX, 01 ; Increment AX
ADD BX, AX ; Add AX to BX
SHL CX,1 ;shift left CX, this in turn doubles the
CX values
JMP L20 ; repeats the statements
Conditional Jump
• If some specified condition is satisfied in
conditional jump, the control flow is transferred
to a target instruction.
• There are numerous conditional jump instructions
depending upon the condition and data.
Conditional jump
• Following are the conditional jump instructions
used on signed data used for arithmetic
Instruction Description Flags tested
operations
JE/JZ Jump Equal or jump ZF
zero
JNE/JNZ Jump not equal or ZF
jump not zero
JG/JNLE Jump Greater or jump OF, SF, ZF
not less/equal
JGE/JNL Jump Greater/equal OF, SF
or jump not less
JL/JNGE Jump less or jump not OF, SF
greater/equal
JLE/JNG Jump less/equal or OF, SF, ZF
jump not greater
Conditional jump
• Following are the conditional jump instructions
used on unsigned data used for logical operations
Instruction Description Flags tested
JE/JZ Jump Equal or jump ZF
zero
JNE/JNZ Jump not equal or ZF
jump not zero
JA/JNBE Jump Above or jump CF, ZF
not below/equal
JAE/JNB Jump Above/equal or CF
jump not below
JB/JNAE Jump below or jump CF
not Above/equal
JBE/JNA Jump below/equal or AF,CF
jump not Above
Conditional jump
• The following conditional jump instructions have
special uses and check the values of flags
Instruction Description Flags tested
JXCZ Jump if CX is zero None
JC Jump if carry CF
JNC Jump if no carry CF
JO Jump if overflow OF
JNO Jump if no overflow OF
JP/JPE Jump Parity or Jump PF
parity Odd
JNP/JPO Jump no parity or PF
Jump parity odd
JS Jump Sign (negative SF
value)
Example
CMP AL, BL
JE EQUAL
CMP AL, BH
JE EQUAL
CMP AL, CL
JE EQUAL
NON_EQUAL;…
EQUAL;….
Sequence
• Pascal; begin…. End
• C, C++, Java; {…}
• Ada; Brackets for sequence are unnecessary.
Keywords for control structures suffice.
for J in 1.. N loop … end loop
• ABC, Python; Indicate structure by indentation
Semicolons
• Pascal; Semicolons are separators
• C etc.; Semicolons are terminators