0% found this document useful (0 votes)
40 views21 pages

Flow Control Instructions

Computer Organization and Assembly Language

Uploaded by

sugarbshopsa
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)
40 views21 pages

Flow Control Instructions

Computer Organization and Assembly Language

Uploaded by

sugarbshopsa
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/ 21

Boolean and Comparison

Instructions
Sir. Rameez Raja
Boolean Operation in Assembly Language
The four basic operations of Boolean algebra:
AND, OR, XOR, and NOT.
 These operations can be carried at the binary bit level, using assembly
language instructions.
 The techniques used here could be used to manipulate control bits
for hardware devices, implement communication protocols, or
encrypt data.
Selected Boolean Instructions
The CPU Flags

Boolean instructions affect the Zero, Carry, Sign, Overflow, and Parity
flags.
• The Zero flag is set when the result of an operation equals zero.
• The Carry flag is set when an operation generates a carry out of the
highest bit of the destination operand.
• The Sign flag is a copy of the high bit of the destination operand,
indicating that it is negative if set and positive if clear. (Zero is
assumed to be positive.)
• The Overflow flag is set when an instruction generates an invalid
signed result.
• The Parity flag is set when an instruction generates an even number
of 1 bits in the low byte of the destination operand.
AND Instruction
The AND instruction performs a Boolean (bitwise) AND operation
between each pair of matching bits in two operands and places the
result in the destination operand:
AND destination, source
Rules:
The following operand combinations are permitted:
AND reg, reg
AND reg, mem
AND mem, reg
 The operands can be 8, 16, or 32 bits, and they must be the same
size.
 For each matching bit in the two operands, the following rule applies:
If both bits equal 1, the result bit is 1; otherwise, it is 0.
Bit Masking
The AND instruction lets you clear 1 or more bits in an operand without
affecting other bits. The technique is called bit masking.
For example:
Suppose AL is initially set to 10101110 binary. After ANDing it with
11110110, AL equals 10100110:

mov al, 10101110b


and al, 11110110b ; result in AL = 10100110
; clear bits 0 and 3, leave others unchanged
Flags:
• The AND instruction always clears the Overflow and Carry flags.
• It modifies the Sign, Zero, and Parity flags in a way that is consistent
with the value assigned to the destination operand.
Converting Characters to Upper Case

The AND instruction provides an easy way to translate a letter from


lowercase to uppercase.
If we compare the ASCII codes of capital A and lowercase a, it becomes
clear that only bit 5 is different:
0 1 1 0 0 0 0 1 = 61h ('a')
0 1 0 0 0 0 0 1 = 41h ('A')
Example:
If we AND any character with 11011111 binary, all bits are unchanged except for bit
5, which is cleared.
In the following example, all characters in an array are converted to uppercase:
.data
array BYTE 50 "This Sentence is in Mixed Case“, 0
.code
mov ecx, LENGTHOF array
mov esi, OFFSET array
L1:
and BYTE PTR [esi], 11011111b ; clear bit 5
inc esi
loop L1
OR Instruction
The OR instruction performs a Boolean OR operation between each
pair of matching bits in two operands and places the result in the
destination operand:
OR destination, source
Rules:
• The OR instruction uses the same operand combinations as the AND
instruction:
OR reg, reg
OR reg, mem
OR mem, reg
• The operands can be 8, 16, or 32 bits, and they must be the same
size.
• For each matching bit in the two operands, the output bit is 1 when
at least one of the input bits is 1 .
• Flags The OR instruction always clears the Carry and Overflow flags.
Application of OR
The OR instruction is particularly useful when you need to set 1 or more bits in
an operand without affecting any other bits.
Suppose, for example, that your computer is attached to a servo
motor, which is activated by setting bit 2 in its control byte.
Example:
If AL is initially equal to 11100011 binary and then we OR it with 00000100, the
result equals 11100111:

MOV al,11100011b
OR al,00000100b ; result in AL = 11100111
Bit-Mapped Sets

Binary bits can indicate set membership. Rather than holding pointers
or references to objects in a container such as a Java HashSet, an
application can use a bit vector (or bit map) to map the bits in a binary
number to an array of objects.
We can easily check for set membership
by ANDing a particular member’s bit position with a 1:

mov eax, SetX


and eax, 10000b ; is element[16] a member of SetX?
Set Complement
The complement of a set can be generated using the NOT instruction,
which reverses all bits.
Therefore, the complement of the 101111 that we introduced is
generated in EAX using the following instructions:

mov eax,101111
not eax ; complement is 010000
Set Intersection
The AND instruction produces a bit vector that represents the intersection of
two sets. The following code generates and stores the intersection of SetX
and SetY in EAX:
mov eax, SetX
and eax, SetY
This is how the intersection of SetX and SetY is produced:
1000000 00000000 00000000 00000111 (SetX)
(AND) 1000001 01010000 00000111 01100011 (SetY)
----------------------------------------------------
1000000 00000000 00000000 00000011 (intersection)
Set Union
The OR instruction produces a bit map that represents the union of
two sets. The following code
generates the union of SetX and SetY in EAX:
MOV eax, SetX
OR eax, SetY
This is how the union of SetX and SetY is generated by the OR
instruction:
1000000 00000000 00000000 00000111 (SetX)
(OR) 1000001 01010000 00000111 01100011 (SetY)
------------------------------------------------------
1000001 01010000 00000111 01100111 (union)

You might also like