Conditional Processing: CMPS293&290 Class Notes (Chap 06) Kuo-Pao Yang Page 1 / 23
Conditional Processing: CMPS293&290 Class Notes (Chap 06) Kuo-Pao Yang Page 1 / 23
Conditional Processing
• A programming language that permits decision making lets you alter the flow of control,
using a technique know as conditional branching..
• 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 when viewed as an unsigned integer.
• 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’s 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
• Performs a Boolean AND operation between each pair of matching bits in two operands
• Syntax:
AND destination, source
• AND instruction is often used to clear selected bits and preserve others.
• Application
o Task: Convert the character in AL to upper case
o Solution: Use the AND instruction to clear bit 5
mov al,'a' ; AL = 01100001b (61h = ‘a’)
and al,11011111b ; AL = 01000001b (41h = ‘A’) clear bit 5
• Performs a Boolean OR operation between each pair of matching bits in two operands
• Syntax:
OR destination, source
• OR instruction is often used to set selected bits and preserve others.
• Application
o Task: Convert a binary decimal byte into its equivalent ASCII decimal digit.
o Solution: Use the OR instruction to set bits 4 and 5.
mov al,6 ; AL = 00000110b (06h)
or al,00110000b ; AL = 00110110b (36h = ‘6’) set bits 4 and 5
• Performs a Boolean exclusive-OR operation between each pair of matching bits in two
operands
• Syntax:
XOR destination, source
• XOR is a useful way to toggle (inverted) the bits in an operand.
• Performs a nondestructive AND operation between each pair of matching bits in two
operands
• Syntax:
TEST destination, source
• No operands are modified, but the Zero flag is affected.
• Example:
o Value 00001001 in this example is called a bit mask. Zero flag is et only when all tested
bits are clear
mov al, 00100101b ; AL = 00100101b
test al, 00001001b ; AL = 00100101b ZF = 0 test bits 0 and 3
• Zero Flag
o ZF = 1: Test or AND an operand with Zero
o ZF = 0: OR an operand with 1
test al, 0 ; ZF = 1
and al, 0 ; ZF = 1
or al, 1 ; ZF = 0
• Sign Flag
o SF = 1: OR the highest bit of an operand with 1
o SF = 0: AND the highest bit with 0
or al, 80h ; SF = 1
and al, 7Fh ; SF = 0
• Carry flag
o CF = 1: STC instruction
o CF = 0: CLC instruction
stc ; CF = 1
clc ; CF = 0
• Overflow Flag
o OF = 1: Add two positive byte values that produce a negative sum
o OF = 0: OR an operand with 0
mov al , 7Fh ; AL = +127
inc al ; OF = 1, AL = 80 (-128)
or al, 0 ; OF = 0
• A conditional jump instruction branches to a label when specific register or flag conditions
are met
o JC: jump if CF = 1; jump to a label if the Carry flag is set
o JNC: jump if CF = 0; jump to a label if the Carry flag is clear
o JZ: jump if ZF = 1; jump to a label if the Zero flag is set
o JNZ: jump if ZF = 0; jump to a label if the Zero flag is clear
• Jcond Ranges
o Prior to the 386:
Jump must be within –128 to +127 bytes from current location counter
o IA-32 processors:
32-bit offset permits jump anywhere in memory
INCLUDE Irvine32.inc
KEY = 239 ; any value between 1-255
BUFMAX = 128 ; maximum buffer size
.data
sPrompt BYTE "Enter the plain text: ",0
sEncrypt BYTE "Cipher text: ",0
sDecrypt BYTE "Decrypted: ",0
buffer BYTE BUFMAX+1 DUP(0)
bufSize DWORD ?
.code
main PROC
call InputTheString ; input the plain text
call TranslateBuffer ; encrypt the buffer
mov edx,OFFSET sEncrypt ; display encrypted message
call DisplayMessage
call TranslateBuffer ; decrypt the buffer
mov edx,OFFSET sDecrypt ; display decrypted message
call DisplayMessage
exit
main ENDP
;-----------------------------------------------------
InputTheString PROC
;
; Prompts user for a plaintext string. Saves the string
; and its length.
; Receives: nothing
; Returns: nothing
;-----------------------------------------------------
pushad
mov edx,OFFSET sPrompt ; display a prompt
call WriteString
mov ecx,BUFMAX ; maximum character count
;-----------------------------------------------------
DisplayMessage PROC
;
; Displays the encrypted or decrypted message.
; Receives: EDX points to the message
; Returns: nothing
;-----------------------------------------------------
pushad
call WriteString
mov edx,OFFSET buffer ; display the buffer
call WriteString
call Crlf
call Crlf
popad
ret
DisplayMessage ENDP
;-----------------------------------------------------
TranslateBuffer PROC
;
; Translates the string by exclusive-ORing each
; byte with the encryption key byte.
; Receives: nothing
; Returns: nothing
;-----------------------------------------------------
pushad
mov ecx,bufSize ; loop counter
mov esi,0 ; index 0 in buffer
L1:
xor buffer[esi],KEY ; translate a byte
inc esi ; point to next byte
loop L1
popad
ret
TranslateBuffer ENDP
END main
• LOOPZ (loop if zero) permits a loop to continue while Zero flag is set and the unsigned
value of ECX is greater than zero.
• LOOPE (loop if equal) instruction equivalent to LOOPZ.
• Syntax:
LOOPE destination
LOOPZ destination
• Logic:
ECX = ECX – 1
if ECX > 0 and ZF=1, jump to destination
• LOOPNZ (loop if not zero) permits a loop to continue while the unsigned value of ECX is
greater than zero and Zero flag is clear.
• LOOPNE (loop if not equal) instruction equivalent to LOOPNZ.
• Syntax:
LOOPNE destination
LOOPNZ 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
• Example: finds the first positive value in an array
.data
array SWORD -3,-6,-1,-10,10,30,40,4
sentinel SWORD 0
.code
mov esi,OFFSET array
mov ecx,LENGTHOF array
L1:
test WORD PTR [esi],8000h ; test sign bit
pushfd ; push flags on stack
add esi,TYPE array
popfd ; pop flags from stack
loopnz L1 ; continue loop
jnz quit ; none found
sub esi,TYPE array ; ESI points to value
quit:
• Assembly language programmers can easily translate logical statements written in C++/Java
into assembly language
• Example:
• A WHILE loop is really an IF statement followed by the body of the loop, followed by an
unconditional jump to the top of the loop
• Consider the following example:
while(eax < ebx)
eax = eax + 1;
• This is a possible implementation:
top:
cmp eax,ebx ; check loop condition
jae next ; false? exit loop
inc eax ; body of loop
jmp top ; repeat the loop
next:
• A finite-state machine (FSM) is a graph structure that changes state based on some input,
also called a state-transition diagram
• Use a graph to represent an FSM, with squares or circles called nodes, and lines with arrows
between the circles called edges (or arcs)
• A FSM is a specific instance of a more general structure called a directed graph (or
digraph).
• Three basic states, represented by nodes:
o Start state
o Terminal state(s)
o Nonterminal state(s)
• 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:
o Provides visual tracking of program's flow of control
o Easy to modify
o Easily implemented in assembly language
• FSM that recognizes strings beginning with 'x', followed by letters 'a'..'y', ending with 'z':
o The following input strings would be recognized by this FSM:
xaabcdefgz
xz
xyyqqrrstuvz
o MASM generates "hidden" code for you, consisting of code labels, CMP and conditional
jump instructions
.data
val1 SDWORD 5
result SDWORD ?
.code
mov eax,6 mov eax,6
.IF eax > val1 cmp eax,val1
mov result,1 jle @C0001 ; jump signed comp
.ENDIF mov result,1
@C0001: