0% found this document useful (0 votes)
25 views34 pages

11-Condition FINAL

Uploaded by

Mah Rukh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views34 pages

11-Condition FINAL

Uploaded by

Mah Rukh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 34

Conditional Processing

Reference:
Section 6.2 and 6.3 of
chapter 6 of kip irvine

with slides by Kip Irvine


Boolean and comparison instructions

• CPU Status Flags


• AND Instruction
• OR Instruction
• XOR Instruction
• NOT Instruction
• Applications
• TEST Instruction
• CMP Instruction
AND , OR, XOR
The following operand combinations are
permitted:
•AND reg,reg
•AND reg,mem
•AND reg,imm
•AND mem,reg
•AND mem,imm

•The operands can be 8, 16, or 32 bits, and they


must be the same size
AND instruction
• Performs a bitwise Boolean AND operation between each pair of matching
bits in two operands
• The AND operation can be used for clearing one or more bits
• Syntax: (O=0,C=0,SZP)
AND destination, source
• Example:
AND
mov al, 00111011b
and al, 00001111b ; OFh

Best Explaination: https://www.tutorialspoint.com/assembly_programming/assembly_logical_instructions.htm


OR instruction
• Performs a bitwise Boolean OR operation between each pair of
matching bits in two operands
• The OR operation can be used for setting one or more bits.
• Syntax: (O=0,C=0,SZP)
OR destination, source
• Example: OR
mov dl, 00111011b
or dl, 00001111b
NOT instruction
• Performs a bitwise Boolean NOT operation on a
single destination operand
• Syntax: (no flag affected)
NOT destination
NOT
• Example:
mov al, 11110000b
not al

NOT 00111011
11000100 inverted
XOR instruction
• Performs a bitwise Boolean exclusive-OR operation between each
pair of matching bits in two operands
• XORing an operand with itself changes the operand to 0. This is
used to clear a register.
• Syntax: (O=0,C=0,SZP)
XOR destination, source
• Example: XOR
mov dl, 00111011b
xor dl, 00001111b

00111011
XOR 0 0 0 0 1 1 1 1

unchanged 00110100 inverted

XOR is a useful way to invert the bits in an operand and data encryption
Applications (1 of 5)

• Task: Convert the character in AL to upper case.


Hint: use AND instruction
• Solution: Use the AND instruction to clear bit 5.

mov al,'a' ; AL = 01100001b


and al,11011111b ; AL = 01000001b

ASCII code for a is 97 (01100001b)


ASCII code for A is 65 (01000001b)
Applications (2 of 5)

• Task: Convert a binary decimal byte into its equivalent


ASCII decimal digit.
• Solution: Use the OR instruction to set bits 4 and 5.

mov al,6 ; AL = 00000110b


or al,00110000b ; AL = 00110110b

The ASCII digit '6' = 00110110b

ASCII code for 6 is 54


Applications (4 of 5)

• Task: Jump to a label if an integer is even.


• Solution: AND the lowest bit with a 1. If the result
is Zero, the number is even. BECAUSE lowest bit
of an even no is always 0. e.g. 8 in binary is 1000

mov ax,wordVal
and ax,1 ; low bit set?
jz EvenValue ; jump if Zero flag set
CMP instruction
• Compares the destination operand to the source
operand
– Same like SUB but it is Nondestructive subtraction of source from
destination (destination operand is not changed)
• We use the CMP instruction to compare integers and
Characters (As the CPU use ASCII code)

• Syntax: (OSZAPC)
CMP destination, source
CMP instruction
• When two unsigned operands are compared, the Zero
and Carry flags indicate the following relations between
operands:
CMP instruction Example
• Example: destination == source
mov al,5
cmp al,5 ; ZF=1, CF=0

• Example: destination < source

mov al,4
cmp al,5 ; CF=1, SF=1

• Example: destination > source


mov al,6
cmp al,5 ; ZF = 0, CF = 0;
CMP instruction
• When two signed operands are compared, the Sign,
Zero, and Overflow flags indicate the following
relations between operands:
CMP instruction Example

• Example: destination > source


mov al,5
cmp al,-2 ; Sign flag == Overflow flag (SF=0, OF=0)

• Example: destination < source

mov al,-1
cmp al,5 ; Sign flag != Overflow flag (SF=1, OF=0)

• 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.
Conditional Instruction

Go to Tuturialpoint.com for better understanding of unconditional jumping instruction


Conditional structures
• There are no high-level logic (if else, while etc) structures
such as if-then-else, in the IA-32 instruction set. But, you
can use combinations of comparisons and jumps to
implement any logic structure.

