View
225
Download
0
Embed Size (px)
Citation preview
CS2422 Assembly Language and System Programming
Conditional Processing
Department of Computer ScienceNational Tsing Hua University
CS2422 Assembly Language and System ProgrammingAssembly Language for Intel-Based Computers, 5th Edition
Chapter 6: Conditional Processing
(c) Pearson Education, 2006-2007. All rights reserved. You may modify and copy this slide show for your personal use, or for use in the classroom, as long as this copyright statement, the author's name, and the title are not changed.
Slides prepared by the author
Revision date: June 4, 2006
Kip Irvine
3
Chapter Overview
Boolean and Comparison Instructions AND, OR, XOR, NOT, TEST, CMP
Conditional Jumps Conditional Loop Instructions Conditional Structures Application: Finite-State Machines (skipped) Decision Directives Brief Overview of Chapter 7
4
AND Instruction
Perform a Boolean AND operation between each pair of matching bits in two operands
Syntax:AND destination, source
(same operand types as MOV) AND
5
OR Instruction
Performs a Boolean OR operation between each pair of matching bits in two operands
Syntax:OR destination, source
OR
6
XOR Instruction
Performs a Boolean exclusive-OR operation between each pair of matching bits in two operands
Syntax:XOR destination, source XOR
0 0 1 1 1 0 1 10 0 0 0 1 1 1 1
0 0 1 1 0 1 0 0
XOR
invertedunchanged
XOR is a useful way to toggle (invert) the bits in an operand.
7
NOT Instruction
Performs a Boolean NOT operation on a single destination operand
Syntax:NOT destination
NOT
0 0 1 1 1 0 1 1
1 1 0 0 0 1 0 0
NOT
inverted
8
Application – An Example
Task: Convert character in AL to upper case Solution: Use AND instruction to clear bit 5
mov al,'a‘ ; AL = 01100001band al,11011111b ; AL = 01000001b
ASCII code of ‘A’ = 41HASCII code of ‘a’ = 61H
9
TEST Instruction
Performs a nondestructive AND 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.
test al,00000011bjnz ValueFound
10
CMP Instruction (1/2)
Compare destination operand to source operand Nondestructive subtraction of source from
destination (destination operand is not changed) Syntax: CMP destination, source Example: destination < source
Example: destination > source
mov al,4cmp al,5 ; Carry flag set
mov al,6cmp al,5 ; ZF = 0, CF = 0
(both the Zero and Carry flags are clear)
11
CMP Instruction (2/2)
The comparisons shown here are performed with signed integers.
Example: destination > source
• Example: destination < source
mov al,5cmp al,-2 ; Sign flag == Overflow flag
mov al,-1cmp al,5 ; Sign flag != Overflow flag
12
What's Next
Boolean and Comparison Instructions Conditional Jumps Conditional Loop Instructions Conditional Structures Application: Finite-State Machines (skipped) Decision Directives Brief Overview of Chapter 7
13
CMP and Jcond Instruction
The IF statement in C and PASCAL is converted into CMP and Jcond instructions in x86 Assembly:
CMP X, op1JNG EndIf<…>
EndIf:
If (X > op1)Then
<…>End If
14
Jcond Instruction
A conditional jump instruction branches to a label when specific register or flag conditions are met
Examples: 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 jumps to a label if the Sign flag is set JNE, JNZ jump to a label if the Zero flag is clear JECXZ jumps to a label if ECX equals 0
15
Jcond Ranges
Jump destinations usually confined within same procedure
Prior to the 386: jump must be within –128 to +127 bytes from
current location counter IA-32 processors:
32-bit offset permits jump anywhere in segment
20
More Frequently Used Jcond
JE (Equal) JNE (Not Equal) JG or JGE (Greater Than or Equal) JL or JLE (Less Than or Equal)
Note: JG=JNLE, JGE=JNL, …etc.
21
Simple IF
If (op1=op2) then <…> end if Two different approaches:
CMP op1, op2JE TrueJMP EndIf
True:<…>
EndIf
CMP op1, op2JNE False<…>
False:
22
IF … AND …
CMP X, op1JNG EndIfCMP Y, op2JNLE EndIfCMP ………
<…>EndIf:
If (X > op1)and(Y <=op2)and…
Then<…>
End If
23
IF … OR …
CMP X, op1JG TrueCMP Y, op2JLE TrueCMP ………JMP EndIf
True:<…>
EndIf:
If (X > op1) or(Y <=op2) or…
Then<…>
End If
24
Applications
Task: Jump to a label if unsigned EAX is greater than EBX
Solution: Use CMP, followed by JA
Task: Jump to a label if signed EAX is greater than EBX
Solution: Use CMP, followed by JG
cmp eax,ebxja Larger
cmp eax,ebxjg Greater
25
What's Next
Boolean and Comparison Instructions Conditional Jumps Conditional Loop Instructions Conditional Structures Application: Finite-State Machines (skipped) Decision Directives Brief Overview of Chapter 7
26
LOOPZ and LOOPE
Syntax: LOOPE destination ; loop if equalLOOPZ destination ; loop if zero
Destination label must be in -128 ~ +127 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.
27
LOOPNZ and LOOPNE
Syntax: LOOPNZ destinationLOOPNE 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.
28
LOOPNZ Example
Find the first positive value in an array:.dataarray SWORD -3,-6,-1,-10,10,30,40,4.code
mov esi,OFFSET arraymov ecx,LENGTHOF arraysub esi,TYPE array
next:add esi, TYPE arraytest WORD PTR [esi],8000h ; test sign bitloopnz next ; continue loopjnz quit ; none found… ; ESI points to value
quit:
29
What's Next
Boolean and Comparison Instructions Conditional Jumps Conditional Loop Instructions Conditional Structures Application: Finite-State Machines (skipped) Decision Directives Brief overview of Chapter 7
30
Block-Structured IF Statements
Assembly language programmers can easily translate logical statements written in C++/Java into assembly language:
mov eax,op1 cmp eax,op2 jne L1 mov X,1 jmp L2
L1: mov X,2L2:
if( op1 == op2 ) X = 1;else X = 2;
31
WHILE
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.
While:CMP op1, op2JNL EndDo<…>JMP While
EndDo:
DO WHILE(op1<op2)<…>
END DO
32
REPEAT UNTIL
repeat:<…>CMP X, op1JE EndIfCMP Y, op2JNG repeat
EndIf:
REPEAT<…>
UNTIL(X == op1) or(Y > op2)
33
What's Next
Boolean and Comparison Instructions Conditional Jumps Conditional Loop Instructions Conditional Structures Application: Finite-State Machines (skipped) Decision Directives
.IF, .WHILE, and .REPEAT directives (note: not instructions)
Brief Overview of Chapter 7
34
Runtime Expressions
.IF, .ELSE, .ELSEIF, and .ENDIF can be used to instruct the assembler to create block-structured IF statements
• MASM generates "hidden" code for you, consisting of code labels, CMP and conditional jump instructions
.IF eax>ebxmov edx,1
.ELSEmov edx,2
.ENDIF
.IF eax>ebx && eax>ecxmov edx,1
.ELSEmov edx,2
.ENDIF
36
MASM-Generated Code
MASM automatically generates an unsigned jump, because val1 is unsigned JBE: jump if below or equal
mov eax,6cmp eax,val1jbe @C0001 mov result,1
@C0001:
.dataval1 DWORD 5result DWORD ?.codemov eax,6.IF eax > val1mov result,1.ENDIF
Generated code:
37
MASM-Generated Code
MASM automatically generates a signed jump (JLE)
mov eax,6cmp eax,val1jle @C0001 mov result,1
@C0001:
.dataval1 SDWORD 5result SDWORD ?.codemov eax,6.IF eax > val1mov result,1.ENDIF
Generated code:
38
.REPEAT Directive
Executes the loop body before testing the loop condition associated with the .UNTIL directive.
Example:
; Display integers 1 ~ 10:
mov eax,0.REPEAT
inc eaxcall WriteDeccall Crlf
.UNTIL eax == 10
39
.WHILE Directive
Tests the loop condition before executing the loop body. The .ENDW directive marks the end of the loop.
Example:
; Display integers 1 ~ 10:
mov eax,0.WHILE eax < 10
inc eaxcall WriteDeccall Crlf
.ENDW
40
When to Use or Not to Use Directives?
Directives make assembly language easier to write and to understand, by hiding tedious work. (Food for thought: Wouldn’t it be even better to use C language?)
Don’t use directives if you want to have total control.
41
What's Next
Boolean and Comparison Instructions Conditional Jumps Conditional Loop Instructions Conditional Structures Application: Finite-State Machines (skipped) Decision Directives Brief Overview of Chapter 7
42
Logical vs Arithmetic Shifts
A logical shift fills the newly created bit position with zero:
An arithmetic shift fills the newly created bit position with the sign bit:
43
SHL Instruction
Shift left: performs a logical left shift on the destination operand, filling the lowest bit with 0.
Operand types for SHL:
SHL reg,imm8SHL mem,imm8SHL reg,CLSHL mem,CL
(Same for all shift and rotate instructions)
44
SHR Instruction
Shift right: performs a logical right shift on the destination operand. The highest bit position is filled with a zero
Shifting right n bits divides the operand by 2n
mov dl,80shr dl,1 ; DL = 40shr dl,2 ; DL = 10
45
SAL and SAR Instructions
Shift arithmetic left: identical to SHL Shift arithmetic right: performs a right arithmetic
shift on the destination operand
An arithmetic shift preserves the number's sign
mov dl,-80sar dl,1 ; DL = -40sar dl,2 ; DL = -10
46
MUL Instruction
The MUL (unsigned multiply) instruction multiplies an 8-, 16-, or 32-bit operand by either AL, AX, or EAX.
The instruction formats are:MUL r/m8MUL r/m16MUL r/m32 Implied operands:
47
DIV Instruction
Unsigned divide: performs 8-bit, 16-bit, and 32-bit division on unsigned integers
A single operand is supplied (register or memory operand), which is assumed to be the divisor
Instruction formats:DIV r/m8DIV r/m16DIV r/m32
Default Operands:
48
Summary Bitwise inst. (AND, OR, XOR, NOT, TEST)
manipulate individual bits in operands CMP: implied sub for comparing operands
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
Shift: SHL, SHR, SAL, SAR Multiplication and division: MUL, DIV