Download presentation
Presentation is loading. Please wait.
Published byKelley Elliott Modified over 8 years ago
2
1 Conditional Processing Chapter 6 Adapted with many modifications from slides prepared by Professor Mario Marchand Computer Science Dept. University of Ottawa Revised: 3/14/05, 3/17/05, 3/11/06
3
2 Conditional Processing n 6.1 Boolean and Comparison Instructions n 6.2 Conditional Jumps n 6.3 Conditional Loop Instructions n 6.4 Conditional Structures n 6.5 Application: Finite state machine n 6.6 Using the.IF Directive
4
3 6.1 Introduction: n Structured programming: Any program can be written using a combination of Sequential code Loops Decision structures n We have looked at sequential code, we have limited loop structures but no decision structures.
5
4 6.2 Logic Instructions n Why study Logic instructions AND, OR, XOR, and NOT instructions? n Useful for bit manipulation. Some examples: u Convert between upper and lower case “A” = 0100 0001b “Z” = 0101 1010b “a” = 0110 0001b“z” = 0111 1010b u Checking/Changing keyboard lights in DOS Location memory location 417h there is byte 0010 1010 Insert (toggle)Right shift Caps lock (toggle) Left shift Num lock (toggle)Left or right control Scroll lock (toggle) Left or right alt
6
5 6.2 Logic Instructions n Syntax for AND, OR, XOR, and TEST: op-code destination, source n AND, OR and XOR perform the Boolean bitwise operation and store the result into destination. n TEST is just an “AND” but the result is not stored n Both operands must be of the same type u either byte, word or dword n Both operands cannot be memory u again: memory to memory operations are forbidden n They clear (ie: set to zero) CF and OF n They affect SF, ZF, PF according to the result of the operation (as usual)
7
6 6.2 Logic Instructions: AND n The source is often an immediate operand called a bit mask: used to determine which bits should be cleared. n To clear a bit we use an AND since: u 0 AND b = 0 (b is cleared) u 1 AND b = b (b is conserved) n Example: to clear the sign bit of AL without affecting the others, we do: and al, 7Fh ;msb of AL is cleared since 7Fh = 0111 1111b
8
7 6.2 Logic Instructions: OR n To set (ie: set to 1) certain bits, we use OR: u 1 OR b = 1 (b is set) u 0 OR b = b (b is conserved) n To set the sign bit of AH, we do: or ah, 80h n To test if EBX=0 we can do: or ebx, ebx u This does not change the number in EBX and set ZF=1 if and only if ebx=0
9
8 6.2 Logic Instructions: XOR n XOR can be used to complement, conserve, or clear certain bits because: u b XOR 1 = NOT(b) (b is complemented) u b XOR 0 = b (b is conserved) u b XOR b = 0 (b is cleared) n Example: to initialize a register to 0 we can use a two-byte instruction: xor ax, ax instead of the three-byte instruction: mov ax, 0 This was faster on 8088's
10
9 6.2 Logic Instructions: OR and XOR Using OR and XOR in strange ways: Assembly xor ax, ax mov ax, 0 or ax, ax cmp ax, 0 16-bit Machine 33 C0 B80000 code 0B C0 83 F8 00 bytes each 2 3 86/88 3 cycles 4 cycles 286 2 cycles 2 cycles 386 2 cycles 2 cycles 486 1 cycle 1 cycle
11
10 6.2 Logic Instructions: Masm listing n This is from a listing of a 32 bit program n 0000000E 66| 33 C0 xor ax, ax ; 16 bit 00000011 66| 0B C0 or ax, ax 00000014 66| B8 0000 mov ax, 0 00000018 66| 83 F8 00 cmp ax, 0 0000001C 33 C0 xor eax, eax ; 32 bit 0000001E 0B C0 or eax, eax 00000020 B8 00000000 mov eax, 0 00000025 83 F8 00 cmp eax, 0 n Notice: The main part of the machine code is the “same” when using ax and eax n The prefix “66” is used in 32-bit programs to say use 16-bit registers. In 16-bit programs, it says to use 32-bit registers.
12
11 6.2 Logic Instructions: Case conversions n “A" = 0100 0001b = 41h "a" = 0110 0001b = 61h To convert from upper case to lower case we can use add dl, 20h ; 20h = 10 0000b But or dl, 20h ;converts from upper to lower case and dl, 0DFh ;converts from lower to upper case since DFh = 1101 1111b Note: or dl, 20h and and dh, 0DFh leaves lower case characters in dl unchanged and upper case characters in dh unchanged Bit 5
13
12 6.2 Logic Instructions: NOT and NEG n To invert all the bits (ones complement): NOT destination u does not affect any flag u destination cannot be an immediate operand n To perform twos complement: NEG destination u affect SF, PF, ZF according to result u CF is set to 1 unless the result is 0 u OF=1 iff destination is the smallest negative number (ie: 80h for byte, 8000h for word...)
14
13 n Determine the value of the specified flags mov ah, 10101010b test ah, 11110000b ; ZF __ SF __ CF __ OF __ test ah, 00010000b ; ZF __ SF __ CF __ OF __ mov eax, 345 cmp eax, 456 ; ZF __ SF __ CF __ OF __ cmp eax, 123 ; ZF __ SF __ CF __ OF __ 6.2 Logic Instructions: Your Turn Problem 1 ; ZF: 0 SF: 1 CF: 0 OF: 0 ; ZF: 1 SF: 0 CF: 0 OF: 0 ; ZF: 0 SF: 1 CF: 1 OF: 0 ; ZF: 0 SF: 0 CF: 0 OF: 0
15
14 6.2 Logic Instructions: TEST n Same as AND except does not store the result. It just sets the flags n Often used for testing individual bits Example: mov al, val test al, 01b ; al unchanged and al, 01b ; al changed test and both val 00001111b 00001111b mask 00000001b 00000001b final al 00001111b 00000001b ZF = 0 val 00001010b 00001010b mask 00000001b 00000001b final al 00001010b 00000000b ZF = 1
16
15 6.2 Logic Instructions: CMP CMP destination,source n Performs: destination - source but does not store the result of this subtraction into destination n But the flags are affected like SUB n Same restrictions on operands as SUB n Very often used just before performing a conditional jump n CMP is similar to TEST except TEST does an AND operation
17
16 6.2 Bolean operations in Java n & bitwise and | bitwise or ^ bitwise xor ~ bitwise not n Example: byte x = 3;// 0000 0011b byte y = 10;// 0000 1010b byte z; z = (byte)(x & y);// z = 2 or 0000 0010b z = (byte)(x | y);// z = 11 or 0000 1011b z = (byte)(x ^ y);// z = 9 or 0000 1001b z = (byte)(~x); // z = -4 or 1111 1100b
18
17 6.3 Conditional Jumps n A conditional jump transfers control to the destination_label when a flag condition is true. Syntax: u Jxxx destination_label u In 16-bit code, the destination_label must be within -128 to +127 bytes from the current location. (Short jump.) u **** In 32-bit code, there is no limitation **** n Groupings of conditional jumps: u General comparison jumps (signed/unsigned) u Unsigned comparison jumps u Signed comparison jumps
19
18 6.3 General Comparison Jumps Description Mnemonic Description Flags/Registers JZ Jump if zero ZF = 1 JE Jump if equal =ZF = 1 JNZ Jump if not zero ZF = 0 JNE Jump if not equal !=ZF = 0 JC Jump if carry CF = 1 JNC Jump if not carry CF = 0 JCXZ Jump if CX = 0 CX = 0 JECXZ Jump if ECX = 0 ECX = 0
20
19 6.3 General Comparison Jumps Description Mnemonic Description Flags/Registers JO Jump overflow OF = 1 JNO Jump not overflow OF = 0 JS Jump sign SF = 1 JNS Jump not signSF = 0 JP Jump if parity even PF = 1 JNP Jump if parity odd PF = 0
21
20 6.3 Jumps for Unsigned Comparison 6.3 Jumps for Unsigned Comparison Description Mnemonic Description Flags/Registers JAJump above > CF = 0 & ZF = 0 JNBE Jump not below or equal CF = 0 & ZF = 0 JAEJump above or equal >= CF = 0 JNBJump not below CF = 0 JBJump below < CF = 1 JNAE Jump not above or equal CF = 1 JBEJump below or equal <= CF = 1 or ZF = 1 JNAJump not above CF = 1 or ZF = 1
22
21 6.3 Jumps for Signed Comparison 6.3 Jumps for Signed Comparison Description Mnemonic Description Flags/Registers JGJump greater than > SF = OF & ZF = 0 JNLE Jump not less than or equal SF = OF & ZF = 0 JGEJump greater than or equal >= SF = OF JNLJump not less than SF = OF JLJump less than < SF != OF JNGE Jump not greater than or equal SF != OF JLEJump less than or equal <= SF != OF or ZF = 1 JNGJump not greater than SF != OF or ZF = 1
23
22 6.3 Using conditional jumps Consider mov eax, x sub eax, y jz SpecialCase n If x equals y then eax is zero, ZF is set, and the jump is taken n If x is not equal to y then eax is not zero, ZF is clear and the jump is not taken We could have used je SpecialCase
24
23 6.3 Using conditional jumps n Simpler if CMP is used before a Jxxx n To branch to label “quit” when AX > BX under a signed interpretation: cmp ax, bx jg quit u if AX = 1, BX = FFFFh, the jump is taken n To branch to label “quit” when AX > BX under an unsigned interpretation: cmp ax, bx ja quit u if AX = 1 and BX = FFFFh, the jump is not taken
25
24 6.3 Unconditional Jump n To jump unconditionally: jmp destination_label u where the destination_label can occur anywhere within the same procedure or is a global label (::)
26
25 6.3 Offsets in Jxxx and Jmp n Machine code for jumps n Example: Offset Machine code Assembly code 00018B71 A1 00000001 R mov eax, x 00018B76 05 00000005 R sub eax, y 00018B7C 7D 02 jge LabGE 00018B7E 33 C0 xor eax, eax 00018B80 LabGE: offset n Location if branch is taken: 00018B7E EIP (points to next instruction) + 02 Offset in machine code 00018B80 Offset to jump to if branch taken Relocatable jge Relocatable
27
26 6.3 Offsets in Jxxx and Jmp (cont.) n Machine code for jumps n Example: Offset Machine code Assembly code 00018B80 48 LabG: dec eax 00018B81 7F FD jg LabG 00018B83 jgoffset n Location if branch is taken: 00018B83 EIP (points to next instruction) + FFFFFFFD Offset in machine code (sign extended) 00018B80 Offset jumped to if branch taken In 32 bit-code, jump offsets may be 8, 16, or 32 bits In 16 bit-code, jump offsets are (almost always) 8 bits
28
27 Jumps: Your Turn: Problem 2 n Determine the output eax ebx CF ZF SF mov eax, 30 mov ebx, 70 cmp eax, ebx jnc L1 add eax, 10 L1: jz L2 add eax, 20 L2: sub eax, ebx jg L3 add eax, 100 L3: call writeInt 30 70 1 0 1 40 0 0 0 60 0 0 0 -10 = 0FFFFFFF6h 1 0 1 90 1 0 0 writes +90
29
28 The isAlpha Procedure ; IsAlpha sets ZF = 1 iff the character ; in AL is alphabetic. isAlpha PROC push eax ; save EAX and al, 11011111b ; clear bit 5 cmp al, 'A' ; check 'A'..'Z' range jb B1 cmp al, 'Z' ja B1 test eax, 0 ; ZF = 1 B1: pop eax ; restore EAX ret isAlpha ENDP
30
29 Ex: checking for alphabetic input n The isAlpha procedure sets ZF to 1 iff the character in AL is alphabetic (upper or lower case). u When comparing characters: use unsigned comparison jumps u Trick: clear bit #5 and check if ‘A’ <= char <= ‘Z’ (converts lower case to upper case) cmp al, 'A' will clear ZF when char < ‘A’ cmp al, 'Z' will clear ZF when char > ‘Z’ test eax, 0 is executed only if ‘A’ <= char <= ‘Z’ and sets ZF to 1
31
30 Ex: encryption n The following property of XOR b XOR 0 = b (bit b is conserved) b XOR 1 = NOT(b) (bit b is complemented) can be used to encrypt files n If a character is stored in AL, XORing AL once encrypts the character. Using it the second time decrypts (restores) the character. key = 137 ;a 1-byte secret key xor al,key ;AL is encrypted xor al,key ;AL is restored to its original value
32
31 Ex: file encryption (cont) n encrypt.asm can be used to encrypt and decrypt a message encrypt.asm u The key was picked carefully to keep encoded messages in ASCII u Run the program once and type in message to be encrypted u Run the program again and type in the encrypted message to get the plain text message.
33
32 6.4 Conditional Loop instructions n See chapter 5 slides for the LOOP, LOOPD, LOOPW instructions n http://www.cs.plu.edu/courses/csce380/spring 06/slideshows/chap05.ppt http://www.cs.plu.edu/courses/csce380/spring 06/slideshows/chap05.ppt
34
33 6.5 Conditional Loops n The LOOPZ and LOOPE (loop if zero, loop if equal) continues a loop while ZF = 1 and ECX > 0 u ECX is first decremented, then the condition is tested (a trivial extension of LOOP) n LOOPNZ and LOOPNE continues the loop while ZF = 0 and ECX > 0 n Syntax (same as LOOP): loopxx destination_label n CX is used when in the 16-bit mode n The destination_label uses a 1 byte offset which is between –128 and +127
35
34 6.5 Example: scanning an integer array until a nonzero value is found mov ebx,OFFSET Warr-2 ;Warr is a word array mov ecx, N ;of N elements next: add ebx,2 cmp word ptr [ebx], 0 loopz next ;loop until nonzero value is found n Note: it is an error to inverse the CMP and ADD operations (ADD affects the flags!)
36
35 6.5 High-Level Control Structures n High-level languages uses high-level structures such as if-then-else, case, while... to control the flow of execution u algorithms are normally expressed in terms of these high-level structures n Processors only provide conditional and unconditional jumps and loops u thus we need to decompose the high-level control flow structures into low-level ones n We give here a few examples; see textbook for more
37
36 6.5 If-Then-Else n Example: if (op1 < op2) then statement 1 else statement 2 end if n Analysis: u Suggestion: Use flow chart to do analysis u there is a jxxx to else when op1 >= op2 u there is a jump from end of statement 1 to end_if n ASM solution for signed comparison: cmp op1,op2 jge else_ statement 1 jmp end_if else_: statement 2 end_if: n Note: “else” is a ASM reserved word. We use label “else_” instead What if the jmp is omitted?
38
37 6.5 While n Example : while (op1 < op2) { Statement } n Analysis: u jxxx to end_while when op1 >= op2 u jump from before the end_while to while_ n AMS solution for an unsigned comparison: while_: cmp op1,op2 jae end_while statement jmp while_ end_while:
39
38 6.5 Observation n Notice that the inequality used in assembler is frequently just the opposite that is used in the high level language
40
39 6.5 Switch ( Case) n Example: switch (input) { case ‘A’ : procA; break; case ‘B’ : procB; break; case ‘C’ : procC; break; } n Analysis: cmp and jxxx for each case. After call jump to end ASM solution: cmp input,’A’ jne L1 call procA jmp L3 L1: cmp input, ’B’ jne L2 call procB jmp L3 L2: cmp input, ’C’ jne L3 call procC L3: What if the jmp is omitted?
41
40 6.5 Your turn: Compile problems n Draw flow charts and compile n if (a < b && c < d) x = 2; n Repeat float charting and compiling n If (a > b || c > d) x = 3; Assume variables are signed, 32 bit ints
42
41 6.3 Bit Testing Instructions n In multithreaded programs, it is sometimes necessary to synchronize the threads n Operating system classes discuss the flags (semaphores) used for this process n The bit testing instructions can be used to test semaphores n BT Bit Test n BTC Bit Test and Complement n BTR Bit Test and Reset n BTS Bit Test and Set
43
42 6.6 Finite State Machines n A finite state machine (FSM) is a machine or program that changes state based on some input n We can use a directed graph to represent a FSM n Illegal inputs (input for which no path exists) cause errors n Example: Recognize a binary number ending in “b” 0, 1 AB C b start Start stateStateTerminal state Edge or arc
44
43 6.6 Finite State Machines n Finite state machines are sometimes called nondeterministic finite automatons. n A technical definition of a finite state machine says that the finite state machine has u A finite number of states u Input characters that it can recognize u Transitions (the edges or arcs) labeled by input characters going from one state to another state u One of the states is the start state u One or more of the states are terminal states
45
44 6.6 Finite State Machines n A finite state machine for decimal integers n See the textbook’s program on pages 213-216 AB C +, - digit start
46
45 6.6 Finite State Machines n Design a finite state machine that recognizes either a binary or decimal number n Complication: initial 0’s and 1’s could begin either a decimal or binary numbers start C digit AB 2.. 9 +, - D E 0, 1 2.. 9 b
47
46 Program for numbers n http://www.cs.plu.edu/courses/csce380/spring06/ examples/Chapter06/Finite.asm http://www.cs.plu.edu/courses/csce380/spring06/ examples/Chapter06/Finite.asm
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.