• First, an operation such as CMP, AND or SUB etc is executed


to modified the CPU flags. Second, a conditional jump
instruction tests the flags and change the execution flow
accordingly.
Mov al, 1
CMP AL, 0
JZ L1
.
.
.
L1:
Jcond instruction
• A conditional jump instruction branches to a label
when specific register or flag conditions are met.
Otherwise, if the flag condition is false, the
instruction immediately following the conditional
• jump is executed.
Jcond destination

• Divided into Four groups: (some are the same)


1. based on specific flag values
2. based on equality between operands
3. based on comparisons of unsigned operands
4. based on comparisons of signed operands
Jumps based on specific flags
Jumps based on equality

No flags are tested for these Conditional jumps.


Although it may work the same. JE and JZ same working
Jumps based on unsigned comparisons

> ≧ <

The jumps in above table are only meaningful when comparing unsigned values.
Examples
Examples
• Compare unsigned AX to BX, and copy the larger of the two
into a variable named Large
//C++ code
mov Large,bx int a, b, large
cmp ax,bx large =b;
jna Next if (a<b)
mov Large,ax goto Next;
Next: else
large = a;
Examples
mov edx,0A523h
cmp edx,0A523h
jne L5 ; jump not taken
je L1 ; jump is taken

mov cx,0FFFFh
inc cx
jcxz L2 ; jump is taken

xor cx,cx
jcxz L2 ; jump is taken
Examples

mov al, 1
mov bl, 0
cmp al, bl
dec al
Je L1 ; jump taken?

Mov cl,-1
mov al, 1
mov bl, 0
cmp al, bl
inc cl
Jb L1 ; jump taken?
Examples
• The JMP instruction can be used for implementing
loops. For example, the following code snippet can be
used for executing the loop-body 10 times.

MOV CL, 10

L1:

<LOOP-BODY>

DEC CL

JNZ L1 ;jmp if not zero or ZF!=0


String encryption
key

message encoder
(plain text)
unintelligible string
(cipher text)
message encoder
(plain text)
key
The XOR instruction has an interesting property. If an integer X is
XORed with Y and the resulting value is XORed with Y again, the value produced is
X:
((X ⊗ Y) ⊗ Y) = X

This “reversible” property of XOR provides an easy way to perform a simple form of
data encryption: A plain text message is transformed into an encrypted string called
cipher text by XORing each of its characters with a character from a third string
called a key. The intended viewer can use the key to decrypt the cipher text and
produce the original plain text.
Conditional structures
How language compilers translate conditional structures into
low-level machine code. Let’s find out how that is done.
Block-structured IF statements
Assembly language programmers can easily translate
logical statements written in C++/Java into assembly
language. For example:

if( op1 == op2 ) mov eax,op1


X = 1; cmp eax,op2
jne L1
else
mov X,1
X = 2; jmp L2
L1: mov X,2
Next statements… L2:
Next Instructions…
op1 and op2 are memory operands (variables), one of them must be moved to a register before executing CMP

Task: Implement the above C++ code using JE


Do it by yourself

if op1 == op2 then Assembly code?


if X > Y then
call Routine1
else
call Routine2
end if
else
call Routine3
end if
Compound Expression with OR

if (al > bl) OR (bl > cl)


X = 1;

We can use "fall-through" logic to keep the code as short as


possible:

cmp al,bl ; is AL > BL?


ja L1 ; yes
cmp bl,cl ; no: is BL > CL?
jbe next ; no: skip next statement
L1: mov X,1 ; set X to 1
next:
WHILE Loops
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)
{
< …some statements…>
eax = eax + 1;
}

beginwhile:
cmp eax,ebx ; check loop condition
jae endwhile ; false? exit loop
< …some statements…>
inc eax
jmp beginwhile ; repeat the loop
endwhile:

When implementing this structure in assembly language, it is convenient to


reverse the loop condition and jump to endwhile if a condition becomes
true.
Your turn . . .
Implement the following loop, using unsigned 32-bit integers:

while( ebx <= val1)


{
ebx = ebx + 5;
val1 = val1 - 1
}

beginwhile: cmp ebx,val1 ; check loop condition


ja endwhile ; false? exit loop
add ebx,5 ; body of loop
dec val1
jmp beginwhile ; repeat the loop
endwhile:
Example: IF statement nested in a loop
while(eax < ebx) _while: cmp eax, ebx
{ jae _endwhile
eax++; inc eax
if (ebx==ecx) cmp ebx, ecx
X=2; jne _else
else mov X, 2
X=3; jmp _while
} _else: mov X, 3
jmp _while
_endwhile:

You might also like