SlideShare a Scribd company logo
1 of 32
CONDITIONAL PROCESSING
Lecture # 14
UniversityofCenteralPunjabFsdCampus
1
CHAPTER OVERVIEW
Programs that deal with hardware devices must be able to
manipulate individual bits in numbers. Data encryption and
compression also rely on bit manipulation.
 How can I use the Boolean operations introduced in Chapter 1
(AND, OR, NOT)?
 How do I write an IF statement in assembly language?
 How are nested-IF statements translated by compilers into
machine language?
 How can I set and clear individual bits in a binary number?
 How can I perform simple binary data encryption?
 How are signed numbers differentiated from unsigned
numbers in Boolean expressions?
UniversityofCenteralPunjabFsdCampus
2
CHAPTER OVERVIEW
 Boolean and Comparison Instructions
 Conditional Jumps
 Conditional Loop Instructions
 Conditional Structures
 Conditional Control Flow Directives
UniversityofCenteralPunjabFsdCampus
3
BOOLEAN AND COMPARISON INSTRUCTIONS
 CPU Status Flags
 AND Instruction
 OR Instruction
 XOR Instruction
 NOT Instruction
 Applications
 TEST Instruction
 CMP Instruction
UniversityofCenteralPunjabFsdCampus
4
STATUS FLAGS - REVIEW
 The Zero flag is set when the result of an operation
equals zero
 The Carry flag is set when an instruction generates
a result that is too large for the destination operand.
 The Sign flag is set if the destination operand is
negative, and it is clear if the destination operand is
positive
 The Parity flag is set when an instruction generates
an even number of 1 bits in the low byte of the
destination operand.
UniversityofCenteralPunjabFsdCampus
5
OPERATIONS WITH DESCRIPTION
UniversityofCenteralPunjabFsdCampus
6
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
 Syntax
 AND reg, reg
 AND reg, mem
 AND reg, imm
 AND mem, reg
 AND mem, imm
 The operands can be 8, 16, 32, or 64 bits, and they
must be the same size.
UniversityofCenteralPunjabFsdCampus
7
EXAMPLE
 AL is initially set to 10101110 binary
 ANDing it with 11110110
 AL equals ??
 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
UniversityofCenteralPunjabFsdCampus
8
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:
 The rest of the alphabetic characters have the
same relationship. If we AND any character with
11011111 binary, all bits are unchanged except for
bit 5
UniversityofCenteralPunjabFsdCampus
9
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
 Syntax
 OR reg,reg
 OR reg,mem
 OR reg,imm
 OR mem,reg
 OR mem,imm
UniversityofCenteralPunjabFsdCampus
10
EXAMPLE
 AL is initially equal to 11100011
 OR it with 00000100
 Al is equals to ??
 Flags 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.
11
UniversityofCenteralPunjabFsdCampus
XOR INSTRUCTION
 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
 XOR destination, source
 The XOR instruction uses the same operand
combinations and sizes as the AND and OR
instructions. 12
UniversityofCenteralPunjabFsdCampus
NOT INSTRUCTION
 The NOT instruction toggles (inverts) all bits in an
operand. The result is called the one’s complement.
 The following operand types are permitted:
 NOT reg
 NOT mem
 Flags No flags are affected by the NOT instruction.
13
UniversityofCenteralPunjabFsdCampus
CMP INSTRUCTION
 In x86 assembly language we use the CMP
instruction to compare integers.
 Compares the destination operand to the source
operand
 Syntax: CMP destination, source
 Example: destination == source
 Flags: The CMP instruction changes the
Overflow, Sign, Zero, Carry, Auxiliary Carry, and
Parity flags
14
UniversityofCenteralPunjabFsdCampus
CMP INSTRUCTION
 CMP is a valuable tool for creating conditional logic
structures. When you follow CMP with a conditional
jump instruction, the result is the assembly
language equivalent of an IF statement.
15
UniversityofCenteralPunjabFsdCampus
CMP INSTRUCTION- EXAMPLES
 When AX equals 5 and is compared to 10, the
Carry flag is set because subtracting 10 from 5
requires a borrow:
 mov ax,5
 cmp ax,10 ; ZF = 0 and CF = 1
16
UniversityofCenteralPunjabFsdCampus
SETTING AND CLEARING INDIVIDUAL CPU
FLAGS
 How can you easily set or clear the Zero, Sign, Carry,
and Overflow flags?
 To set the Zero flag, TEST or AND an operand with
