0% found this document useful (0 votes)
22 views11 pages

Coal Lab-08

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views11 pages

Coal Lab-08

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

EL2003 Lab 08

Computer Conditional
Organization & Processing
Assembly Language

NATIONAL UNIVERSITY OF COMPUTER AND EMERGING SCIENCES


LAB 08
Learning Objectives
a. Boolean and Comparison Instructions
b. Conditional Jumps
c. Conditional Loop Instructions
d. Conditional Structures

Boolean and Comparison Instructions


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 operands can be 8, 16, or 32 bits,
and they must be the same size.

Syntax: AND reg, reg


AND reg, mem
AND reg, imm
AND mem, reg
AND mem, imm

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.
NOTE: The All other instructions use the same operand combinations and sizes as
the AND instruction.

- 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

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.
Page | 1
- 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.

Syntax: NOT reg


NOT mem

- 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.
Testing Multiple Bits: The TEST instruction can check several bits at once. Suppose 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

mov al, 11100011b ; set bit 2


or al, 00000100b ; AL = 11100111

mov al, 10110101b ; 5 bits means odd parity


xor al, 0 ;PF=0(PO)

mov al, 10100101b ; 4 bits means even parity


xor al, 0 ;PF=1(PE)
Page | 2
mov al, 11110000b
not al ; AL = 00001111b

mov al, 00100101b


test al, 00001001b ;ZF=0

mov al, 00100101b


test al, 00001000b ;ZF=1

call DumpRegs
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

Flags: The CMP instruction changes the Overflow, Sign, Zero, Carry, Auxiliary Carry,
and Parity flags according to the value the destination operand.

When two unsigned operands are compared, the Zero and Carry flags indicate
the following relations between operands:

 Destination < source ZF=0 CF=1


 Destination > source ZF=0 CF=0
 Destination = source ZF=1 CF=0

When two signed operands are compared, the Sign, Zero, and Overflow flags indicate the
following relations between operands:

 Destination < source SF!=OF


 Destination > source SF=OF
 Destination = source ZF=1

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
Page | 3
mov ax, 1000
cmp ax, 1000 ;ZF=1 and CF =0

mov si, 106


cmp si, 0 ;ZF=0 and CF=0

- Conditional Jumps

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:

The conditional jump instructions can be divided into four groups:

 Jumps based on Flag values

 Jumps based on Equality

Page | 4
 Jumps based on unsigned comparisons

 Jumps based on signed comparisons

EXAMPLE
; This program compares and finds larger of the two integers

INCLUDE Irvine32.inc
.data
var1 DWORD 500
var2 DWORD 125
larger DWORD ?
.code
main PROC
mov eax, var1
mov larger, eax
mov ebx, var2
cmp eax, ebx
jae L1
mov larger, ebx
L1:
exit
main ENDP
END main

Page | 5
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
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

- Conditional Loop Instructions

- LOOPZ and LOOPE Instructions


The LOOPZ (loop if zero) instruction works just like the LOOP instruction except that
it has one additional condition: The Zero flag must be set in order for control to transfer
to the destination label.

The syntax is

LOOPZ destination

The LOOPE (loop if equal) instruction is equivalent to LOOPZ and they share the
same opcode.

Page | 6
- LOOPNZ and LOOPNE Instructions
The LOOPNZ (loop if not zero) instruction is the counterpart of LOOPZ. The loop
continues while the unsigned value of ECX is greater than zero (after being
decremented) and the Zero flag is clear.

The syntax is LOOPNZ destination

The LOOPNE (loop if not equal) instruction is equivalent to LOOPNZ and they share the
same opcode.

Example:

; The following take input from user until user press 0

.code
mov ecx,5
L1:
CALL readInt
cmp eax, 0
LOOPNZ L1
call DumpRegs
- 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.

If structure: In High level Vs Assembly Language

Page | 7
If-else structure: In High level Vs Assembly Language

- Compound Expression with AND


When implementing the logical AND operator in compound expression, if the
first expression is false, the second expression is skipped.

Page | 8
- Compound Expression with OR
When implementing the logical OR operator in compound expression, if the
first expression is true, 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 | 9
ACTIVITY:

Task#1
Write a program that takes four input integers from the user. Then compare and display a
message whether these integers are equal or not.

Task#2
Use cmp and jumps to find the first non-zero value in the given array:
intArr SWORD 0, 0, 0, 150, 120, 35, -12, 66, 4, 0

Task#3 Implement the following given code in Assembly and Consider var = 5 , edx =
var+1 and counter value from array initialized in task#2.

if ( var < ecx ) AND (ecx >= edx) then


x=0
else
x=1
Task#4 Implement the following given code in Assembly and consider var = 0.

while ( var <= 10)


if (var < 5)
Print “Hello”
else
Print “World”
var = var + 1
end while
Task#5
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

Task#6
Write a program for bubble sort on this array.
arr WORD 10, 4, 7, 14, 299, 156, 3, 19, 29, 300, 20

Task#7
Write a program to print weekday based on given number.

Task#8
Write a program to check whether a character is alphabet or not.

Page | 10

You might also like