Notes
Slide Show
Outline
1
Assembly Language for x86 Processors 6th Edition
  • Chapter 6: Conditional Processing
2
Chapter Overview
  • Boolean and Comparison Instructions
  • Conditional Jumps
  • Conditional Loop Instructions
  • Conditional Structures
  • Application: Finite-State Machines
  • Conditional Control Flow Directives
3
Boolean and Comparison Instructions
  • CPU Status Flags
  • AND Instruction
  • OR Instruction
  • XOR Instruction
  • NOT Instruction
  • Applications
  • TEST Instruction
  • CMP Instruction
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 (or too small) 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 Overflow flag is set when an instruction generates an invalid signed result (bit 7 carry is XORed with bit 6 Carry).
  • The Parity flag is set when an instruction generates an even number of 1 bits in the low byte of the destination operand.
  • The Auxiliary Carry flag is set when an operation produces a carry out from bit 3 to bit 4
5
AND Instruction
  • Performs a Boolean AND operation between each pair of matching bits in two operands
  • Syntax:
      • AND destination, source
    • (same operand types as MOV)
6
OR Instruction
  • Performs a Boolean OR operation between each pair of matching bits in two operands
  • Syntax:
      • OR destination, source
7
XOR Instruction
  • Performs a Boolean exclusive-OR operation between each pair of matching bits in two operands
  • Syntax:
      • XOR destination, source
8
NOT Instruction
  • Performs a Boolean NOT operation on a single destination operand
  • Syntax:
      • NOT destination
9
Bit-Mapped Sets
  • Binary bits indicate set membership
  • Efficient use of storage
  • Also known as bit vectors
10
Bit-Mapped Set Operations
  • Set Complement
      • mov eax,SetX
      • not eax


  • Set Intersection
      • mov eax,setX
      • and eax,setY


  • Set Union
      • mov eax,setX
      • or  eax,setY

11
Applications  (1 of 5)
12
Applications  (2 of 5)
13
Applications  (3 of 5)
14
Applications  (4 of 5)
15
Applications  (5 of 5)
16
TEST Instruction
  • Performs a nondestructive AND operation between each pair of matching bits in two operands
  • No operands are modified, but the Zero flag is affected.
  • Example: jump to a label if either bit 0 or bit 1 in AL is set.
17
CMP Instruction  (1 of 3)
  • Compares the destination operand to the source operand
    • Nondestructive subtraction of source from destination (destination operand is not changed)
  • Syntax: CMP destination, source
  • Example: destination == source
18
CMP Instruction  (2 of 3)
  • Example: destination > source
19
CMP Instruction  (3 of 3)
  • Example: destination > source
20
What's Next
  • Boolean and Comparison Instructions
  • Conditional Jumps
  • Conditional Loop Instructions
  • Conditional Structures
  • Application: Finite-State Machines
  • Conditional Control Flow Directives
21
Conditional Jumps
  • Jumps Based On . . .
    • Specific flags
    • Equality
    • Unsigned comparisons
    • Signed Comparisons
  • Applications
  • Encrypting a String
  • Bit Test (BT) Instruction
22
Jcond Instruction
  • A conditional jump instruction branches to a label when specific register or flag conditions are met


  • Specific jumps:
    • JB, JC - jump to a label if the Carry flag is set
    • JE, JZ - jump to a label if the Zero flag is set
    • JS - jump to a label if the Sign flag is set
    • JNE, JNZ - jump to a label if the Zero flag is clear
    • JECXZ - jump to a label if ECX = 0
23
Jcond Ranges
  • Prior to the 386:
    • jump must be within –128 to +127 bytes from current location counter
  • x86 processors:
    • 32-bit offset permits jump anywhere in memory
24
Jumps Based on Specific Flags
25
Jumps Based on Equality
26
Jumps Based on Unsigned Comparisons
27
Jumps Based on Signed Comparisons
28
Applications  (1 of 5)
29
Applications  (2 of 5)
30
Applications  (3 of 5)
31
Applications  (4 of 5)
32
Applications  (5 of 5)
33
Your turn . . .
  • Write code that jumps to label L1 if either bit 4, 5, or 6 is set in the BL register.
  • Write code that jumps to label L1 if bits 4, 5, and 6 are all set in the BL register.
  • Write code that jumps to label L2 if AL has even parity.
  • Write code that jumps to label L3 if EAX is negative.
  • Write code that jumps to label L4 if the expression (EBX – ECX) is greater than zero.
34
Encrypting a String
35
String Encryption Program
  • Tasks:
    • Input a message (string) from the user
    • Encrypt the message
    • Display the encrypted message
    • Decrypt the message
    • Display the decrypted message
36
BT (Bit Test) Instruction
  • Copies bit n from an operand into the Carry flag
  • Syntax: BT bitBase, n
    • bitBase may be r/m16 or r/m32
    • n may be r16, r32, or imm8
  • Example: jump to label L1 if bit 9 is set in the AX register:
37
What's Next
  • Boolean and Comparison Instructions
  • Conditional Jumps
  • Conditional Loop Instructions
  • Conditional Structures
  • Application: Finite-State Machines
  • Conditional Control Flow Directives
38
Conditional Loop Instructions
  • LOOPZ and LOOPE
  • LOOPNZ and LOOPNE
