|
1
|
- Chapter 6: Conditional Processing
|
|
2
|
- Boolean and Comparison Instructions
- Conditional Jumps
- Conditional Loop Instructions
- Conditional Structures
- Application: Finite-State Machines
- Conditional Control Flow Directives
|
|
3
|
- CPU Status Flags
- AND Instruction
- OR Instruction
- XOR Instruction
- NOT Instruction
- Applications
- TEST Instruction
- CMP Instruction
|
|
4
|
- 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
|
- Performs a Boolean AND operation between each pair of matching bits in
two operands
- Syntax:
- (same operand types as MOV)
|
|
6
|
- Performs a Boolean OR operation between each pair of matching bits in
two operands
- Syntax:
|
|
7
|
- Performs a Boolean exclusive-OR operation between each pair of matching
bits in two operands
- Syntax:
|
|
8
|
- Performs a Boolean NOT operation on a single destination operand
- Syntax:
|
|
9
|
- Binary bits indicate set membership
- Efficient use of storage
- Also known as bit vectors
|
|
10
|
- Set Complement
- Set Intersection
- mov eax,setX
- and eax,setY
- Set Union
|
|
11
|
|
|
12
|
|
|
13
|
|
|
14
|
|
|
15
|
|
|
16
|
- 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
|
- 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
|
- Example: destination > source
|
|
19
|
- Example: destination > source
|
|
20
|
- Boolean and Comparison Instructions
- Conditional Jumps
- Conditional Loop Instructions
- Conditional Structures
- Application: Finite-State Machines
- Conditional Control Flow Directives
|
|
21
|
- Jumps Based On . . .
- Specific flags
- Equality
- Unsigned comparisons
- Signed Comparisons
- Applications
- Encrypting a String
- Bit Test (BT) Instruction
|
|
22
|
- 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
|
- 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
|
|
|
25
|
|
|
26
|
|
|
27
|
|
|
28
|
|
|
29
|
|
|
30
|
|
|
31
|
|
|
32
|
|
|
33
|
- 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
|
|
|
35
|
- Tasks:
- Input a message (string) from the user
- Encrypt the message
- Display the encrypted message
- Decrypt the message
- Display the decrypted message
|
|
36
|
- 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
|
- Boolean and Comparison Instructions
- Conditional Jumps
- Conditional Loop Instructions
- Conditional Structures
- Application: Finite-State Machines
- Conditional Control Flow Directives
|
|
38
|
- LOOPZ and LOOPE
- LOOPNZ and LOOPNE
|
|
39
|
- 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 (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
|
|
|
42
|
|
|
43
|
|
|
44
|
- Boolean and Comparison Instructions
- Conditional Jumps
- Conditional Loop Instructions
- Conditional Structures
- Application: Finite-State Machines
- Conditional Control Flow Directives
|
|
45
|
- Block-Structured IF Statements
- Compound Expressions with AND
- Compound Expressions with OR
- WHILE Loops
- Table-Driven Selection
|
|
46
|
- Assembly language programmers can easily translate logical statements
written in C++/Java into assembly language. For example:
|
|
47
|
- Implement the following pseudocode in assembly language. All values are
unsigned:
|
|
48
|
- Implement the following pseudocode in assembly language. All values are
32-bit signed integers:
|
|
49
|
- 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
|
|
|
51
|
|
|
52
|
- Implement the following pseudocode in assembly language. All values are
unsigned:
|
|
53
|
- 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
|
|
|
55
|
|
|
56
|
|
|
57
|
- 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
|
|
|
59
|
- Table of Procedure Offsets:
|
|
60
|
|
|
61
|
- Boolean and Comparison Instructions
- Conditional Jumps
- Conditional Loop Instructions
- Conditional Structures
- Application: Finite-State Machines
- Conditional Control Flow Directives
|
|
62
|
- 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
|
- 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
|
- 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
|
- FSM that recognizes strings beginning with 'x', followed by letters
'a'..'y', ending with 'z':
|
|
66
|
- Explain why the following FSM does not work as well for signed integers
as the one shown on the previous slide:
|
|
67
|
|
|
68
|
|
|
69
|
|
|
70
|
- 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
|
- Boolean and Comparison Instructions
- Conditional Jumps
- Conditional Loop Instructions
- Conditional Structures
- Application: Finite-State Machines
- Conditional Control Flow Directives
|
|
72
|
- Runtime Expressions
- Relational and Logical Operators
- MASM-Generated Code
- .REPEAT Directive
- .WHILE Directive
|
|
73
|
|
|
74
|
|
|
75
|
|
|
76
|
|
|
77
|
|
|
78
|
|
|
79
|
|
|
80
|
|
|
81
|
- Bitwise instructions (AND, OR, XOR, NOT, TEST)
- manipulate individual bits in operands
- CMP – compares operands using implied subtraction
- 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
|
|