Zero; to clear the Zero flag, OR an operand with 1:
17
UniversityofCenteralPunjabFsdCampus
SETTING AND CLEARING INDIVIDUAL CPU
FLAGS
 To set the Carry flag, use the STC instruction; to
clear the Carry flag, use CLC:
 stc ; set Carry flag
 clc ; clear Carry flag
18
UniversityofCenteralPunjabFsdCampus
WHAT'S NEXT
 Boolean and Comparison Instructions
 Conditional Jumps
 Conditional Loop Instructions
 Conditional Structures
 Conditional Control Flow Directives
19
UniversityofCenteralPunjabFsdCampus
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.
20
UniversityofCenteralPunjabFsdCampus
EXAMPLE
 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:
21
UniversityofCenteralPunjabFsdCampus
JCOND INSTRUCTION
 A conditional jump instruction branches to a label
when specific register or flag conditions are met
22
UniversityofCenteralPunjabFsdCampus
JUMPS BASED ON SPECIFIC FLAGS
23
UniversityofCenteralPunjabFsdCampus
JUMPS BASED ON EQUALITY
24
UniversityofCenteralPunjabFsdCampus
EXAMPLES
25
UniversityofCenteralPunjabFsdCampus
EXAMPLE
 Loop until Key Pressed In the following 32-bit code, a
loop runs continuously until the user presses a standard
alphanumeric key. The ReadKey method from the
Irvine32 library sets the Zero flag if no key is present in
the input buffer:
 .data
 char BYTE ?
 .code
 L1: mov eax,10 ; create 10 ms delay
 call Delay
 call ReadKey ; check for key
 jz L1 ; repeat if no key
 mov char,AL ; save the character 26
UniversityofCenteralPunjabFsdCampus
OTHER IMPORTANT EXAMPLES
 Application: Sequential Search of an Array
 Application: Simple String Encryption
27
UniversityofCenteralPunjabFsdCampus
WHAT'S NEXT
 Boolean and Comparison Instructions
 Conditional Jumps
 Conditional Loop Instructions
 Conditional Structures
 Conditional Control Flow Directives
28
UniversityofCenteralPunjabFsdCampus
CONDITIONAL LOOP INSTRUCTIONS
 LOOPZ and LOOPE
 LOOPNZ and LOOPNE
29
UniversityofCenteralPunjabFsdCampus
LOOPZ INSTRUCTIONS
 LOOPZ (loop if zero) instruction works just like the
LOOP instruction except that the Zero flag must be
set in order for control to transfer to the destination
label
 The syntax is
 LOOPZ destination
30
UniversityofCenteralPunjabFsdCampus
LOOPE INSTRUCTIONS
 The LOOPE (loop if equal) instruction is equivalent
to LOOPZ, and they share the same opcode. They
perform the following tasks:
 ECX = ECX - 1
 if ECX > 0 and ZF = 1, jump to destination
31
UniversityofCenteralPunjabFsdCampus
LOOPNE INSTRUCTIONS
 The LOOPNE (loop if not equal) instruction is
equivalent to LOOPNZ, and they share the same
opcode. They perform the following tasks:
 ECX = ECX - 1
 if ECX > 0 and ZF = 0, jump to destination
32
UniversityofCenteralPunjabFsdCampus

More Related Content

What's hot

N_Asm Assembly arithmetic instructions (sol)
N_Asm Assembly arithmetic instructions (sol)N_Asm Assembly arithmetic instructions (sol)
N_Asm Assembly arithmetic instructions (sol)Selomon birhane
 
assembly language programming organization of IBM PC chapter 9 part-2(decimal...
assembly language programming organization of IBM PC chapter 9 part-2(decimal...assembly language programming organization of IBM PC chapter 9 part-2(decimal...
assembly language programming organization of IBM PC chapter 9 part-2(decimal...Bilal Amjad
 
C programming-apurbo datta
C programming-apurbo dattaC programming-apurbo datta
C programming-apurbo dattaApurbo Datta
 
The future of DSLs - functions and formal methods
The future of DSLs - functions and formal methodsThe future of DSLs - functions and formal methods
The future of DSLs - functions and formal methodsMarkus Voelter
 
Unit 3 – assembly language programming
Unit 3 – assembly language programmingUnit 3 – assembly language programming
Unit 3 – assembly language programmingKartik Sharma
 
Branching instructions in 8086 microprocessor
Branching instructions in 8086 microprocessorBranching instructions in 8086 microprocessor
Branching instructions in 8086 microprocessorRabin BK
 
Report on c and c++
Report on c and c++Report on c and c++
Report on c and c++oggyrao
 
Fortran compiling 2
Fortran compiling 2Fortran compiling 2
Fortran compiling 2najenssr
 
Paradigmas de Linguagens de Programacao - Aula #5
Paradigmas de Linguagens de Programacao - Aula #5Paradigmas de Linguagens de Programacao - Aula #5
Paradigmas de Linguagens de Programacao - Aula #5Ismar Silveira
 
Assembly language (coal)
Assembly language (coal)Assembly language (coal)
Assembly language (coal)Hareem Aslam
 
Microcontroller directives
Microcontroller directivesMicrocontroller directives
Microcontroller directivesManoj Harsule
 
Microcontroladores: Tutorial de lenguaje ensamblador AVR: ramas y bucles cond...
Microcontroladores: Tutorial de lenguaje ensamblador AVR: ramas y bucles cond...Microcontroladores: Tutorial de lenguaje ensamblador AVR: ramas y bucles cond...
Microcontroladores: Tutorial de lenguaje ensamblador AVR: ramas y bucles cond...SANTIAGO PABLO ALBERTO
 
1344 Alp Of 8086
1344 Alp Of 80861344 Alp Of 8086
1344 Alp Of 8086techbed
 
C Language (All Concept)
C Language (All Concept)C Language (All Concept)
C Language (All Concept)sachindane
 

What's hot (20)

[ASM]Lab5
[ASM]Lab5[ASM]Lab5
[ASM]Lab5
 
N_Asm Assembly arithmetic instructions (sol)
N_Asm Assembly arithmetic instructions (sol)N_Asm Assembly arithmetic instructions (sol)
N_Asm Assembly arithmetic instructions (sol)
 
assembly language programming organization of IBM PC chapter 9 part-2(decimal...
assembly language programming organization of IBM PC chapter 9 part-2(decimal...assembly language programming organization of IBM PC chapter 9 part-2(decimal...
assembly language programming organization of IBM PC chapter 9 part-2(decimal...
 
[ASM]Lab7
[ASM]Lab7[ASM]Lab7
[ASM]Lab7
 
Assembly basics
Assembly basicsAssembly basics
Assembly basics
 
C programming-apurbo datta
C programming-apurbo dattaC programming-apurbo datta
C programming-apurbo datta
 
ETAPS03 SC.ppt
ETAPS03 SC.pptETAPS03 SC.ppt
ETAPS03 SC.ppt
 
The future of DSLs - functions and formal methods
The future of DSLs - functions and formal methodsThe future of DSLs - functions and formal methods
The future of DSLs - functions and formal methods
 
Unit 3 – assembly language programming
Unit 3 – assembly language programmingUnit 3 – assembly language programming
Unit 3 – assembly language programming
 
Unit3 cspc
Unit3 cspcUnit3 cspc
Unit3 cspc
 
Branching instructions in 8086 microprocessor
Branching instructions in 8086 microprocessorBranching instructions in 8086 microprocessor
Branching instructions in 8086 microprocessor
 
Report on c and c++
Report on c and c++Report on c and c++
Report on c and c++
 
Fortran compiling 2
Fortran compiling 2Fortran compiling 2
Fortran compiling 2
 
Paradigmas de Linguagens de Programacao - Aula #5
Paradigmas de Linguagens de Programacao - Aula #5Paradigmas de Linguagens de Programacao - Aula #5
Paradigmas de Linguagens de Programacao - Aula #5
 
Assembly language (coal)
Assembly language (coal)Assembly language (coal)
Assembly language (coal)
 
Microcontroller directives
Microcontroller directivesMicrocontroller directives
Microcontroller directives
 
Microcontroladores: Tutorial de lenguaje ensamblador AVR: ramas y bucles cond...
Microcontroladores: Tutorial de lenguaje ensamblador AVR: ramas y bucles cond...Microcontroladores: Tutorial de lenguaje ensamblador AVR: ramas y bucles cond...
Microcontroladores: Tutorial de lenguaje ensamblador AVR: ramas y bucles cond...
 
1344 Alp Of 8086
1344 Alp Of 80861344 Alp Of 8086
1344 Alp Of 8086
 
Fortran 95
Fortran 95Fortran 95
Fortran 95
 
C Language (All Concept)
C Language (All Concept)C Language (All Concept)
C Language (All Concept)
 

Similar to BIT MANIPULATION AND CONDITIONAL PROCESSING

Lecture 16 17 code-generation
Lecture 16 17 code-generationLecture 16 17 code-generation
Lecture 16 17 code-generationIffat Anjum
 
Loop instruction, controlling the flow of progam
Loop instruction, controlling the flow of progamLoop instruction, controlling the flow of progam
Loop instruction, controlling the flow of progamDr. Girish GS
 
Microcontroladores: introducción a la programación en lenguaje ensamblador AVR
Microcontroladores: introducción a la programación en lenguaje ensamblador AVRMicrocontroladores: introducción a la programación en lenguaje ensamblador AVR
Microcontroladores: introducción a la programación en lenguaje ensamblador AVRSANTIAGO PABLO ALBERTO
 
Instruction Set Of 8086 DIU CSE
Instruction Set Of 8086 DIU CSEInstruction Set Of 8086 DIU CSE
Instruction Set Of 8086 DIU CSEsalmancreation
 
Embedded system (Chapter 2) part 2
Embedded system (Chapter 2) part 2Embedded system (Chapter 2) part 2
Embedded system (Chapter 2) part 2Ikhwan_Fakrudin
 
Chap 3_2.ppt
Chap 3_2.pptChap 3_2.ppt
Chap 3_2.pptinian2
 
8051 data type and directives
8051 data type and directives8051 data type and directives
8051 data type and directivesSARITHA REDDY
 
8051 data types and directives
8051 data types and directives8051 data types and directives
8051 data types and directivesSARITHA REDDY
 
EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5PRADEEP
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAAiman Hud
 
8051 MMD Chapter 1.ppt
8051 MMD Chapter 1.ppt8051 MMD Chapter 1.ppt
8051 MMD Chapter 1.pptnotagain0712
 
Addressing Modes
Addressing ModesAddressing Modes
Addressing ModesMayank Garg
 

Similar to BIT MANIPULATION AND CONDITIONAL PROCESSING (20)

Chapt 06
Chapt 06Chapt 06
Chapt 06
 
Lecture 16 17 code-generation
Lecture 16 17 code-generationLecture 16 17 code-generation
Lecture 16 17 code-generation
 
Loop instruction, controlling the flow of progam
Loop instruction, controlling the flow of progamLoop instruction, controlling the flow of progam
Loop instruction, controlling the flow of progam
 
X86 operation types
X86 operation typesX86 operation types
X86 operation types
 
Microcontroladores: introducción a la programación en lenguaje ensamblador AVR
Microcontroladores: introducción a la programación en lenguaje ensamblador AVRMicrocontroladores: introducción a la programación en lenguaje ensamblador AVR
Microcontroladores: introducción a la programación en lenguaje ensamblador AVR
 
MES_MODULE 2.pptx
MES_MODULE 2.pptxMES_MODULE 2.pptx
MES_MODULE 2.pptx
 
Al2ed chapter7
Al2ed chapter7Al2ed chapter7
Al2ed chapter7
 
Arduino Functions
Arduino FunctionsArduino Functions
Arduino Functions
 
Instruction Set Of 8086 DIU CSE
Instruction Set Of 8086 DIU CSEInstruction Set Of 8086 DIU CSE
Instruction Set Of 8086 DIU CSE
 
8086 instruction set
8086 instruction set8086 instruction set
8086 instruction set
 
Embedded system (Chapter 2) part 2
Embedded system (Chapter 2) part 2Embedded system (Chapter 2) part 2
Embedded system (Chapter 2) part 2
 
Chap 3_2.ppt
Chap 3_2.pptChap 3_2.ppt
Chap 3_2.ppt
 
8051 data type and directives
8051 data type and directives8051 data type and directives
8051 data type and directives
 
8051 data types and directives
8051 data types and directives8051 data types and directives
8051 data types and directives
 
EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
PM1
PM1PM1
PM1
 
8051 MMD Chapter 1.ppt
8051 MMD Chapter 1.ppt8051 MMD Chapter 1.ppt
8051 MMD Chapter 1.ppt
 
8086-instruction sets.ppt
8086-instruction sets.ppt8086-instruction sets.ppt
8086-instruction sets.ppt
 
Addressing Modes
Addressing ModesAddressing Modes
Addressing Modes
 

Recently uploaded

Chiulli_Aurora_Oman_Raffaele_Beowulf.pptx
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptxChiulli_Aurora_Oman_Raffaele_Beowulf.pptx
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptxraffaeleoman
 
Night 7k Call Girls Noida Sector 128 Call Me: 8448380779
Night 7k Call Girls Noida Sector 128 Call Me: 8448380779Night 7k Call Girls Noida Sector 128 Call Me: 8448380779
Night 7k Call Girls Noida Sector 128 Call Me: 8448380779Delhi Call girls
 
Governance and Nation-Building in Nigeria: Some Reflections on Options for Po...
Governance and Nation-Building in Nigeria: Some Reflections on Options for Po...Governance and Nation-Building in Nigeria: Some Reflections on Options for Po...
Governance and Nation-Building in Nigeria: Some Reflections on Options for Po...Kayode Fayemi
 
Mathematics of Finance Presentation.pptx
Mathematics of Finance Presentation.pptxMathematics of Finance Presentation.pptx
Mathematics of Finance Presentation.pptxMoumonDas2
 
VVIP Call Girls Nalasopara : 9892124323, Call Girls in Nalasopara Services
VVIP Call Girls Nalasopara : 9892124323, Call Girls in Nalasopara ServicesVVIP Call Girls Nalasopara : 9892124323, Call Girls in Nalasopara Services
VVIP Call Girls Nalasopara : 9892124323, Call Girls in Nalasopara ServicesPooja Nehwal
 
If this Giant Must Walk: A Manifesto for a New Nigeria
If this Giant Must Walk: A Manifesto for a New NigeriaIf this Giant Must Walk: A Manifesto for a New Nigeria
If this Giant Must Walk: A Manifesto for a New NigeriaKayode Fayemi
 
The workplace ecosystem of the future 24.4.2024 Fabritius_share ii.pdf
The workplace ecosystem of the future 24.4.2024 Fabritius_share ii.pdfThe workplace ecosystem of the future 24.4.2024 Fabritius_share ii.pdf
The workplace ecosystem of the future 24.4.2024 Fabritius_share ii.pdfSenaatti-kiinteistöt
 
ANCHORING SCRIPT FOR A CULTURAL EVENT.docx
ANCHORING SCRIPT FOR A CULTURAL EVENT.docxANCHORING SCRIPT FOR A CULTURAL EVENT.docx
ANCHORING SCRIPT FOR A CULTURAL EVENT.docxNikitaBankoti2
 
George Lever - eCommerce Day Chile 2024
George Lever -  eCommerce Day Chile 2024George Lever -  eCommerce Day Chile 2024
George Lever - eCommerce Day Chile 2024eCommerce Institute
 
No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...
No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...
No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...Sheetaleventcompany
 
BDSM⚡Call Girls in Sector 93 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 93 Noida Escorts >༒8448380779 Escort ServiceBDSM⚡Call Girls in Sector 93 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 93 Noida Escorts >༒8448380779 Escort ServiceDelhi Call girls
 
Air breathing and respiratory adaptations in diver animals
Air breathing and respiratory adaptations in diver animalsAir breathing and respiratory adaptations in diver animals
Air breathing and respiratory adaptations in diver animalsaqsarehman5055
 
Introduction to Prompt Engineering (Focusing on ChatGPT)
Introduction to Prompt Engineering (Focusing on ChatGPT)Introduction to Prompt Engineering (Focusing on ChatGPT)
Introduction to Prompt Engineering (Focusing on ChatGPT)Chameera Dedduwage
 
Report Writing Webinar Training
Report Writing Webinar TrainingReport Writing Webinar Training
Report Writing Webinar TrainingKylaCullinane
 
Microsoft Copilot AI for Everyone - created by AI
Microsoft Copilot AI for Everyone - created by AIMicrosoft Copilot AI for Everyone - created by AI
Microsoft Copilot AI for Everyone - created by AITatiana Gurgel
 
Andrés Ramírez Gossler, Facundo Schinnea - eCommerce Day Chile 2024
Andrés Ramírez Gossler, Facundo Schinnea - eCommerce Day Chile 2024Andrés Ramírez Gossler, Facundo Schinnea - eCommerce Day Chile 2024
Andrés Ramírez Gossler, Facundo Schinnea - eCommerce Day Chile 2024eCommerce Institute
 
Re-membering the Bard: Revisiting The Compleat Wrks of Wllm Shkspr (Abridged)...
Re-membering the Bard: Revisiting The Compleat Wrks of Wllm Shkspr (Abridged)...Re-membering the Bard: Revisiting The Compleat Wrks of Wllm Shkspr (Abridged)...
Re-membering the Bard: Revisiting The Compleat Wrks of Wllm Shkspr (Abridged)...Hasting Chen
 
BDSM⚡Call Girls in Sector 97 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 97 Noida Escorts >༒8448380779 Escort ServiceBDSM⚡Call Girls in Sector 97 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 97 Noida Escorts >༒8448380779 Escort ServiceDelhi Call girls
 
Presentation on Engagement in Book Clubs
Presentation on Engagement in Book ClubsPresentation on Engagement in Book Clubs
Presentation on Engagement in Book Clubssamaasim06
 
SaaStr Workshop Wednesday w/ Lucas Price, Yardstick
SaaStr Workshop Wednesday w/ Lucas Price, YardstickSaaStr Workshop Wednesday w/ Lucas Price, Yardstick
SaaStr Workshop Wednesday w/ Lucas Price, Yardsticksaastr
 

Recently uploaded (20)

Chiulli_Aurora_Oman_Raffaele_Beowulf.pptx
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptxChiulli_Aurora_Oman_Raffaele_Beowulf.pptx
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptx
 
Night 7k Call Girls Noida Sector 128 Call Me: 8448380779
Night 7k Call Girls Noida Sector 128 Call Me: 8448380779Night 7k Call Girls Noida Sector 128 Call Me: 8448380779
Night 7k Call Girls Noida Sector 128 Call Me: 8448380779
 
Governance and Nation-Building in Nigeria: Some Reflections on Options for Po...
Governance and Nation-Building in Nigeria: Some Reflections on Options for Po...Governance and Nation-Building in Nigeria: Some Reflections on Options for Po...
Governance and Nation-Building in Nigeria: Some Reflections on Options for Po...
 
Mathematics of Finance Presentation.pptx
Mathematics of Finance Presentation.pptxMathematics of Finance Presentation.pptx
Mathematics of Finance Presentation.pptx
 
VVIP Call Girls Nalasopara : 9892124323, Call Girls in Nalasopara Services
VVIP Call Girls Nalasopara : 9892124323, Call Girls in Nalasopara ServicesVVIP Call Girls Nalasopara : 9892124323, Call Girls in Nalasopara Services
VVIP Call Girls Nalasopara : 9892124323, Call Girls in Nalasopara Services
 
If this Giant Must Walk: A Manifesto for a New Nigeria
If this Giant Must Walk: A Manifesto for a New NigeriaIf this Giant Must Walk: A Manifesto for a New Nigeria
If this Giant Must Walk: A Manifesto for a New Nigeria
 
The workplace ecosystem of the future 24.4.2024 Fabritius_share ii.pdf
The workplace ecosystem of the future 24.4.2024 Fabritius_share ii.pdfThe workplace ecosystem of the future 24.4.2024 Fabritius_share ii.pdf
The workplace ecosystem of the future 24.4.2024 Fabritius_share ii.pdf
 
ANCHORING SCRIPT FOR A CULTURAL EVENT.docx
ANCHORING SCRIPT FOR A CULTURAL EVENT.docxANCHORING SCRIPT FOR A CULTURAL EVENT.docx
ANCHORING SCRIPT FOR A CULTURAL EVENT.docx
 
George Lever - eCommerce Day Chile 2024
George Lever -  eCommerce Day Chile 2024George Lever -  eCommerce Day Chile 2024
George Lever - eCommerce Day Chile 2024
 
No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...
No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...
No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...
 
BDSM⚡Call Girls in Sector 93 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 93 Noida Escorts >༒8448380779 Escort ServiceBDSM⚡Call Girls in Sector 93 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 93 Noida Escorts >༒8448380779 Escort Service
 
Air breathing and respiratory adaptations in diver animals
Air breathing and respiratory adaptations in diver animalsAir breathing and respiratory adaptations in diver animals
Air breathing and respiratory adaptations in diver animals
 
Introduction to Prompt Engineering (Focusing on ChatGPT)
Introduction to Prompt Engineering (Focusing on ChatGPT)Introduction to Prompt Engineering (Focusing on ChatGPT)
Introduction to Prompt Engineering (Focusing on ChatGPT)
 
Report Writing Webinar Training
Report Writing Webinar TrainingReport Writing Webinar Training
Report Writing Webinar Training
 
Microsoft Copilot AI for Everyone - created by AI
Microsoft Copilot AI for Everyone - created by AIMicrosoft Copilot AI for Everyone - created by AI
Microsoft Copilot AI for Everyone - created by AI
 
Andrés Ramírez Gossler, Facundo Schinnea - eCommerce Day Chile 2024
Andrés Ramírez Gossler, Facundo Schinnea - eCommerce Day Chile 2024Andrés Ramírez Gossler, Facundo Schinnea - eCommerce Day Chile 2024
Andrés Ramírez Gossler, Facundo Schinnea - eCommerce Day Chile 2024
 
Re-membering the Bard: Revisiting The Compleat Wrks of Wllm Shkspr (Abridged)...
Re-membering the Bard: Revisiting The Compleat Wrks of Wllm Shkspr (Abridged)...Re-membering the Bard: Revisiting The Compleat Wrks of Wllm Shkspr (Abridged)...
Re-membering the Bard: Revisiting The Compleat Wrks of Wllm Shkspr (Abridged)...
 
BDSM⚡Call Girls in Sector 97 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 97 Noida Escorts >༒8448380779 Escort ServiceBDSM⚡Call Girls in Sector 97 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 97 Noida Escorts >༒8448380779 Escort Service
 
Presentation on Engagement in Book Clubs
Presentation on Engagement in Book ClubsPresentation on Engagement in Book Clubs
Presentation on Engagement in Book Clubs
 
SaaStr Workshop Wednesday w/ Lucas Price, Yardstick
SaaStr Workshop Wednesday w/ Lucas Price, YardstickSaaStr Workshop Wednesday w/ Lucas Price, Yardstick
SaaStr Workshop Wednesday w/ Lucas Price, Yardstick
 

BIT MANIPULATION AND CONDITIONAL PROCESSING

  • 1. CONDITIONAL PROCESSING Lecture # 14 UniversityofCenteralPunjabFsdCampus 1
  • 2. CHAPTER OVERVIEW Programs that deal with hardware devices must be able to manipulate individual bits in numbers. Data encryption and compression also rely on bit manipulation.  How can I use the Boolean operations introduced in Chapter 1 (AND, OR, NOT)?  How do I write an IF statement in assembly language?  How are nested-IF statements translated by compilers into machine language?  How can I set and clear individual bits in a binary number?  How can I perform simple binary data encryption?  How are signed numbers differentiated from unsigned numbers in Boolean expressions? UniversityofCenteralPunjabFsdCampus 2
  • 3. CHAPTER OVERVIEW  Boolean and Comparison Instructions  Conditional Jumps  Conditional Loop Instructions  Conditional Structures  Conditional Control Flow Directives UniversityofCenteralPunjabFsdCampus 3
  • 4. BOOLEAN AND COMPARISON INSTRUCTIONS  CPU Status Flags  AND Instruction  OR Instruction  XOR Instruction  NOT Instruction  Applications  TEST Instruction  CMP Instruction UniversityofCenteralPunjabFsdCampus 4
  • 5. STATUS FLAGS - REVIEW  The Zero flag is set when the result of an operation equals zero  The Carry flag is set when an instruction generates a result that is too large for the destination operand.  The Sign flag is set if the destination operand is negative, and it is clear if the destination operand is positive  The Parity flag is set when an instruction generates an even number of 1 bits in the low byte of the destination operand. UniversityofCenteralPunjabFsdCampus 5
  • 7. 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  Syntax  AND reg, reg  AND reg, mem  AND reg, imm  AND mem, reg  AND mem, imm  The operands can be 8, 16, 32, or 64 bits, and they must be the same size. UniversityofCenteralPunjabFsdCampus 7
  • 8. EXAMPLE  AL is initially set to 10101110 binary  ANDing it with 11110110  AL equals ??  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 UniversityofCenteralPunjabFsdCampus 8
  • 9. 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:  The rest of the alphabetic characters have the same relationship. If we AND any character with 11011111 binary, all bits are unchanged except for bit 5 UniversityofCenteralPunjabFsdCampus 9
  • 10. 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  Syntax  OR reg,reg  OR reg,mem  OR reg,imm  OR mem,reg  OR mem,imm UniversityofCenteralPunjabFsdCampus 10
  • 11. EXAMPLE  AL is initially equal to 11100011  OR it with 00000100  Al is equals to ??  Flags 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. 11 UniversityofCenteralPunjabFsdCampus
  • 12. XOR INSTRUCTION  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  XOR destination, source  The XOR instruction uses the same operand combinations and sizes as the AND and OR instructions. 12 UniversityofCenteralPunjabFsdCampus
  • 13. NOT INSTRUCTION  The NOT instruction toggles (inverts) all bits in an operand. The result is called the one’s complement.  The following operand types are permitted:  NOT reg  NOT mem  Flags No flags are affected by the NOT instruction. 13 UniversityofCenteralPunjabFsdCampus
  • 14. CMP INSTRUCTION  In x86 assembly language we use the CMP instruction to compare integers.  Compares the destination operand to the source operand  Syntax: CMP destination, source  Example: destination == source  Flags: The CMP instruction changes the Overflow, Sign, Zero, Carry, Auxiliary Carry, and Parity flags 14 UniversityofCenteralPunjabFsdCampus
  • 15. CMP INSTRUCTION  CMP is a valuable tool for creating conditional logic structures. When you follow CMP with a conditional jump instruction, the result is the assembly language equivalent of an IF statement. 15 UniversityofCenteralPunjabFsdCampus
  • 16. CMP INSTRUCTION- EXAMPLES  When AX equals 5 and is compared to 10, the Carry flag is set because subtracting 10 from 5 requires a borrow:  mov ax,5  cmp ax,10 ; ZF = 0 and CF = 1 16 UniversityofCenteralPunjabFsdCampus
  • 17. SETTING AND CLEARING INDIVIDUAL CPU FLAGS  How can you easily set or clear the Zero, Sign, Carry, and Overflow flags?  To set the Zero flag, TEST or AND an operand with Zero; to clear the Zero flag, OR an operand with 1: 17 UniversityofCenteralPunjabFsdCampus
  • 18. SETTING AND CLEARING INDIVIDUAL CPU FLAGS  To set the Carry flag, use the STC instruction; to clear the Carry flag, use CLC:  stc ; set Carry flag  clc ; clear Carry flag 18 UniversityofCenteralPunjabFsdCampus
  • 19. WHAT'S NEXT  Boolean and Comparison Instructions  Conditional Jumps  Conditional Loop Instructions  Conditional Structures  Conditional Control Flow Directives 19 UniversityofCenteralPunjabFsdCampus
  • 20. 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. 20 UniversityofCenteralPunjabFsdCampus
  • 21. EXAMPLE  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: 21 UniversityofCenteralPunjabFsdCampus
  • 22. JCOND INSTRUCTION  A conditional jump instruction branches to a label when specific register or flag conditions are met 22 UniversityofCenteralPunjabFsdCampus
  • 23. JUMPS BASED ON SPECIFIC FLAGS 23 UniversityofCenteralPunjabFsdCampus
  • 24. JUMPS BASED ON EQUALITY 24 UniversityofCenteralPunjabFsdCampus
  • 26. EXAMPLE  Loop until Key Pressed In the following 32-bit code, a loop runs continuously until the user presses a standard alphanumeric key. The ReadKey method from the Irvine32 library sets the Zero flag if no key is present in the input buffer:  .data  char BYTE ?  .code  L1: mov eax,10 ; create 10 ms delay  call Delay  call ReadKey ; check for key  jz L1 ; repeat if no key  mov char,AL ; save the character 26 UniversityofCenteralPunjabFsdCampus
  • 27. OTHER IMPORTANT EXAMPLES  Application: Sequential Search of an Array  Application: Simple String Encryption 27 UniversityofCenteralPunjabFsdCampus
  • 28. WHAT'S NEXT  Boolean and Comparison Instructions  Conditional Jumps  Conditional Loop Instructions  Conditional Structures  Conditional Control Flow Directives 28 UniversityofCenteralPunjabFsdCampus
  • 29. CONDITIONAL LOOP INSTRUCTIONS  LOOPZ and LOOPE  LOOPNZ and LOOPNE 29 UniversityofCenteralPunjabFsdCampus
  • 30. LOOPZ INSTRUCTIONS  LOOPZ (loop if zero) instruction works just like the LOOP instruction except that the Zero flag must be set in order for control to transfer to the destination label  The syntax is  LOOPZ destination 30 UniversityofCenteralPunjabFsdCampus
  • 31. LOOPE INSTRUCTIONS  The LOOPE (loop if equal) instruction is equivalent to LOOPZ, and they share the same opcode. They perform the following tasks:  ECX = ECX - 1  if ECX > 0 and ZF = 1, jump to destination 31 UniversityofCenteralPunjabFsdCampus
  • 32. LOOPNE INSTRUCTIONS  The LOOPNE (loop if not equal) instruction is equivalent to LOOPNZ, and they share the same opcode. They perform the following tasks:  ECX = ECX - 1  if ECX > 0 and ZF = 0, jump to destination 32 UniversityofCenteralPunjabFsdCampus