1 Conditional Processing Chapter 6 Adapted with many modifications from slides prepared by Professor Mario Marchand Computer Science Dept. University.

Slides:



Advertisements
Similar presentations
Jump Condition.
Advertisements

CSC 221 Computer Organization and Assembly Language Lecture 21: Conditional and Block Structures: Assembly Programs.
NEG Instruction Change operand content into two’s complement (negative value) and stored back into its operand mov bl, b neg bl; bl = mov.
ACOE2511 Assembly Language Arithmetic and Logic Instructions.
Computer Organization & Assembly Language
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You may modify.
Assembly Language for Intel-Based Computers
CS2422 Assembly Language & System Programming October 17, 2006.
CS2422 Assembly Language and System Programming Conditional Processing Department of Computer Science National Tsing Hua University.
Flow Control Instructions
Conditional Processing If … then … else While … do; Repeat … until.
Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures.
ICS312 Set 9 Logic & Shift Instructions. Logic & Shift Instructions Logic and Shift Instructions can be used to change the bit values in an operand. The.
Microprocessors Monday, Apr. 13 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.
Assembly Language for Intel-Based Computers, 6 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You may modify.
(Flow Control Instructions)
Decision Structures – Code Generation Lecture 24 Mon, Apr 18, 2005.
1 Logic, Shift, and Rotate Instructions Read Sections 6.2, 7.2 and 7.3 of textbook.
Assembly Language. Symbol Table Variables.DATA var DW 0 sum DD 0 array TIMES 10 DW 0 message DB ’ Welcome ’,0 char1 DB ? Symbol Table Name Offset var.
Chapter 5 Branching and Looping.
EEL 3801 Part V Conditional Processing. This section explains how to implement conditional processing in Assembly Language for the 8086/8088 processors.
LEA instruction The LEA instruction can be used to get the offset address of a variable Example ORG 100h MOV AL, VAR1 ; check value of VAR1 by moving it.
Conditional Loop Instructions, Conditional Structures
COMP 2003: Assembly Language and Digital Logic Chapter 2: Flags and Instructions Notes by Neil Dickson.
Assembly 06. Outline cmp (review) Jump commands test mnemonic bt mnemonic Addressing 1.
2/20/2016CAP 2211 Flow Control Instructions. 2/20/2016CAP 2212 Transfer of Control Flow control instructions are used to control the flow of a program.
CSC 221 Computer Organization and Assembly Language Lecture 20: Conditional and Block Structures.
Jumps, Loops and Branching. Unconditional Jumps Transfer the control flow of the program to a specified instruction, other than the next instruction in.
Computer and Information Sciences College / Computer Science Department CS 206 D Computer Organization and Assembly Language.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 251 Introduction to Computer Organization.
Bitwise and Logical Manipulations Assembly Language Programming University of Akron Dr. Tim Margush.
Selection and Iteration Chapter 8 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
Assembly Language for Intel-Based Computers, 4 th Edition Week 10: Conditional Processing Slides modified by Dr. Osama Younes.
Practical Session 2 Computer Architecture and Assembly Language.
1 Chapter 6 Conditional Processing Assembly Language for Intel-Based Computers, 3rd edition Kip R. Irvine.
CSC 221 Computer Organization and Assembly Language
Assembly Language for Intel-Based Computers, 5th Edition
Assembly Language Programming of 8085
Homework Reading Labs PAL, pp
Practical Session 2.
Instruksi Set Prosesor 8088
Microprocessor and Assembly Language
EE3541 Introduction to Microprocessors
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Basic Assembly Language
Assembly IA-32.
Assembly Language Lab (4).
INSTRUCTION SET.
More on logical instruction and
Assembly Language Programming Part 2
Microprocessor and Assembly Language
CS 301 Fall 2002 Assembly Instructions
Computer Organization and Assembly Language
Flags Register & Jump Instruction
Shift & Rotate Instructions)
Program Logic and Control
Program Logic and Control
Homework Reading Machine Projects Labs PAL, pp
Shift & Rotate Instructions)
Flow Control Instructions
EECE.3170 Microprocessor Systems Design I
High-level language structures
Computer Organization and Assembly Language
Assembly Language for Intel 8086 Jump Condition
Chapter 7 –Program Logic and Control
Carnegie Mellon Ithaca College
Chapter 8: Instruction Set 8086 CPU Architecture
Chapter 7 –Program Logic and Control
Computer Architecture and Assembly Language
Presentation transcript:

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

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

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.

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” = b “Z” = b “a” = b“z” = b u Checking/Changing keyboard lights in DOS Location memory location 417h there is byte Insert (toggle)Right shift Caps lock (toggle) Left shift Num lock (toggle)Left or right control Scroll lock (toggle) Left or right alt

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)

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 = b

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

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

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 /88 3 cycles 4 cycles cycles 2 cycles cycles 2 cycles cycle 1 cycle

Logic Instructions: Masm listing n This is from a listing of a 32 bit program n E 66| 33 C0 xor ax, ax ; 16 bit | 0B C0 or ax, ax | B mov ax, | 83 F8 00 cmp ax, C 33 C0 xor eax, eax ; 32 bit E 0B C0 or eax, eax B mov eax, 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.

Logic Instructions: Case conversions n “A" = b = 41h "a" = b = 61h To convert from upper case to lower case we can use add dl, 20h ; 20h = b But or dl, 20h ;converts from upper to lower case and dl, 0DFh ;converts from lower to upper case since DFh = b Note: or dl, 20h and and dh, 0DFh leaves lower case characters in dl unchanged and upper case characters in dh unchanged Bit 5

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...)

13 n Determine the value of the specified flags mov ah, b test ah, b ; ZF __ SF __ CF __ OF __ test ah, b ; 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

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 b b mask b b final al b b ZF = 0 val b b mask b b final al b b ZF = 1

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

Bolean operations in Java n & bitwise and | bitwise or ^ bitwise xor ~ bitwise not n Example: byte x = 3;// b byte y = 10;// b byte z; z = (byte)(x & y);// z = 2 or b z = (byte)(x | y);// z = 11 or b z = (byte)(x ^ y);// z = 9 or b z = (byte)(~x); // z = -4 or b

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

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

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

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

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

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

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

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 (::)

Offsets in Jxxx and Jmp n Machine code for jumps n Example: Offset Machine code Assembly code 00018B71 A R mov eax, x 00018B 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

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

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 = 0FFFFFFF6h writes +90

28 The isAlpha Procedure ; IsAlpha sets ZF = 1 iff the character ; in AL is alphabetic. isAlpha PROC push eax ; save EAX and al, b ; 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

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

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

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.

Conditional Loop instructions n See chapter 5 slides for the LOOP, LOOPD, LOOPW instructions n 06/slideshows/chap05.ppt 06/slideshows/chap05.ppt

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

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!)

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

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?

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:

Observation n Notice that the inequality used in assembler is frequently just the opposite that is used in the high level language

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?

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

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

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

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

Finite State Machines n A finite state machine for decimal integers n See the textbook’s program on pages AB C +, - digit start

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 , - D E 0, b

46 Program for numbers n examples/Chapter06/Finite.asm examples/Chapter06/Finite.asm