Basic Assembly Language
Chapter Five
Outline
Branch instruction
Conditional loop instruction
Comparisons
Conditional control Instructions
Decision Directive
JMP and LOOP Instruction
A transfer of control, or branch, is a way of altering the order in which
statements are executed. There are two basic types of transfers:
Unconditional Transfer: Control is transferred to a new location in all cases; a new address
is loaded into the instruction pointer, causing execution to continue at the new address. The
JMP instruction does this.
Conditional Transfer: The program branches if a certain condition is true. A wide variety
of conditional transfer instructions can be combined to create conditional logic structures.
The CPU interprets true/false conditions based on the contents of the ECX and Flags
registers.
Unconditional Transfer
CALL : Unconditional Call
This instruction is used to call a Subroutine (Procedure) from
a main program. Address of procedure may be specified
directly or indirectly.
There are two types of procedure depending upon whether it
is available in the same segment or in another segment.
i. Near CALL i.e., ±32K displacement.
ii. For CALL i.e., anywhere outside the segment.
Cont…
On execution this instruction stores the incremented IP & CS onto
the stack and loads the CS & IP registers with segment and offset
addresses of the procedure to be called.
RET: Return from the Procedure.
At the end of the procedure, the RET instruction must be executed. When it
is executed, the previously stored content of IP and CS along with Flags are
retrieved into the CS, IP and Flag registers from the stack and execution of
the main program continues further.
Cont…
INT N: Interrupt Type N.
In the interrupt structure of 8086, 256 interrupts are defined corresponding to
the types from 00H to FFH. When INT N instruction is executed, the type
byte N is multiplied by 4 and the contents of IP and CS of the interrupt
service routine will be taken from memory block in 0000 segment.
INTO: Interrupt on Overflow
This instruction is executed, when the overflow flag OF is set. This is
equivalent to a Type 4 Interrupt instruction.
JMP: Unconditional Jump
JMP Instruction The JMP instruction causes an unconditional transfer to a
destination, identified by a code label that is translated by the assembler into an
offset. The syntax is
JMP destination
When the CPU executes an unconditional transfer, the offset of destination is
moved into the instruction pointer, causing execution to continue at the new
location. Creating a Loop The JMP instruction provides an easy way to create a
loop by jumping to a label at the top of the loop:
top: . .
jmp top ;repeat the endless loop
JMP is unconditional, so a loop like this will continue endlessly unless another way is found
to exit the loop.
LOOP Instruction
The LOOP instruction, formally known as Loop According to ECX Counter, repeats a block of
statements a specific number of times. ECX is automatically used as a counter and is
decremented each time the loop repeats. Its syntax is
LOOP destination
The loop destination must be within -128 to +127 bytes of the current location counter. The
execution of the LOOP instruction involves two steps:
First, it subtracts 1 from ECX.
Next, it compares ECX to zero.
If ECX is not equal to zero, a jump is taken to the label identified by destination.
Otherwise, if ECX equals zero, no jump takes place, and control passes to the instruction following the loop.
In the following example, we add 1 to AX each time the loop repeats. When the loop ends, AX
= 5 and ECX = 0:
Nested Loops When creating a loop inside another loop, special
consideration must be given to the outer loop counter in ECX. You can
save it in a variable:
As a general rule, nested loops more
than two levels deep are difficult to
write. If the algorithm you’re using
requires deep loop nesting, move some
of the inner loops into subroutines.
Cont…
Here’s a short program that sums an array of 16-bit
integers.
LOOPNZ and LOOPNE
LOOPNZ (LOOPNE) is a conditional loop instruction
Syntax:
LOOPNZ destination
LOOPNE destination
Logic:
ECX ←ECX –1;
if ECX > 0 and ZF=0, jump to destination
Useful when scanning an array for the first element that matches a given value.
LOOPNZ
The following code finds the first positive value in an array:
Conditional jumps
Conditional Structures
There are no explicit high-level logic structures in the x86 instruction set, but you can
implement them using a combination of comparisons and jumps. Two steps are involved in
executing a conditional statement: First, an operation such as CMP, AND, or SUB modifies
the CPU status flags. Second, a conditional jump instruction tests the flags and causes a
branch to a new address. Let’s look at a couple of examples.
Example 1 The CMP instruction in the following example compares EAX to Zero. The JZ
(jump if Zero) instruction jumps to label L1 if the Zero flag was set by the CMP instruction:
Example 2 The AND instruction in the following example performs a bitwise AND on the DL register,
affecting the Zero flag. The JNZ (jump if not Zero) instruction jumps if the Zero flag is clear:
Generally, the conditional jump instructions syntax is given as
follows:
• Jcond destination, cond refers to a flag condition
identifying the state of one or more flags. The following
examples are based on the Carry and Zero flags:
Types of Conditional Jump Instructions
The x86 instruction set has a large number Table below shows a list of jumps based on the Zero,
Carry, Overflow, Parity, and Sign flags.
of conditional jump instructions.
They are able to compare signed and
unsigned integers and perform actions based
on the values of individual CPU flags.
The conditional jump instructions can be
divided into four groups:
Jumps based on specific flag values
Jumps based on equality between
operands or the value of (E)CX
Jumps based on comparisons of
unsigned operands
Jumps based on comparisons of signed
operands
Equality Comparisons
The table lists jump instructions based on
evaluating equality. In some cases, two operands
are compared; in other cases, a jump is taken Following are code examples that use the JE, JNE,
JCXZ, and JECXZ instructions. Examine the
based on the value of CX or ECX. In the table, the comments carefully to be sure that you understand
notations left Op and right Op refer to the left why the conditional jumps were (or were not) taken.
(destination) and right (source) operands in a
CMP instruction:
CMP left Op, right Op
The operand names reflect the ordering of
operands for relational operators in algebra. For
example, in the expression X < Y, X is called left
Op and Y is called right Op.
Unsigned Comparisons
Signed Comparisons Table below displays a list
Jumps based on comparisons of unsigned numbers are
of jumps based on signed comparisons. The
shown in Table below. The operand names reflect the
following instruction sequence demonstrates the
order of operands, as in the expression (leftOp < rightOp).
comparison of two signed values:
The jumps in Table are only meaningful when comparing
unsigned values. Signed operands use a different set of
jumps.
Cont…
In the following code examples, examine the comments to be sure you understand why the
jumps were (or were not) taken.
Conditional structures
In assembly, it is possible to use different control structures
like high-level languages.
Let’s start with the most common high-level control structure:
if-then-else in C or C++
In the following C++ code, two assignment statements are
executed if op1 is equal to op2:
if( op1 == op2 ) then
{ We translate this IF statement into assembly language
with a CMP instruction followed by conditional jumps.
X = 1; Y = 2;
}
Cont..
The following code implements the IF statement as efficiently as possible by allowing
the code to “fall through” to the two MOV instructions that we want to execute when the
Boolean condition is true:
mov ax,op1
if( op1 == op2 ) then cmp ax,op2 ;op1 == op2?
{ jne L1 ; no: skip next
mov X,1 ; yes: assign X and Y
X = 1; Y = 2; mov Y,2
} L1:
If we implemented the ==operator using JE, the resulting code would be slightly less compact (six instructions rather
than five): mov ax,op1
cmp ax,op2 ;op1 == op2?
je L1 ; yes: jump to L1
jmp L2 ; no: skip assignments
L1: mov X,1 ; assign X and Y
mov Y,2
L2:
Cont…
Nested IF statement
if op1 == op2 then
if X > Y then
call Routine1
else
call Routine2
end if
else
call Routine3
end if
The table shows the results of white box testing of
the sample code. In the first four columns, test
values have been assigned to op1, op2, X, and Y.
The resulting execution paths are verified in
columns 5 and 6.
Compound Expressions
Logical AND Operator Assembly language easily implements compound Boolean expressions containing
AND operators.
Consider the following pseudocode, in which the values being compared are assumed to be unsigned
integers:
Short-Circuit Evaluation The following is a straightforward implementation
if (al > bl) AND (bl > cl) then
using short circuit evaluation, in which the second expression is not evaluated
X=1
if the first expression is false. This is the norm for high-level languages:
end if
We can reduce the code to five instructions by
changing the initial JA instruction to JBE:
Logical OR Operator
When a compound expression contains subexpressions joined by the OR operator, the overall expression is
true if any of the subexpressions is true.
Let’s use the following pseudocode as an example:
In the following implementation, the code branches to L1 if the first expression is true; otherwise, it falls
through to the second CMP instruction. The second expression reverses the > operator and uses JBE
instead:
For a given compound expression there are multiple way of implementing in assembly language.
These character are used to control the cursor position. The 10 shifts cursor on new line with
same column no. and 13 returns the cursor to its initial position of line that is at start of the line!
WHILE Loops
A WHILE loop tests a condition first before performing a block of statements. As long as the
loop condition remains true, the statements are repeated.
The following loop is written in C++:
When implementing this structure in assembly language, it is convenient to
reverse the loop condition and jump to endwhile if a condition becomes true.
Assuming that val1 and val2 are variables, we must copy one of them to a
register at the beginning and restore the variable’s value at the end:
EAX is a proxy (substitute) for val1 inside the loop. References to
val1 must be through EAX. JNL is used, implying that val1 and val2
are signed integers.
Cont…
Example: IF statement Nested in a Loop High-level languages are particularly good at representing nested control
structures. In the following C++ code, an IF statement is nested inside a WHILE loop. It calculates the sum of all array
elements greater than the value in sample:
Decision Directive
Using the .IF Directive
Runtime Expressions
Relational and Logical Operators
MASM-Generated Code
.REPEAT Directive
.WHILE Directive
Runtime Expressions
.IF, .ELSE, .ELSEIF, and .ENDIF can be used to
evaluate runtime expressions and create block-
structured IF statements.
Examples
MASM generates "hidden" code for you, consisting of code labels, CMP and conditional jump
instructions.
Relational and Logical Operators
MASM-Generated Code
. . . unless you prefix one of the register operands with the SDWORD PTR operator.
Then a signed jump is generated.
.REPEAT Directive
Executes the loop body before testing the loop
condition associated with the .UNTIL directive.
Example
.WHILE Directive
Tests the loop condition before executing the loop
body The .ENDW directive marks the end of the loop.
Example: