Boolean Operations
Boolean Operations
Statement Purpose:
Objective of this lab is to introduce students to work with Boolean operators i.e. AND, OR, NOT and XOR and to know the impact of these operators on the destination operand. Further, TEST and CMP instructions are introduced which are the advance comparison instruction based on AND instruction. They will know various types of operation those can be performed on bits to make them set, clear, mask, unmask etc. They will know applications of bit manipulation as well.
Activity Outcomes:
This lab teaches you the following topics:
- Students will know that programmer in assembly can efficiently manipulate bits to do some processing more effective like knowing the bit combination.
- They will know interesting property of bit level instruction to set, clear and toggle bits and they will be able to apply those in relevant applications.
Instructor Note:
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. The TEST instruction permits the same operand combinations as the AND instruction.
At the heart of any Boolean expression is some type of comparison. In Intel assembly language we use the CMP instruction to compare integers. The CMP (compare) instruction performs an implied subtraction of a source operand from a destination operand.
- Neither operand is modified:
- CMP destination, source
- CMP uses the same operand combinations as the AND
When two unsigned operands are compared, the Zero and Carry flags indicate the following relations between operands:
When two signed operands are compared, the Sign, Zero, and Overflow flags indicate the following relations between operands:
Introduction
The AND, OR, XOR, NOT, and TEST instructions are called bitwise instructions because they work at the bit level. Each bit in a source operand is matched to a bit in the same position of the destination operand:
- The AND instruction produces 1 when both input bits are 1.
- The OR instruction produces 1 when at least one of the input bits is
- The XOR instruction produces 1 only when the input bits are different.
- The NOT instruction reverses all bits in a destination
Operation | Description |
AND | Boolean AND operation between a source operand and a destination operand. |
OR | Boolean OR operation between a source operand and a destination operand |
XOR. | Boolean exclusive-OR operation between a source operand and a destination operand |
NOT | Boolean NOT operation on a destination operand. |
Encryption is a process that encodes data, and decryption is a process that decodes data. The XOR instruction can be used to perform simple encryption and decryption.
Here are the selected Boolean Operations that the students will be introduced in Lab via practical examples.
Activities:
Activity 1:
How AND instruction is used to convert characters to uppercase?
Solution:
TITLE Lab Boolean Operators
; Author: Ashfaq Hussain Farooqi INCLUDE Irvine32.inc
.data
.code
main PROC
mov eax, 0 mov al, ‘a’ call dumpregs call crlf
call writebin call crlf
call writechar call crlf
and al,11011111b call writechar
call crlf
exit main ENDP END main
Activity 2:
How OR instruction is used to make ASCII code of input digits 0 to 9?
Solution:
TITLE Lab Boolean Operators
; Author: Ashfaq Hussain Farooqi INCLUDE Irvine32.inc
.data
.code
main PROC
mov eax, 0 mov al,1
call dumpregs call crlf
call writebin call crlf
call writechar call crlf
or al,00110000b call writechar
call crlf
call dumpregs
exit
main ENDP END main
Activity 3:
TEST Instruction: No Flag affected as the output is nor zero neither using sign bit
Solution:
TITLE Lab Boolean Operators
; Author: Ashfaq Hussain Farooqi INCLUDE Irvine32.inc
.data
.code
main PROC
mov eax, 0
mov ebx, 5
add ebx, 6
mov al, 00110110b call dumpregs
test al, 10001100b call dumpregs
exit main ENDP END main
Activity 4:
TEST Instruction: Flag affected as the output is not zero but setting sign bit
Solution:
TITLE Lab Boolean Operators
; Author: Ashfaq Hussain Farooqi INCLUDE Irvine32.inc
.data
.code
main PROC
mov eax, 0
mov ebx, 5
add ebx, 6
mov al, 10101010b call dumpregs
test al, 10010000b call dumpregs
exit main ENDP END main
Activity 5:
TEST Instruction: Flag affected as the output is zero Solution:
TITLE Lab Boolean Operators
; Author: Ashfaq Hussain Farooqi
INCLUDE Irvine32.inc
.data
.code
main PROC
mov eax, 0
mov ebx, 5
add ebx, 6
mov al, 10101010b call dumpregs
test al, 01010000b call dumpregs
exit main ENDP END main
Activity 6:
CMP Instruction (Unsigned): D==S; Zero Flag affected Solution:
TITLE Lab Boolean Operators
; Author: Ashfaq Hussain Farooqi INCLUDE Irvine32.inc
.data
.code
main PROC
mov ebx, 5
add ebx, 6 call dumpregs mov eax, 5
cmp eax, 5 call dumpregs
exit main ENDP
END main
Activity 7:
CMP Instruction (Unsigned): D > S; No Flag affected Solution:
TITLE Lab Boolean Operators
; Author: Ashfaq Hussain Farooqi INCLUDE Irvine32.inc
.data
.code
main PROC
mov ebx, 5
add ebx, 6 call dumpregs mov eax, 5
cmp eax, 4 call dumpregs
exit main ENDP END main
Activity 8:
CMP Instruction (Unsigned): D<S; Carry Flag affected Solution:
TITLE Lab Boolean Operators
; Author: Ashfaq Hussain Farooqi INCLUDE Irvine32.inc
.data
.code
main PROC
mov ebx, 5
add ebx, 6 call dumpregs mov eax, 5
cmp eax, 6 call dumpregs
exit main ENDP END main
Activity 9:
CMP Instruction (Signed): D==S; Zero Flag affected Solution:
Solution:
TITLE Lab Boolean Operators
; Author: Ashfaq Hussain Farooqi INCLUDE Irvine32.inc
.data
.code
main PROC
mov ebx, 5
add ebx, 6 call dumpregs mov eax, -10
cmp eax, -10 call dumpregs
exit main ENDP
END main
Activity 10:
CMP Instruction (Signed): D>S; SF = OF Solution:
TITLE Lab Boolean Operators
; Author: Ashfaq Hussain Farooqi INCLUDE Irvine32.inc
.data
.code
main PROC
mov ebx, 5
add ebx, 6 call dumpregs mov eax, -10
cmp eax, -15 call dumpregs
exit main ENDP END main
Activity 11:
CMP Instruction (Signed): D < S; No Flag affected Solution:
TITLE Lab Boolean Operators
; Author: Ashfaq Hussain Farooqi INCLUDE Irvine32.inc
.data
.code
main PROC
mov ebx, 5
add ebx, 6 call dumpregs mov eax, -10
cmp eax, -5 call dumpregs
exit
main ENDP END main
Home Activities:
Activity 1:
Take a string from the user and display it in Capital letters. Activity 2:
Take a string from the user and display it in Small letters. Activity 3:
Take a string from the user called it a plain text. Convert it into the cipher text assuming key as 78h. display the cipher text. Again convert the cipher text into the original text. Note: You may use XOR command to cipher or encipher the text BYTE by BYTE.