39
LOOPZ and LOOPE
  • Syntax:
    • LOOPE destination
    • LOOPZ destination
  • Logic:
    • ECX ¬ ECX – 1
    • if ECX > 0 and ZF=1, jump to destination
  • Useful when scanning an array for the first element that does not match a given value.
40
LOOPNZ and LOOPNE
  • LOOPNZ (LOOPNE) is a conditional loop instruction
  • Syntax:
  • LOOPNZ destination
  • LOOPNE destination
  • Logic:
    • ECX ¬ ECX – 1;
    • if ECX > 0 and ZF=0, jump to destination
  • Useful when scanning an array for the first element that matches a given value.
41
LOOPNZ Example
42
Your turn . . .
43
. . . (solution)
44
What's Next
  • Boolean and Comparison Instructions
  • Conditional Jumps
  • Conditional Loop Instructions
  • Conditional Structures
  • Application: Finite-State Machines
  • Conditional Control Flow Directives
45
Conditional Structures
  • Block-Structured IF Statements
  • Compound Expressions with AND
  • Compound Expressions with OR
  • WHILE Loops
  • Table-Driven Selection
46
Block-Structured IF Statements
  • Assembly language programmers can easily translate logical statements written in C++/Java into assembly language. For example:
47
Your turn . . .
  • Implement the following pseudocode in assembly language. All values are unsigned:
48
Your turn . . .
  • Implement the following pseudocode in assembly language. All values are 32-bit signed integers:
49
Compound Expression with AND  (1 of 3)
  • When implementing the logical AND operator, consider that HLLs use short-circuit evaluation
  • In the following example, if the first expression is false, the second expression is skipped:
50
Compound Expression with AND  (2 of 3)
51
Compound Expression with AND  (3 of 3)
52
Your turn . . .
  • Implement the following pseudocode in assembly language. All values are unsigned:
53
Compound Expression with OR  (1 of 2)
  • When implementing the logical OR operator, consider that HLLs use short-circuit evaluation
  • In the following example, if the first expression is true, the second expression is skipped:
54
Compound Expression with OR  (2 of 2)
55
WHILE Loops
56
Your turn . . .
57
Table-Driven Selection  (1 of 4)
  • Table-driven selection uses a table lookup to replace a multiway selection structure
  • Create a table containing lookup values and the offsets of labels or procedures
  • Use a loop to search the table
  • Suited to a large number of comparisons
58
Table-Driven Selection  (2 of 4)
59
Table-Driven Selection  (3 of 4)
  • Table of Procedure Offsets:
60
Table-Driven Selection  (4 of 4)
61
What's Next
  • Boolean and Comparison Instructions
  • Conditional Jumps
  • Conditional Loop Instructions
  • Conditional Structures
  • Application: Finite-State Machines
  • Conditional Control Flow Directives
62
Application: Finite-State Machines
  • A finite-state machine (FSM) is a graph structure that changes state based on some input. Also called a state-transition diagram.
  • We use a graph to represent an FSM, with squares or circles called nodes, and lines with arrows between the circles called edges.
63
Application: Finite-State Machines
  • A FSM is a specific instance of a more general structure called a directed graph.
  • Three basic states, represented by nodes:
    • Start state
    • Terminal state(s)
    • Nonterminal state(s)
64
Finite-State Machine
  • Accepts any sequence of symbols that puts it into an accepting (final) state
  • Can be used to recognize, or validate a sequence of characters that is governed by language rules (called a regular expression)
  • Advantages:
    • Provides visual tracking of program's flow of control
    • Easy to modify
    • Easily implemented in assembly language
65
Finite-State Machine Examples
  • FSM that recognizes strings beginning with 'x', followed by letters 'a'..'y', ending with 'z':
66
Your Turn . . .
  • Explain why the following FSM does not work as well for signed integers as the one shown on the previous slide:
67
Implementing an FSM
68
IsDigit Procedure
69
Flowchart of State A
70
Your Turn . . .
  • Draw a FSM diagram for hexadecimal integer constant that conforms to MASM syntax.
  • Draw a flowchart for one of the states in your FSM.
  • Implement your FSM in assembly language. Let the user input a hexadecimal constant from the keyboard.
71
What's Next
  • Boolean and Comparison Instructions
  • Conditional Jumps
  • Conditional Loop Instructions
  • Conditional Structures
  • Application: Finite-State Machines
  • Conditional Control Flow Directives
72
Creating IF Statements
  • Runtime Expressions
  • Relational and Logical Operators
  • MASM-Generated Code
  • .REPEAT Directive
  • .WHILE Directive
73
Runtime Expressions
74
Relational and Logical Operators
75
Signed and Unsigned Comparisons
76
Signed and Unsigned Comparisons
77
Signed and Unsigned Comparisons
78
Signed and Unsigned Comparisons
79
.REPEAT Directive
80
.WHILE Directive
81
Summary
  • Bitwise instructions (AND, OR, XOR, NOT, TEST)
    • manipulate individual bits in operands
  • CMP – compares operands using implied subtraction
    • sets condition flags
  • Conditional Jumps & Loops
    • equality: JE, JNE
    • flag values: JC, JZ, JNC, JP, ...
    • signed: JG, JL, JNG, ...
    • unsigned: JA, JB, JNA, ...
    • LOOPZ, LOOPNZ, LOOPE, LOOPNE
  • Flowcharts – logic diagramming tool
  • Finite-state machine – tracks state changes at runtime
82
4C 6F 70 70 75 75 6E