0% found this document useful (0 votes)
10 views

Control Structures

Uploaded by

peterodey067
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Control Structures

Uploaded by

peterodey067
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

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

begin X;= 1: {X=1:


Y;=2 Y=2:
end }
Conditionals
• If condition then statement – Pascal, Ada
• If (condition) Statement – C, C++, Java
• To avoid ambiguities, use end marker; end if, “}”
• To deal with alternatives, use keyword or
bracketing;
if conditions if (conditions)
then statements {statements}
else if Conditions else if
(conditions)
then Statements Then
If-else ambiguity
If Conditions if (conditions)
then then {
if conditions if (conditions)
then Statements {statements}
else statements else {statements}
end if }
end if
Loops
• A loop statement allows us to execute a
statement or group of statements multiple times.
• The JMP instruction can be used for implementing
loops.
• The processor instruction set, however, incudes a
group of loop instructions for implementing
iteration
Loops
• For example, the following code snippet can be
used for executing the loop-body 10 times.
MOV CL, 10
L1;
<LOOP-BODY>
DEC CL
JNZ L1
Loops
• While loop; test at beginning
while (condition) Statement
• Repeat loop; test at end
repeat Statement until condition
do statement while (condition)
• Breaking out; test in middle
loop statement
if (condition) then exit loop:
Statement
end loop
Implementation
• Consider these when implementing control
structures:
• Finite set of possibilities ; can build a table of
addresses
and
• Convert expression into table index:
• Compute value
• Transform into index
• Retrieve address of corresponding code fragment
• Branch to code fragment and execute
Syntax
• The basic LOOP instruction has the following
syntax
LOOP label
• Where:
• label is the target label that identifies the target
instruction as in the jump instructions.
• The Loop instruction assumes that the ECX
register contains the loop count.
• When the loop instruction is executed, the ECX
register is decremented and the control jumps to
the target label, until the ECX register value, i.e.,
Example
• The above code snippet can be written as
MOV ECX ,10
L1;
<loop body>
loop l1

You might also like