11-Condition FINAL
11-Condition FINAL
Reference:
Section 6.2 and 6.3 of
chapter 6 of kip irvine
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
XOR is a useful way to invert the bits in an operand and data encryption
Applications (1 of 5)
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
mov al,4
cmp al,5 ; CF=1, SF=1
mov al,-1
cmp al,5 ; Sign flag != Overflow flag (SF=1, OF=0)
> ≧ <
≦
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
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:
beginwhile:
cmp eax,ebx ; check loop condition
jae endwhile ; false? exit loop
< …some statements…>
inc eax
jmp beginwhile ; repeat the loop
endwhile: