EL213 Computer Organization & Assembly Language Lab 08: Conditional Processing
EL213 Computer Organization & Assembly Language Lab 08: Conditional Processing
EL213 Computer Organization & Assembly Language Lab 08: Conditional Processing
Computer Conditional
Organization & Processing
Assembly Language
A programming language that permits decision making lets you alter the flow of control,
using a technique known as conditional branching.
- AND Instruction
It is boolean AND operation between a source operand and destination operand. If both
bits equal 1, the result bit is 1; otherwise, it is 0.
The AND instruction always clears the Carry and Overflow flags. It modifies the Sign,
Zero,
and Parity flags in a way that is consistent with the value assigned to the destination
operand.
- OR Instruction
It is boolean AND operation between a source operand and destination operand. For each
matching bit in the two operands, the output bit is 1 when at least one of the input bits is 1.
mov al,11100011b
or al,00000100b ; result in AL = 11100111
Page | 1
The OR instruction always clears the Carry and Overflow flags. It modifies the Sign, Zero,
and Parity flags in a way that is consistent with the value assigned to the destination
operand.
- XOR Instruction
The XOR instruction performs a boolean exclusive-OR operation between each pair of
matching bits in two operands and stores the result in the destination operand. If both bits
are the same (both 0 or both 1), the result is 0; otherwise, the result is 1.
The XOR instruction always clears the Overflow and Carry flags. XOR modifies the Sign,
Zero, and Parity flags.
- NOT Instruction
The NOT instruction toggles (inverts) all bits in an operand. The result is called the one’s
complement.
- TEST Instruction
The TEST instruction performs an implied AND operation between each pair of matching
bits in two operands and sets the Sign, Zero, and Parity flags based on the value assigned
to the destination operand.
The only difference between TEST and AND is that TEST does not modify the destination
operand.
Syntax: TEST reg, reg
TEST reg, mem
TEST reg, imm
TEST mem, reg
TEST mem, imm
Page | 2
Testing Multiple Bits: The TEST instruction can check several bits at once. Sup-pose we
want to know whether bit 0 or bit 3 is set in the AL register. We can use the following
instruction to find this out:
EXAMPLE
.code
mov al, 10101110b ; Clear only bit 3
and al, 11110110b ; AL = 10100110
call DumpRegs
exit
Set Complement
The complement of a set can be generated using the NOT instruction, which reverses all
bits.
Set Intersection
The AND instruction produces a bit vector that represents the intersection of two sets.
Page | 3
Set Union
The OR instruction produces a bit map that represents the union of two sets.
EXAMPLE
.data
A DWORD 10000000000000000000000000000111b
B DWORD 10000001010100000000011101100011b
msg1 BYTE "A intersection B is: ", 0
msg2 BYTE "A union B is: ", 0
msg3 BYTE "Complement of A is: ", 0
.code
mov eax, A
and eax, B ; A intersection B
mov edx, OFFSET msg1
call WriteString
mov ebx, TYPE DWORD
call WriteBinB
call Crlf
mov eax, A
or eax, B ; A union B
mov edx, OFFSET msg2
call WriteString
mov ebx, TYPE DWORD
call WriteBinB
call Crlf
mov eax, A
not eax ; A complement
mov edx, OFFSET msg3
call WriteString
mov ebx, TYPE DWORD
call WriteBinB
exit
- CMP Instruction
CMP (compare) instruction performs an implied subtraction of a source operand from
a destination operand for comparison. Neither operand is modified.
Syntax:
CMP destination, source
Page | 4
Flags: The CMP instruction changes the Overflow, Sign, Zero, Carry, Auxiliary Carry,
and Parity flags according to the value the destination operand.
Examples: Let’s look at three code fragments showing how flags are affected by the CMP
instruction.
.code
mov ax, 5
cmp ax, 10 ; ZF = 0 and CF = 1
- Conditional Jumps
Two steps are involved in executing a conditional statement:
1. an operation such as CMP, AND, or SUB modifies the CPU status flags.
2. a conditional jump instruction tests the flags and causes a branch to a new address.
- Jcond Instruction
A conditional jump instruction branches to a destination label when a status flag condition
is true.
Syntax:
Page | 5
The conditional jump instructions can be divided into four groups:
Page | 6
• Jumps based on signed comparisons
EXAMPLE
; This program compares and finds larger of the two integers
.data
var1 DWORD 250
var2 DWORD 125
larger DWORD ?
.code
mov eax, var1
mov larger, eax
mov ebx, var2
cmp eax, ebx
jae L1
mov larger, ebx
L1:
exit
EXAMPLE
; This program compares and finds smallest of the three integers
.data
var1 DWORD 50
var2 DWORD 25
var3 DWORD 103
msg BYTE "The smallest integer is: ", 0
.code
mov eax, var1
Page | 7
cmp eax, var2
jbe L1
mov eax, var2
L1:
cmp eax, var3
jbe L2
mov eax, var3
L2:
mov edx, OFFSET msg
call WriteString
call crlf
call WriteDec
call crlf
exit
EXAMPLE
; The following program continues a loop until an alphanumeric key is pressed
.data
char BYTE ?
.code
L1:
mov eax, 10 ; create 10ms delay
call Delay
call ReadKey ; reads a key input
jz L1 ; repeat if no key is pressed
mov char, al ; saves the character
The syntax is
LOOPZ destination
Page | 8
The LOOPE (loop if equal) instruction is equivalent to LOOPZ and they share the same
opcode.
The syntax is
LOOPNZ destination
The LOOPNE (loop if not equal) instruction is equivalent to LOOPNZ and they share the
same opcode.
Example:
- Conditional Structures
We define a conditional structure to be one or more conditional expressions that trigger
a choice between different logical branches. Each branch causes a different sequence
of instructions to execute.
- Block-Structured IF Statements
An IF structure implies that a boolean expression is followed by two lists of statements;
one per-formed when the expression is true, and another performed when the expression
is false.
Page | 9
If structure: In High level Vs Assembly Language
Page | 10
- Compound Expression with AND
When implementing the logical AND operator in compound expression, if the first
expression is false, the second expression is skipped.
- While Loops
A WHILE loop is really an IF statement followed by the body of the loop,
followed byan unconditional jump to the top of the loop.
Page | 11
Page | 12
ACTIVITIES:
(a)
var = 5
if ( var < ecx ) AND (ecx >= edx) then
x=0
else
x=1
(b)
var = 0
while ( var <= 10)
if (var % 2 == 0)
Print “Hello”
else
Print “World”
var = var + 1
end while
3. Use cmp and jumps to find the first non-zero value in the given array:
intArr SWORD 0, 0, 0, 0, 1, 20, 35, -12, 66, 4, 0
4. Write a program that takes four input integers from the user. Then compare and display
a message whether these integers are equal or not.
6. Write a program for sequential search. Take an input from the user and find if it occurs
in the following array:
arr WORD 10, 4, 7, 14, 299, 156, 3, 19, 29, 300, 20
Swap_Count = 0
for all elements of list
if list[i] > list[i+1]
swap(list[i], list[i+1])
Swap_Count = Swap_Count + 1
end if
end for
Print Swap_Count
Page | 13